Logrus: Lumberjack hook for Logrus

Created on 6 Dec 2017  路  8Comments  路  Source: sirupsen/logrus

Hello,

I propose a new hook for Lumberjack : https://github.com/fallais/logrus-lumberjack-hook

documentation help-wanted

Most helpful comment

I do it like this:

    ljack := &lumberjack.Logger{
        Filename:   "logs/misc.log",
        MaxSize:    100, // megabytes
        MaxBackups: 52,
        MaxAge:     7,     //days
        Compress:   false, // disabled by default
    }
    mWriter := io.MultiWriter(os.Stderr, ljack)
    log.SetOutput(mWriter)

All 8 comments

No need for that, You could only:

// Set the Lumberjack logger
lumberjackLogger := &lumberjack.Logger{
  Filename:   "/var/log/misc.log",
  MaxSize:    10,
  MaxBackups: 3,
  MaxAge:     3,
  LocalTime:  true,
}
logrus.SetOutput(lumberjackLogger)

Not really, because if you do this, you only log to a file. The hook allows to use logrus the default way and logging to a file.

Makes sense, Although we can accomplish it without setting the hook.

@RenathoAzevedo, can you supply an example of how this can be achieved without a hook?

I do it like this:

    ljack := &lumberjack.Logger{
        Filename:   "logs/misc.log",
        MaxSize:    100, // megabytes
        MaxBackups: 52,
        MaxAge:     7,     //days
        Compress:   false, // disabled by default
    }
    mWriter := io.MultiWriter(os.Stderr, ljack)
    log.SetOutput(mWriter)

@phoenix147 Thank you very much! Your solution works great. I have my logger configured as below:

package main

import (
    log "github.com/sirupsen/logrus"
    "time"
    "os"
    "github.com/natefinch/lumberjack"
    "runtime"
    "io"
)

const LogFilePath = "logs/misc.log"

func main() {
    // Setup logger
    lumberjackLogrotate := &lumberjack.Logger{
        Filename:   LogFilePath,
        MaxSize:    5,  // Max megabytes before log is rotated
        MaxBackups: 90, // Max number of old log files to keep
        MaxAge:     60, // Max number of days to retain log files
        Compress:   true,
    }

    log.SetFormatter(&log.TextFormatter{FullTimestamp: true, TimestampFormat: time.RFC1123Z})

    logMultiWriter := io.MultiWriter(os.Stdout, lumberjackLogrotate)
    log.SetOutput(logMultiWriter)

    log.WithFields(log.Fields{
        "Runtime Version": runtime.Version(),
        "Number of CPUs":  runtime.NumCPU(),
        "Arch":            runtime.GOARCH,
    }).Info("Application Initializing")
}

One can also set _ForceColors_ property to true (There is Jetbrains GoLand IDE plugin called ANSI Highlighter that can parse log levels and colors when displaying log files)
log.SetFormatter(&log.TextFormatter{ForceColors: true, FullTimestamp: true, TimestampFormat: time.RFC1123Z})

If someone could add these examples in the wiki or in the readme, that would be great.
We also have a page in the wiki which lists all the known hook which could be updated.

@renathoaz I tried your approach but it's not rotating the log file. It keeps writing logs to the same file. Here's my code.

lumberjackLogrotate := &lumberjack.Logger{
Filename: path,
MaxSize: 1,
}
ApiLog = logrus.New()
ApiLog.Formatter = &CustomFormatter{
prefix: 'stats',
}
ApiLog.SetOutput(lumberjackLogrotate)

Anyone facing the same problem?

Was this page helpful?
0 / 5 - 0 ratings