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

Implement a setup_logger function #108

Open
VincentVerelst opened this issue May 27, 2024 · 2 comments
Open

Implement a setup_logger function #108

VincentVerelst opened this issue May 27, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@VincentVerelst
Copy link
Collaborator

Currently, a user has to manually set up a logger function. For example:

def setup_logger(level=logging.INFO) -> None:
    """Setup the logger from the openeo_gfmap package to the assigned level."""
    global pipeline_log
    pipeline_log = logging.getLogger("pipeline_sar")

    pipeline_log.setLevel(level)

    stream_handler = logging.StreamHandler()
    pipeline_log.addHandler(stream_handler)

    formatter = logging.Formatter("%(asctime)s|%(name)s|%(levelname)s:  %(message)s")
    stream_handler.setFormatter(formatter)

    # Exclude the other loggers from other libraries
    class ManagerLoggerFilter(logging.Filter):
        """Filter to only accept the OpenEO-GFMAP manager logs."""

        def filter(self, record):
            return record.name in [pipeline_log.name]

    stream_handler.addFilter(ManagerLoggerFilter())

We could provide a basic setup_logger function like this in GFMap, such that the user only needs to specify the loglevel.

@VincentVerelst VincentVerelst added the enhancement New feature or request label May 27, 2024
@soxofaan
Copy link
Member

I might be missing some context, but some quick feedback

global pipeline_log

It shouldn't be necessary to use globals here.
logging.getLogger("pipeline_sar") will return the same object even if called from different places, so no need to pass things around with globals.

That ManagerLoggerFilter pattern looks a bit dangerous because it will suppress everything (warnings and errors) from other components, which might hide actual problems.

@soxofaan
Copy link
Member

I think something like the following has the same intent, but will not hide warnings/errors:

import logging

logging.basicConfig(level=logging.INFO)  # you can also specify a custom format here
# Set default log level to warning ...
logging.getLogger().setLevel(logging.WARNING)
# ... but use INFO level for our own logs
logging.getLogger("pipeline_sar").setLevel(logging.INFO)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants