Original post

I’m trying to figure out the best way to initialize the loggers for the packages in my go project. The logger I’m using is zap, and I am having some issues after a bit of refactoring. When I run my main function I read in env vars which determine log levels and dev/prod logging, but currently my other packages create their own loggers in their respective init() functions. This results in the other packages creating loggers with default settings before my main function has been able to load the logger settings from env.

Are there any common patterns for how this is usually done?

  • Should each package still have their own logger setups in init() somehow?

  • Should each package have a “SetLogger” func which is called by the main func after the environment variables have been read?

  • Should I use some kind of global mutex setup so that no packages create their loggers until after the env vars have been loaded and the lock is released?

  • None of the above?