My Haskell webapp starts with a particular logging configuration. Once the webserver starts, the logging configuration cannot be changed. Here's what the logging config controls:
- logging output format: text logs (mostly used for local dev) vs JSON logs (mostly used for production env)
- whether to compute and emit aggregate metrics for each req/res, like number of SQL statements, number of downstream HTTP requests made, time spent in SQL, time spent in API calls, etc.
- log verbosity: in development env the actual SQL statements executed, detailed req/res logs of API calls made to downstream systems, etc. are logged. In production environment, these are skipped.
The naive way of writing a logging function would check, for every log statement, the logging config to determine (a) whether to output the log at all, (b) which format to use - text vs json, and (c) whether to update the aggregate metrics
Somehow this seems very inefficient to me.
Is it possible to efficiently do the following:
- upon application startup, depending upon the logging config, choose one of several implementations
- somehow inject this implementation into the application's monad (which might be an instance of
MonadLogger), such that the compiler is still able to do any optimizations that it would have done otherwise.