chinampa/pkg/logger/log.go

97 lines
1.8 KiB
Go
Raw Permalink Normal View History

2023-03-20 06:15:53 +00:00
// Copyright © 2022 Roberto Hidalgo <chinampa@un.rob.mx>
// SPDX-License-Identifier: Apache-2.0
package logger
import (
"context"
"git.rob.mx/nidito/chinampa/pkg/runtime"
"github.com/sirupsen/logrus"
)
var componentKey = "_component"
func init() {
logrus.SetFormatter(new(ttyFormatter))
2023-03-20 06:15:53 +00:00
}
var Main = logrus.WithContext(context.Background())
func Sub(name string) *logrus.Entry {
return logrus.WithField(componentKey, name)
}
// Level is a log entry severity level.
2023-03-20 06:15:53 +00:00
type Level int
const (
// LevelError is the most severe.
LevelError Level = iota + 2
// LevelWarning happens when something is potentially off.
2023-03-20 06:15:53 +00:00
LevelWarning
// LevelInfo is regular information relayed back to the user.
2023-03-20 06:15:53 +00:00
LevelInfo
// LevelDebug is debugging information.
2023-03-20 06:15:53 +00:00
LevelDebug
// LevelTrace is verbose debugging information.
2023-03-20 06:15:53 +00:00
LevelTrace
)
// Configure sets up the Main logger.
2023-03-20 06:15:53 +00:00
func Configure(name string, level Level) {
Main = logrus.WithField(componentKey, name)
if runtime.SilenceEnabled() {
logrus.SetLevel(logrus.ErrorLevel)
} else {
logrus.SetLevel(logrus.AllLevels[level])
}
}
func Debug(args ...any) {
Main.Debug(args...)
}
func Debugf(format string, args ...any) {
Main.Debugf(format, args...)
}
func Info(args ...any) {
Main.Info(args...)
}
func Infof(format string, args ...any) {
Main.Infof(format, args...)
}
func Warn(args ...any) {
Main.Warn(args...)
}
func Warnf(format string, args ...any) {
Main.Warnf(format, args...)
}
func Error(args ...any) {
Main.Error(args...)
}
func Errorf(format string, args ...any) {
Main.Errorf(format, args...)
}
func Fatal(args ...any) {
Main.Fatal(args...)
}
func Fatalf(format string, args ...any) {
Main.Fatalf(format, args...)
}
func Trace(args ...any) {
Main.Trace(args...)
}
func Tracef(format string, args ...any) {
Main.Tracef(format, args...)
}