BritishGeologicalSurvey / etlhelper

ETL Helper is a Python ETL library to simplify data transfer into and out of databases.
https://britishgeologicalsurvey.github.io/etlhelper/
GNU Lesser General Public License v3.0
104 stars 25 forks source link

Refactor logging to add `log_to_stderr` function #125

Closed volcan01010 closed 1 year ago

volcan01010 commented 2 years ago

Summary

As an ETL Helper user I want a simple way to turn on logging that doesn't add a handler to the ETL Helper logger by default so that I don't get multiple messages.

Description

The current ETL Helper logging setup is configured here: https://github.com/BritishGeologicalSurvey/etlhelper/blob/6cd64fd0dfbf5a40d80437992fae8b69fcbbe05e/etlhelper/__init__.py#L33

The configuration adds a logger with a custom handler that handles DEBUG messages in custom way so that SQL queries and other details that they include are easy to read. By default it is set to WARNING so that it doesn't emit many messages. Logging can be turned on by taking a reference to the logger and setting the level to INFO or below. See: https://github.com/BritishGeologicalSurvey/etlhelper/blob/6cd64fd0dfbf5a40d80437992fae8b69fcbbe05e/README.md?plain=1#L567

However, adding a logger to a library is considered bad practice. It would be better if we didn't do this by default. But it would still be useful to be able to easily configure this logger, especially for novice Python users. A function to add the handler would solve this problem. e.g.

def log_to_stderr(level=logging.INFO):
    """Log ETL Helper messages to stderr at level."""
    logger = logging.getLogger("etlhelper")
    # Add handler
    ...
    # Set level
    logger.setLevel(level)

Users would then just add the following to the top of their scripts:

import etlhelper

etlhelper.log_to_stderr()

Acceptance criteria

volcan01010 commented 1 year ago

Slight change:

def log_to_console(level=logging.INFO, output=sys.STDERR):
   ...

This would allow users to easily choose where the logging goes.

volcan01010 commented 1 year ago

Done in for_v1.