Skip to content

Instantly share code, notes, and snippets.

@AlonsoMackenlly
Last active December 2, 2023 17:47
Show Gist options
  • Save AlonsoMackenlly/f14868dd6fa6767d80d0c30064ba3fb5 to your computer and use it in GitHub Desktop.
Save AlonsoMackenlly/f14868dd6fa6767d80d0c30064ba3fb5 to your computer and use it in GitHub Desktop.
Loguru with Django
class InterceptHandler(logging.Handler):
def emit(self, record):
# Get corresponding Loguru level if it exists
try:
level = logger.level(record.levelname).name
except ValueError:
level = record.levelno
# Find caller from where originated the logged message
frame, depth = logging.currentframe(), 2
while frame.f_code.co_filename == logging.__file__:
frame = frame.f_back
depth += 1
logger.opt(depth=depth, exception=record.exc_info).log(level, record.getMessage())
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'intercept': {
'()': InterceptHandler,
'level': 0,
},
},
'loggers': {
'': {
'handlers': ['intercept'],
'level': "DEBUG",
'propagate': True,
},
}
}
logger.remove()
logger.add(sys.stdout, level="DEBUG", backtrace=True)
logger.add("debug.log", level="DEBUG", rotation="30 MB", backtrace=True, retention="7 days")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment