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

write to file #156

Closed
anth1y opened this issue Mar 20, 2015 · 14 comments
Closed

write to file #156

anth1y opened this issue Mar 20, 2015 · 14 comments

Comments

@anth1y
Copy link

anth1y commented Mar 20, 2015

Excuse the newb question but I truly enjoy using logrus and we are about to ship our product to production but I have not yet figured out how to write to file. for example how do i write to /var/log/$app using logrus.

thanks in advance

@sirupsen
Copy link
Owner

Use log.SetOutput() with a file handler, anything that implements io.Writer is good.

f, err := os.OpenFile(filename, os.O_WRONLY | os.O_CREATE, 0755)
if err != nil {
    # handle
}
logrus.SetOutput(f)

@anth1y
Copy link
Author

anth1y commented Mar 21, 2015

Thanks much appreciated!
On Mar 20, 2015 3:04 PM, "Simon Eskildsen" notifications@github.com wrote:

Closed #156 #156.


Reply to this email directly or view it on GitHub
#156 (comment).

@sirupsen
Copy link
Owner

If you're using the text formatter you may have to pass DisableColors (see the docs) if you're running from a TTY.

@sirupsen
Copy link
Owner

Sorry that this is a little confusing, it could probably be documented better. It's much more common to log to stdout/stderr in my experience, and have something pick it up.

@anth1y
Copy link
Author

anth1y commented Mar 21, 2015

I agree but where I currently am we log to both /var/log/app.log & then
logstash picks it up from there. But I agree ;).
On Mar 20, 2015 5:48 PM, "Simon Eskildsen" notifications@github.com wrote:

Sorry that this is a little confusing, it could probably be documented
better. It's much more common to log to stdout/stderr in my experience, and
have something pick it up.


Reply to this email directly or view it on GitHub
#156 (comment).

@sirupsen
Copy link
Owner

:) enjoy logrus

emoji

@wangkirin
Copy link

Hi @sirupsen , how to make logs both print on screen and save to local file?

@krak3n
Copy link

krak3n commented Jul 20, 2016

@wangkirin take a look at the LFS hook: https://github.com/rifflock/lfshook

@meetme2meat
Copy link

@sirupsen When I'm trying to discard the logs (for testing purpose) using

logrus.SetOutput(ioutil.Discard)

It does not work and I still see the error on STDOUT.

Any idea ?

@zeroc0d3
Copy link

zeroc0d3 commented Oct 3, 2018

This is works for me:

package main

import (
    "os"
    "github.com/Sirupsen/logrus"
)

const LOG_FILE = "/tmp/logrus.log"

func main() {
        // create the logger
	logger := logrus.New()
	// with Json Formatter
        logger.Formatter = &logrus.JSONFormatter{}
	logger.SetOutput(os.Stdout)

	file, err := os.OpenFile(LOG_FILE, os.O_WRONLY | os.O_CREATE | os.O_APPEND, 0755)
	if err != nil {
		logger.Fatal(err)
	}
	defer file.Close()
	logger.SetOutput(file)
}

All log saved into LOG_FILE

@MMulthaupt
Copy link

MMulthaupt commented Jan 25, 2019

Do we not have to register an exit handler to make sure the file is closed, as well as explicitly call logrus.Exit(0) to make sure it is called at the end of main()?

var logFile *os.File

func main() {
    logFile, _ = os.OpenFile("app_<timestamp_here>.log", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
    logrus.SetOutput(io.MultiWriter(os.Stderr, logFile))
    logrus.RegisterExitHandler(closeLogFile)
    // ...
    logrus.Exit(0)
}

func closeLogFile() {
    if logFile != nil {
        logFile.Close()
    }
}

@blaskovicz
Copy link

To simplify @MMulthaupt 's usage, I did the following (which includes generating a new timestamp per run):

        package main

        import (
          "time"
          "os"
          "path/filepath"
           log "github.com/sirupsen/logrus"
        )

        cwd, err := os.Getwd()
	if err != nil {
		log.Fatalf("Failed to determine working directory: %s", err)
	}
        runID := time.Now().Format("run-2006-01-02-15-04-05")
	logLocation := filepath.Join(cwd, runID + ".log")
	logFile, err := os.OpenFile(logLocation, os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		log.Fatalf("Failed to open log file %s for output: %s", logLocation, err)
	}
	log.SetOutput(io.MultiWriter(os.Stderr, logFile))
	log.RegisterExitHandler(func() {
		if logFile == nil {
			return
		}
		logFile.Close()
	})
	log.WithFields(log.Fields{"at": "start", "log-location": logLocation}).Info()
        // perform actions
	log.Exit(0)

@Farit-Biktimirov
Copy link

Did someone faced with a problem caused by dangling file descriptor while using logrotate? logrus doesn't frees up file descriptor and thereby continues write logs to an archive file maded by logrotate.

@freeformz
Copy link
Collaborator

freeformz commented Sep 18, 2019

@Farit-Biktimirov You would have to have logrotate HUP the process and register a signal handler to respond to that signal open a new file, call SetOutput to start using the new file, and then close the old file.

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

10 participants