Last active
October 15, 2021 18:21
-
-
Save rafaelhenrique/f1b9db898f7e78fa002d409819789ca1 to your computer and use it in GitHub Desktop.
Basic logging
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
import logging | |
logging.basicConfig(filename='debug.log', level=logging.DEBUG, | |
format='%(asctime)s %(levelname)s %(funcName)s => %(message)s') | |
def my_beautiful_function(): | |
logging.debug("Enter on function!") | |
print("Hello!") | |
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
# Reference: https://realpython.com/python-logging/ | |
import logging | |
# Create a custom logger | |
logger = logging.getLogger(__name__) | |
# Create handlers | |
c_handler = logging.StreamHandler() | |
f_handler = logging.FileHandler('file.log') | |
c_handler.setLevel(logging.WARNING) | |
f_handler.setLevel(logging.ERROR) | |
# Create formatters and add it to handlers | |
c_format = logging.Formatter('%(name)s - %(levelname)s - %(message)s') | |
f_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | |
c_handler.setFormatter(c_format) | |
f_handler.setFormatter(f_format) | |
# Add handlers to the logger | |
logger.addHandler(c_handler) | |
logger.addHandler(f_handler) | |
logger.warning('This is a warning') | |
logger.error('This is an error') |
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
import logging | |
import logging.config | |
from .config import settings | |
# Complete log: | |
# | |
# JSON_FORMAT = ( | |
# "%(asctime)s %(levelname)s %(pathname)s %(lineno)s %(process)s " | |
# "%(processName)s %(thread)s %(threadName)s %(name)s %(message)s" | |
# ) | |
JSON_FORMAT = "%(name)s %(message)s" | |
DEFAULT_LOGGING_CONF = { | |
"version": 1, | |
"disable_existing_loggers": False, | |
"formatters": { | |
"default": {"format": "%(asctime)s %(module)s %(message)s"}, | |
"json": {"()": "pythonjsonlogger.jsonlogger.JsonFormatter", "format": JSON_FORMAT}, | |
}, | |
"filters": { | |
"logger_filter": { | |
"()": "belogging.filters.LoggerFilter", | |
}, | |
}, | |
"handlers": { | |
"default": { | |
"level": settings.LOG_LEVEL, | |
"class": "logging.StreamHandler", | |
"formatter": "json", | |
"filters": ["logger_filter"], | |
}, | |
"null": { | |
"class": "logging.NullHandler", | |
}, | |
}, | |
"root": { | |
"handlers": ["default"], | |
"level": "DEBUG", | |
}, | |
"loggers": {}, | |
} | |
logging.config.dictConfig(DEFAULT_LOGGING_CONF) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment