Logrus: Logrus Formatter per output?

Created on 10 Jul 2018  路  5Comments  路  Source: sirupsen/logrus

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..

INFO[04 Jul 18 17:12 EEST] Initializing

Most helpful comment

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

All 5 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?

@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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SergeiVasilenko picture SergeiVasilenko  路  3Comments

Integralist picture Integralist  路  3Comments

anotherGoogleFan picture anotherGoogleFan  路  3Comments

piotrkowalczuk picture piotrkowalczuk  路  4Comments

zhangdaoling picture zhangdaoling  路  4Comments