Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple handlers causing duplicate messages #43

Merged
merged 3 commits into from
Feb 15, 2022

Commits on Feb 15, 2022

  1. test_logger setup: allow kwargs to be forwarded to CumulusLogger cons…

    …tructor
    michaeljb authored and GitHub Actions committed Feb 15, 2022
    Configuration menu
    Copy the full SHA
    8c02640 View commit details
    Browse the repository at this point in the history
  2. add failing test, each logger should only have one handler

    michaeljb authored and GitHub Actions committed Feb 15, 2022
    Configuration menu
    Copy the full SHA
    226c2a1 View commit details
    Browse the repository at this point in the history
  3. CumulusLogger: only create new handler if one is not present

    This fixes the test added in the previous commit
    
    Before this change, multiple calls to `CumulusLogger(name)` would construct a
    `CumulusLogger` instance with the same underlying `Logger` instance, but each
    construction would also create and attach a new handler, resulting in duplicate
    messages, e.g.:
    
    ```
    > python
    >>> from cumulus_logger import CumulusLogger
    >>> logger = CumulusLogger('test')
    >>> logger.info('hello there')
    {"message": "hello there", "timestamp": "2021-12-21T16:18:09.315309", "level": "info"}
    >>>
    >>> logger2 = CumulusLogger('test')
    >>> logger2.info('hello there')
    {"message": "hello there", "timestamp": "2021-12-21T16:18:16.845070", "level": "info"}
    {"message": "hello there", "timestamp": "2021-12-21T16:18:16.845070", "level": "info"}
    >>>
    >>> logger.info('hello there')
    {"message": "hello there", "timestamp": "2021-12-21T16:18:18.165564", "level": "info"}
    {"message": "hello there", "timestamp": "2021-12-21T16:18:18.165564", "level": "info"}
    ```
    
    See also the screenshots on
    ghrcdaac/dmrpp-file-generator-docker#25
    
    With this change, a new handler is only constructed if the underlying `Logger`
    does not already have any handlers, preventing messages from being handled more
    than once:
    
    ```
    > python
    >>> from cumulus_logger import CumulusLogger
    >>> logger  = CumulusLogger('test')
    >>> logger.info('hello there')
    {"message": "hello there", "timestamp": "2021-12-21T16:33:56.735453", "level": "info"}
    >>>
    >>> logger2 = CumulusLogger('test')
    >>> logger2.info('hello there')
    {"message": "hello there", "timestamp": "2021-12-21T16:34:03.887972", "level": "info"}
    >>>
    >>> logger.info('hello there')
    {"message": "hello there", "timestamp": "2021-12-21T16:34:07.808415", "level": "info"}
    ```
    michaeljb authored and GitHub Actions committed Feb 15, 2022
    Configuration menu
    Copy the full SHA
    264682e View commit details
    Browse the repository at this point in the history