-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
🚀 Consistent way of logging and fix middleware log format #2432
🚀 Consistent way of logging and fix middleware log format #2432
Conversation
Thanks for opening this pull request! 🎉 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
We should add interface like https://github.com/labstack/echo/blob/master/log.go#L11 to make it customizable (maybe smaller). For example i may want to use zerolog, zap with fiber |
That would be a good addition. I can work on it. But I think a separate PR would be better instead of adding here. |
Ok to me |
I'd prefer Why don't we use a proper logging library like zerolog or zap though? |
We shouldn't add extra dependencies for optional things |
Totally agree with @efectn , I have opened an issue to track this. Please feel free to add additional context. |
I agree with @leonklingele it should be |
ftr: this‘ll soon be part of the Go stdlib 🫨🥳🚀
https://pkg.go.dev/golang.org/x/exp/slog
|
Seems slower than zerolog and zap. At least for now https://github.com/uber-go/zap#performance |
Ya agree, if others agree I will make the change here. |
Congrats on merging your first pull request! 🎉 We here at Fiber are proud of you! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
Why did this get merged already? Didn’t we want to change the order of log level and scope info?
|
Missed it, will revert it next time it is better to store a task in the pull request or to reject it first, then I usually check it first |
I‘d leave it as-is and just do a fixup.
|
what do you think about this kind of solution package main
import (
"fmt"
"log"
)
type Logger struct {
topic string
}
func NewLogger(topic string) *Logger {
return &Logger{topic: topic}
}
func (l *Logger) formatMessage(level, format string, args ...interface{}) string {
message := fmt.Sprintf(format, args...)
if l.topic != "" {
return fmt.Sprintf("[%s] - [%s] %s", level, l.topic, message)
}
return fmt.Sprintf("[%s] - %s", level, message)
}
func (l *Logger) Info(format string, args ...interface{}) {
log.Println(l.formatMessage("Info", format, args...))
}
func (l *Logger) Warning(format string, args ...interface{}) {
log.Println(l.formatMessage("Warning", format, args...))
}
func (l *Logger) Error(format string, args ...interface{}) {
log.Println(l.formatMessage("Error", format, args...))
}
func main() {
cacheLogger := NewLogger("CACHE")
cacheLogger.Warning("Key is deprecated, please use KeyGenerator")
corsLogger := NewLogger("CORS")
corsLogger.Warning("Both 'AllowOrigins' and 'AllowOriginsFunc' have been defined.")
csrfLogger := NewLogger("CSRF")
csrfLogger.Warning("TokenLookup is deprecated, please use KeyLookup")
idempotencyLogger := NewLogger("IDEMPOTENCY")
key := "example_key"
err := fmt.Errorf("an error occurred")
idempotencyLogger.Error("failed to unlock key %q: %v", key, err)
generalLogger := NewLogger("")
generalLogger.Warning("Parse() is deprecated, please use Load() instead.")
} or we use the slog lib |
Description
[MIDDLEWARENAME] - [LOG-LEVEL] text
in all middleware for loggingFixes #2402
Type of change
Please delete options that are not relevant.
Checklist:
Commit formatting:
Use emojis on commit messages so it provides an easy way of identifying the purpose or intention of a commit. Check out the emoji cheatsheet here: https://gitmoji.carloscuesta.me/