-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Comments
Use f, err := os.OpenFile(filename, os.O_WRONLY | os.O_CREATE, 0755)
if err != nil {
# handle
}
logrus.SetOutput(f) |
Thanks much appreciated!
|
If you're using the text formatter you may have to pass |
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. |
I agree but where I currently am we log to both /var/log/app.log & then
|
Hi @sirupsen , how to make logs both print on screen and save to local file? |
@wangkirin take a look at the LFS hook: https://github.com/rifflock/lfshook |
@sirupsen When I'm trying to discard the logs (for testing purpose) using
It does not work and I still see the error on Any idea ? |
This is works for me:
All log saved into |
Do we not have to register an exit handler to make sure the file is closed, as well as explicitly call
|
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) |
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. |
@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. |
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
The text was updated successfully, but these errors were encountered: