(Zap log framework, Go) Initialize log once and reuse from other Go file (Solved)

You can set up your logger in the main function and call https://godoc.org/go.uber.org/zap#ReplaceGlobals to use it as a default global logger.


Replacing the default go Global logger with zaps' implementation is possible, but discouraged.

Per their FAQ

Why include package-global loggers? Since so many other logging packages include a global logger, many applications aren't designed to accept loggers as explicit parameters. Changing function signatures is often a breaking change, so zap includes global loggers to simplify migration.

Avoid them where possible.

Depending on your needs, you can create a logger in main and pass it around or create a new logger in each package. I elected to create one in main and pass it around since I'm using an Atomic logger, which allows me to change log levels while my app is running via an API call. With a long history of using DI and consolidating code in general it does feel like code smell, but apparently it's significantly more performant for how zap works to pass it around over a singleton or global.