-
Notifications
You must be signed in to change notification settings - Fork 33
Description
witchcraft-go-server has a notion of logging and is opinionated on output -- it decides on the io.Writer to use for writing logs based on factors such as configuration (useConsoleLog) and environment (isDocker or isJail), and when it performs file-based logging it does so using a lumberjack.Logger.
In some instances, a program may want to perform logging before the witchcraft.Server is created or started, but still wants to log in the same manner that the server would.
In order to do so, I propose the following:
- Define a new
FileWriterProviderinterface that, given a path, returns anio.Writerthat writes to that path - Define a
LumberjackFileWriterProviderimplementation that returns alumberjack.Loggeras it is currently defined innewDefaultLogOutput - Define a
CreateLogWriter(logOutputPath string, logToStdout bool, stdoutWriter io.Writer, fileWriterProvider FileWriterProvider) io.Writerfunction - Add a
WithFileWriterProvider(FileWriterProvider)function to the builder forwitchcraft.Server(if not specified, it will use theLumberjackFileWriterProviderby default)
These primitives provide a general way to do a few things:
- It makes it possible to configure the file output writer in code. Right now, a file writer is hard-coded to be a
lumberjack.Loggerwith a specific set of parameters. This change would allow file writers to be customized at a code level. - With the above, a client can provide an implementation of
FileWriterProviderthat caches results and returns the same instance of a*lumberjack.Loggerfor a given path- This means that multiple loggers can be instantiated with the same lumberjack logger, and will thus not need to worry about overwriting each other
- With the above, a client can use
CreateLogWriterto get theio.Writerthat is appropriate for their environment/setup (log tostdoutbased on environment, etc.)
The only real downside I see is that this does expose a vector for customizing the file-based log output location that did not previously exist. However, this configuration point is purely in code (not in end-user configuration), so I don't think it's a huge risk.
If we really wanted to be stringent about this we could modify the FileWriterProvider API to more targeted (for example, only allow it to return a lumberjack.Logger and always override portions of that config after it is returned), but I think that's overkill and is over-fitting the specific problem trying to be solved here.