-
Notifications
You must be signed in to change notification settings - Fork 536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Log error appear as info #148
Comments
I use the following. Please note that I switch to the pretty logger in case of debug level. Edit: I updated with a proper interface name package logger
import (
"fmt"
"os"
"strings"
"time"
"github.com/rs/zerolog"
)
// Logger -.
type Logger interface {
Debug(message interface{}, args ...interface{})
Info(message string, args ...interface{})
Warn(message string, args ...interface{})
Error(message interface{}, args ...interface{})
Fatal(message interface{}, args ...interface{})
}
// logger -.
type logger struct {
logger *zerolog.Logger
}
// New -.
func New(level string) Logger {
var l zerolog.Level
switch strings.ToLower(level) {
case "error":
l = zerolog.ErrorLevel
break
case "warn":
l = zerolog.WarnLevel
break
case "info":
l = zerolog.InfoLevel
break
case "debug":
l = zerolog.DebugLevel
break
default:
l = zerolog.InfoLevel
break
}
zerolog.SetGlobalLevel(l)
skipFrameCount := 3
var z zerolog.Logger
if l == zerolog.DebugLevel {
z = zerolog.New(os.Stdout).Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339}).
With().
Timestamp().
CallerWithSkipFrameCount(zerolog.CallerSkipFrameCount + skipFrameCount).
Logger()
} else {
z = zerolog.New(os.Stdout).
With().
Timestamp().
CallerWithSkipFrameCount(zerolog.CallerSkipFrameCount + skipFrameCount).
Logger()
}
return &logger{
logger: &z,
}
}
func (l *logger) formatMessage(message any) string {
switch t := message.(type) {
case error:
return t.Error()
case string:
return t
default:
return fmt.Sprintf("Unknown type %v", message)
}
}
// Debug -.
func (l *logger) Debug(message any, args ...any) {
mf := l.formatMessage(message)
l.log(l.logger.Debug(), mf, args...)
}
// Info -.
func (l *logger) Info(message string, args ...any) {
mf := l.formatMessage(message)
l.log(l.logger.Info(), mf, args...)
}
// Warn -.
func (l *logger) Warn(message string, args ...any) {
mf := l.formatMessage(message)
l.log(l.logger.Warn(), mf, args...)
}
// Error -.
func (l *logger) Error(message interface{}, args ...any) {
mf := l.formatMessage(message)
l.log(l.logger.Error(), mf, args...)
}
// Fatal -.
func (l *logger) Fatal(message interface{}, args ...any) {
mf := l.formatMessage(message)
l.log(l.logger.Fatal(), mf, args...)
os.Exit(1)
}
func (l *logger) log(e *zerolog.Event, m string, args ...any) {
if len(args) == 0 {
e.Msg(m)
} else {
e.Msgf(m, args)
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This project helps me a lot to have a guideline to follow the clean code, so thank you very much.
I'm studying go and forgive me if I did something wrong.
But when I set the log level to error and after creating the configuration, if I call l.error it always appears as info.
I don't know if there's something here that always turns into info or if I'm doing something wrong.
The text was updated successfully, but these errors were encountered: