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

Example of setting output file #294

Closed
davoudrafati opened this issue Feb 15, 2017 · 8 comments
Closed

Example of setting output file #294

davoudrafati opened this issue Feb 15, 2017 · 8 comments
Labels

Comments

@davoudrafati
Copy link

looks like API changed in rc1 version

so can you provide an example how can I write output to a file ?

@akshayjshah akshayjshah changed the title output file Example of setting output file Feb 15, 2017
@akshayjshah
Copy link
Contributor

akshayjshah commented Feb 15, 2017

Sure! For most use cases, you should be able to use zap.Config to build a logger. (If you manage your code's configuration in JSON, TOML, YAML, or some similar format, you can also put zap's config in there.)

func NewLogger() (*zap.Logger, error) {
  cfg := zap.NewProductionConfig()
  cfg.OutputPaths = []string{
    "/var/log/myproject/myproject.log",
  }
  return cfg.Build()
}

You can put multiple output paths in the config, and the logs will be written to all of them. Internally, we often want logs sent to both standard out and a file.

@MonsieurHorse
Copy link

MonsieurHorse commented May 5, 2017

@akshayjshah when i use this logger to log to file,why most of the logs are dropped?

@akshayjshah
Copy link
Contributor

@MonsieurHorse The production configuration samples logs by default; this prevents deadlocking your process when you suddenly encounter a lot of errors. You can configure this behavior by creating your own zap.Config struct instead of using the canned NewProduction struct.

Also, it's much easier for me to help you if you keep this discussion on the new issue you opened. It's difficult to respond to comments spread over a number of old issues and PRs.

@vinod023
Copy link

vinod023 commented Apr 8, 2019

Hi @akshayjshah can you explain in detail for log generation?

@abhinav
Copy link
Collaborator

abhinav commented Apr 8, 2019

Hi @akshayjshah can you explain in detail for log generation?

Hi @vinod023, it's not clear what you're asking. Also, as @akshayjshah
mentioned earlier, it would be much easier for us to answer new questions in
their own issues. This issue has been closed for nearly two years.

@aditya-opsverse
Copy link

aditya-opsverse commented Apr 4, 2022

Hey there!
If you want to write the log output to a file as well as the console/terminal, use this in zap.config;
OutputPaths: []string{"path_to_logfile", "stderr"},
This will write the logger output to the log file and to the terminal/console as well

@ankit-agarwal-E1304
Copy link

Will the write to file be safe if multiple applications write to the same file?

@akshayjshah
Copy link
Contributor

Will the write to file be safe if multiple applications write to the same file?

In general, no - it’s not safe for multiple processes to write to the same file without some mutual exclusion lock. It’s much safer to include the process name as a field in your log output, have each process write to a separate file, and merge the files during processing.

Also, please open new issues for these discussions. It’s hard for maintainers and the rest of the community to track new comments on years-old, closed issues.

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

No branches or pull requests

7 participants