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
Logrus Formatter per output? #784
Comments
Solved by using hooks: func initLogger() {
var logLevel = logrus.InfoLevel
if debug {
logLevel = logrus.DebugLevel
}
rotateFileHook, err := rotatefilehook.NewRotateFileHook(rotatefilehook.RotateFileConfig{
Filename: "logs/console.log",
MaxSize: 50, // megabytes
MaxBackups: 3,
MaxAge: 28, //days
Level: logLevel,
Formatter: &logrus.JSONFormatter{
TimestampFormat: time.RFC822,
},
})
if err != nil {
logrus.Fatalf("Failed to initialize file rotate hook: %v", err)
}
logrus.SetLevel(logLevel)
logrus.SetOutput(colorable.NewColorableStdout())
logrus.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
FullTimestamp: true,
TimestampFormat: time.RFC822,
})
logrus.AddHook(rotateFileHook)
} |
Thanks for the solution, but I find it somewhat overkilling to add another dependency (lumberjack) just to solve a simple problem, namely writing logs to file in different format. Can we consider re-open this issue? thanks |
@UnAfraid How do you have two different outputs/formatters using vanilla logrus? |
By using hook, either existing one or implement your own |
Agree this should be re-opened, having a trivial way to configure a formatter for the built-in hooks is a must. |
Yes, it would be very useful and trivial |
Had the same issue and it's indeed trivial. The maintainer though on the README mentions that no new functionality is going to be implemented, therefore I guess the only solution is either use hooks or another logger. |
I based my idea on this and came up with a version that doesn't use RotateFileHook, |
Hello,
I wanted to set TextFormatter for stdout and JsonFormatter for file output, is that possible?
Here's my logger initialization
I am logging into Stdout and log file
The problem is that it output color chars are getting inside the file as well..
�[36mINFO�[0m[04 Jul 18 17:12 EEST] Initializing
The text was updated successfully, but these errors were encountered: