Skip to content

Commit

Permalink
Enable logger-specific handler configuration via env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
nir0s committed Mar 9, 2018
1 parent 1629225 commit 962ced9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -458,14 +458,15 @@ The idea behind this is three-fold:

### Using Environment Variables to configure logging handlers

NOTE: This is WIP, so things may break / be broken. To truly be able to use this feature, Wryte will have to support logger-name-based env vars (e.g. `WRYTE_HANDLERS_logger_name_*`).

NOTE: DO NOT use this feature if you have multiple loggers in the same service unless you explicitly intend to have all loggers log to all handlers configured.
NOTE: This is WIP, so things may break / be broken.

One of Wryte's goals is to provide a simple way to configure loggers. Much like Grafana and Fabio, Wryte aims to be completely env-var configurable.

On top of having two default `console` and `json` handlers which indicate the formatting and both log to stdout, you can utilize built-in and 3rd party handlers quite easily.

Below are the global env vars used to configure all loggers instantiated by Wryte.
If you want to apply a handler to a specific logger, use the `WRYTE_$LOGGER_NAME_HANDLERS_*` pattern instead (e.g. `WRYTE_WEBLOGGER_HANDLERS_FILE_ENABLED` - all uppercase).

#### File Handler

Wryte supports both the rotating and watching file handlers (on Windows, FileHandler replaces WatchingFileHandler if not rotating).
Expand Down
17 changes: 8 additions & 9 deletions wryte.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ def __init__(self,
If `hostname` isn't provided, it will be retrieved via socket.
"""
logger_name = name or __name__
self.logger_name = name or __name__

self.pretty = pretty
self.color = color
self.simple = simple

self._log = self._get_base(logger_name, hostname)
self.logger = self._logger(logger_name)
self._log = self._get_base(self.logger_name, hostname)
self.logger = self._logger(self.logger_name)

if not bare:
self._configure_handlers(level, jsonify)
Expand Down Expand Up @@ -250,12 +250,11 @@ def _enrich(self, message, level, objects, kwargs=None):

return log

@staticmethod
def _env(variable, logger=None, default=None):
if logger:
return os.getenv('WRYTE_{0}_{1}'.format(logger, variable), default)
else:
return os.getenv('WRYTE_{0}'.format(variable), default)
def _env(self, variable, default=None):
logger_env = os.getenv('WRYTE_{0}_{1}'.format(
self.logger_name.upper(), variable), default)
global_env = os.getenv('WRYTE_{0}'.format(variable), default)
return logger_env or global_env or None

def _configure_handlers(self, level, jsonify=False):
if not jsonify:
Expand Down

0 comments on commit 962ced9

Please sign in to comment.