Open-EO / openeo-gfmap

Generic framework for EO mapping applications building on openEO
Apache License 2.0
4 stars 0 forks source link

Implement a setup_logger function #108

Open VincentVerelst opened 1 month ago

VincentVerelst commented 1 month ago

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.

soxofaan commented 1 month ago

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 commented 1 month ago

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)