Skip to content

Commit

Permalink
- Added reload option to logger_setup.
Browse files Browse the repository at this point in the history
- add_file_handler now returns the handler object.
- Added missing long-description-content-type to setup.cfg.
  • Loading branch information
mauvilsa committed May 3, 2021
1 parent 932c24e commit 8630075
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
22 changes: 14 additions & 8 deletions reconplogger.py
Expand Up @@ -87,7 +87,7 @@ def _request_patch(slf, *args, **kwargs):
configs_loaded = set()


def load_config(cfg: Optional[Union[str, dict]] = None):
def load_config(cfg: Optional[Union[str, dict]] = None, reload: bool = False):
"""Loads a logging configuration from path or environment variable or dictionary object.
Args:
Expand Down Expand Up @@ -121,7 +121,7 @@ def load_config(cfg: Optional[Union[str, dict]] = None):
cfg_dict['disable_existing_loggers'] = False

cfg_hash = yaml.safe_dump(cfg_dict).__hash__()
if cfg_hash not in configs_loaded:
if reload or cfg_hash not in configs_loaded:
logging.config.dictConfig(cfg_dict)
configs_loaded.add(cfg_hash)

Expand Down Expand Up @@ -162,22 +162,26 @@ def add_file_handler(
file_path: str,
format: str = reconplogger_format,
level: Optional[str] = 'DEBUG',
):
) -> logging.FileHandler:
"""Adds a file handler to a given logger.
Args:
logger: Logger object where to add the file handler.
file_path: Path to log file for handler.
format: Format for logging.
level: Logging level for the handler.
Returns:
The handler object which could be used for removeHandler.
"""
fileHandler = logging.FileHandler(file_path)
fileHandler.setFormatter(logging.Formatter(format))
file_handler = logging.FileHandler(file_path)
file_handler.setFormatter(logging.Formatter(format))
if level is not None:
if level not in logging_levels:
raise ValueError('Invalid logging level: "'+str(level)+'".')
fileHandler.setLevel(logging_levels[level])
logger.addHandler(fileHandler)
file_handler.setLevel(logging_levels[level])
logger.addHandler(file_handler)
return file_handler


def test_logger(logger: logging.Logger):
Expand Down Expand Up @@ -209,6 +213,7 @@ def logger_setup(
config: Optional[str] = None,
level: Optional[str] = None,
env_prefix: str = 'LOGGER',
reload: bool = False,
parent: Optional[logging.Logger] = None,
init_messages: bool = False,
) -> logging.Logger:
Expand All @@ -219,6 +224,7 @@ def logger_setup(
config: Configuration string or path to configuration file or configuration file via environment variable.
level: Optional logging level that overrides one in config.
env_prefix: Environment variable names prefix for overriding logger configuration.
reload: Whether to reload logging configuration overriding any previous settings.
parent: Set for logging delegation to the parent.
init_messages: Whether to log init and test messages.
Expand All @@ -232,7 +238,7 @@ def logger_setup(
env_level = env_prefix + '_LEVEL'

# Configure logging
load_config(os.getenv(env_cfg, config))
load_config(os.getenv(env_cfg, config), reload=reload)

# Get logger
logger = get_logger(os.getenv(env_name, logger_name))
Expand Down
3 changes: 1 addition & 2 deletions reconplogger_tests.py
Expand Up @@ -257,9 +257,8 @@ def test_add_file_handler(self):
self.assertTrue(any([error_msg in line for line in open(log_file).readlines()]))
self.assertTrue(any([debug_msg in line for line in open(log_file).readlines()]))

reconplogger.configs_loaded = set()
log_file = os.path.join(tmpdir, 'file2.log')
logger = reconplogger.logger_setup(logger_name='plain_logger', level='DEBUG')
logger = reconplogger.logger_setup(logger_name='plain_logger', level='DEBUG', reload=True)
reconplogger.add_file_handler(logger, file_path=log_file, level='ERROR')
self.assertEqual(logger.handlers[0].level, logging.DEBUG)
self.assertEqual(logger.handlers[1].level, logging.ERROR)
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Expand Up @@ -34,6 +34,7 @@ name = reconplogger
version = 4.8.1
description = omni:us python logging package
long-description = file: README.rst
long-description-content-type = text/x-rst
author = Nischal Padmanabha, Mauricio Villegas
author-email = nischal@omnius.com
license = MIT
Expand Down

0 comments on commit 8630075

Please sign in to comment.