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

Lumberjack hook for Logrus #679

Closed
fallais opened this issue Dec 6, 2017 · 10 comments
Closed

Lumberjack hook for Logrus #679

fallais opened this issue Dec 6, 2017 · 10 comments

Comments

@fallais
Copy link

fallais commented Dec 6, 2017

Hello,

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

@renathoaz
Copy link

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)

@fallais
Copy link
Author

fallais commented Jan 12, 2018

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.

@renathoaz
Copy link

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

@pieterlouw
Copy link

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

@phoenix147
Copy link

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)

@jpiechowka
Copy link

jpiechowka commented Aug 6, 2018

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

@dgsb
Copy link
Collaborator

dgsb commented Sep 16, 2018

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.

@yashi-gupta-mmt
Copy link

yashi-gupta-mmt commented Mar 24, 2021

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

@github-actions
Copy link

This issue is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale label Sep 13, 2021
@github-actions
Copy link

This issue was closed because it has been inactive for 14 days since being marked as stale.

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

No branches or pull requests

7 participants