- A Basic Logging Tutorial in Python 3.12.0 documentation.
- The Logging Cookbook contains a number of recipes related to logging.
- The RotatingFileHandler class, located in the logging.handlers module, supports rotation of disk log files.
- LoggerAdapter instances are used to conveniently pass contextual information into logging calls.
- Logging in Python from GeeksforGeeks.
- Logging in Python from Real Python.
- A Comprehensive Guide to Logging in Python
Last active
October 16, 2023 17:23
-
-
Save JV-conseil/a3530f669bde8b6c53e0405c77b58184 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# author : JV-conseil | |
# credits : JV-conseil | |
# copyright : Copyright (c) 2019-2023 JV-conseil | |
# All rights reserved | |
# ==================================================== | |
import logging | |
def configure_logger_file(name: str = "default") -> object: | |
"""Freely adapted from | |
https://simpletutorials.com/c/python/zf02eyin/python-3-logging-using-dictconfig | |
""" | |
output = None | |
try: | |
logging.config.dictConfig( | |
{ | |
"version": 1, | |
"disable_existing_loggers": True, | |
"formatters": { | |
"default": { | |
"format": "{asctime}\t{levelname}\t{message}\t{value}\t{data}", | |
"style": "{", | |
}, | |
}, | |
"handlers": { | |
"file": { | |
"class": "logging.handlers.RotatingFileHandler", | |
"filename": f"./logs/{name}.log", | |
"formatter": "default", | |
"level": "DEBUG", | |
"maxBytes": 1024, | |
"backupCount": 3, | |
}, | |
}, | |
"loggers": { | |
name: { | |
"handlers": ["file"], | |
"propagate": False, | |
} | |
}, | |
} | |
) | |
output = logging.getLogger(name) | |
except Exception as e: | |
print(e) | |
return output | |
# Create a custom logger | |
logger_file = configure_logger_file("default") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment