bessagroup / f3dasm

Framework for Data-Driven Design & Analysis of Structures & Materials (F3DASM)
https://f3dasm.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
111 stars 29 forks source link

Implement a flexible logging framework for using f3dasm as a library or as an application #171

Open GuillaumeBroggi opened 12 months ago

GuillaumeBroggi commented 12 months ago

Motivation

@BernardoFerreira

The best default behavior for loggers is to hand over to the user the responsibility of defining log handlers (see Python documentation).

f3dasm may be used as an application or as a library. A default logging behavior is suitable when f3dasm is used as an application (current implementation). However, this is not suitable when used as a library because the logging configuration may overlap / be in conflict with the logging configuration wished by the user.

Description

Commit 3cd4d1e proposes a more flexible framework where the existence of a logger is checked when initializing f3dasm. Then, if the user wishes to define his own logging configuration, he simply has to get a logger with f3dasm name before importing f3dasm. Otherwise, f3dasm loads the default behavior from a config file avoiding the use of the basicConfig() method.

The default behavior is similar to the existing implementation: logs are printed to stderr when level is above INFO. In addition, logs ares saved to a "f3dasm.log" file.

Behavior

import logging

logging.basicConfig(level=logging.INFO)

logger = logging.getLogger("f3dsam")
logger.setLevel(logging.INFO)
logger.propagate = False
formatter = logging.Formatter(fmt="My f3dsam logger - %(message)s")
stderr = logging.StreamHandler()
stderr.setLevel(logging.INFO)
stderr.setFormatter(formatter)
logger.addHandler(stderr)

import f3dasm

logging.info("Test log.")
>>> My f3dsam logger - Imported f3dasm (version: 1.4.0)
>>> INFO:root:Test log.
import logging

logging.basicConfig(level=logging.INFO)

import f3dasm

logging.info("Test log.")
>>> 2023-09-21 14:38:16,097 - f3dsam - Imported f3dasm (version: 1.4.0)
>>> INFO:root:Test log.

Tasks