Junior — Middle
Can you name popular Python libraries used for advanced logging and their configuration?
sobes.tech AI
Answer from AI
For advanced logging and configuration in Python, the following libraries are often used:
-
logging — the built-in Python module for logging, supporting different log levels, handlers, formatting, and filters.
-
loguru — a convenient and powerful library with a simple API that simplifies logging setup and adds useful features like file rotation and colored output.
-
structlog — a library for structured logging, helping to create logs in JSON format or other structured forms suitable for analysis.
-
python-json-logger — an extension for the logging module that allows logs to be output in JSON format.
Example of basic logging setup with file rotation:
import logging
from logging.handlers import RotatingFileHandler
logger = logging.getLogger('my_logger')
logger.setLevel(logging.DEBUG)
handler = RotatingFileHandler('app.log', maxBytes=1000000, backupCount=3)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.info('Sample log message')