Logging to both stdout and file, with different format
CaledoniaProject opened this issue · comments
CaledoniaProject commented
I'm using logrus like the following:
var (
textFormatter = &logrus.TextFormatter{
TimestampFormat: "2006-01-02 15:04:05",
DisableColors: false,
ForceColors: true,
FullTimestamp: true,
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
funcName := strings.Split(f.Function, "/")
return "", fmt.Sprintf("[%s:%d %s]", path.Base(f.File), f.Line, funcName[len(funcName)-1])
},
}
)
func SetupLogger() {
logrus.SetOutput(os.Stdout)
logrus.SetLevel(logrus.InfoLevel)
logrus.SetReportCaller(true)
logrus.SetFormatter(textFormatter)
}
Is it possible to log to both file and stdout, but with different text formatter?
I've tried to use io.MultiWriter in SetOutput method, but the color in console is gone and textFormatter is not applied.
Kimmo Lehto commented
Yes, via AddHook
.
Not the cleanest implementation, but it's all done in this file here: https://github.com/k0sproject/k0sctl/blob/257028f69d93dae3039fe82ae78c878de2402b01/cmd/flags.go#L156
github-actions commented
This issue is stale because it has been open for 30 days with no activity.
CaledoniaProject commented
AddHook worked for me, closing this issue, thanks!