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() {
|
2023-03-21 06:28:25 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-03-21 06:28:25 +00:00
|
|
|
// Level is a log entry severity level.
|
2023-03-20 06:15:53 +00:00
|
|
|
type Level int
|
|
|
|
|
|
|
|
const (
|
2023-03-21 06:28:25 +00:00
|
|
|
// 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
|
2023-03-21 06:28:25 +00:00
|
|
|
// LevelInfo is regular information relayed back to the user.
|
2023-03-20 06:15:53 +00:00
|
|
|
LevelInfo
|
2023-03-21 06:28:25 +00:00
|
|
|
// LevelDebug is debugging information.
|
2023-03-20 06:15:53 +00:00
|
|
|
LevelDebug
|
2023-03-21 06:28:25 +00:00
|
|
|
// LevelTrace is verbose debugging information.
|
2023-03-20 06:15:53 +00:00
|
|
|
LevelTrace
|
|
|
|
)
|
|
|
|
|
2023-03-21 06:28:25 +00:00
|
|
|
// 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...)
|
|
|
|
}
|