Skip to content
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

Closed
UnAfraid opened this issue Jul 10, 2018 · 8 comments
Closed

Logrus Formatter per output? #784

UnAfraid opened this issue Jul 10, 2018 · 8 comments

Comments

@UnAfraid
Copy link

UnAfraid commented Jul 10, 2018

Hello,

I wanted to set TextFormatter for stdout and JsonFormatter for file output, is that possible?

Here's my logger initialization

func initLogger() {
	if debug {
		logrus.SetLevel(logrus.DebugLevel)
	}

	logrus.SetFormatter(&logrus.TextFormatter{
		ForceColors:     true, // Seems like automatic color detection doesn't work on windows terminals
		FullTimestamp:   true,
		TimestampFormat: time.RFC822,
	})

	multiWriter := io.MultiWriter(colorable.NewColorableStdout(), &lumberjack.Logger{
		Filename:   "logs/console.log",
		MaxSize:    50, // megabytes
		MaxBackups: 3,
		MaxAge:     28,   //days
		Compress:   true, // disabled by default
	})

	logrus.SetOutput(multiWriter)
}

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

@UnAfraid
Copy link
Author

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)
}

@david0u0
Copy link

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

@utdrmac
Copy link

utdrmac commented Oct 9, 2020

@UnAfraid How do you have two different outputs/formatters using vanilla logrus?

@UnAfraid
Copy link
Author

@UnAfraid How do you have two different outputs/formatters using vanilla logrus?

By using hook, either existing one or implement your own

@stevekuznetsov
Copy link

Agree this should be re-opened, having a trivial way to configure a formatter for the built-in hooks is a must.

@tatulea
Copy link

tatulea commented May 18, 2021

Yes, it would be very useful and trivial

@dimtass
Copy link

dimtass commented Jun 22, 2021

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.

@mem-
Copy link

mem- commented Oct 19, 2022

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",
  .
  .
  .
  .
}

I based my idea on this and came up with a version that doesn't use RotateFileHook,
see #894 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants