-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add example that shows how to log to a file #101
Conversation
@@ -54,6 +55,35 @@ func Example() { | |||
// {"msg":"Oh no!","level":"error","ts":0,"fields":{"user":"jane@test.com","visits":42}} | |||
} | |||
|
|||
func Example_fileOutput() { | |||
// Create a temporary file to output logs to. | |||
f, err := ioutil.TempFile("", "log") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't you need to delete the file after you're done?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, added a defer os.Remove(...)
87ea222
to
d522221
Compare
LGTM |
|
||
logger.Info("This is an info log.", zap.Int("foo", 42)) | ||
|
||
// Sync the file so logs are written to disk, and print the file contents. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're including this in an example, can we call out that zap already syncs when logging at PanicLevel
and FatalLevel
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, added
d522221
to
747abfb
Compare
|
} | ||
defer os.Remove(f.Name()) | ||
|
||
logger := zap.NewJSON( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
zap do not have NewJSON method?
what should I do?
@MonsieurHorse let's discuss this on the issue you opened. This PR is very old, and APIs have changed quite a bit. |
Fixes #100