Last active
December 2, 2022 07:14
-
-
Save st4lk/6725777 to your computer and use it in GitHub Desktop.
Django logging settings
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
# Logging settings for django projects, works with django 1.5+ | |
# If DEBUG=True, all logs (including django logs) will be | |
# written to console and to debug_file. | |
# If DEBUG=False, logs with level INFO or higher will be | |
# saved to production_file. | |
# Logging usage: | |
# import logging | |
# logger = logging.getLogger(__name__) | |
# logger.info("Log this message") | |
LOGGING = { | |
'version': 1, | |
'disable_existing_loggers': False, | |
'filters': { | |
'require_debug_false': { | |
'()': 'django.utils.log.RequireDebugFalse' | |
}, | |
'require_debug_true': { | |
'()': 'django.utils.log.RequireDebugTrue' | |
} | |
}, | |
'formatters': { | |
'main_formatter': { | |
'format': '%(levelname)s:%(name)s: %(message)s ' | |
'(%(asctime)s; %(filename)s:%(lineno)d)', | |
'datefmt': "%Y-%m-%d %H:%M:%S", | |
}, | |
}, | |
'handlers': { | |
'mail_admins': { | |
'level': 'ERROR', | |
'filters': ['require_debug_false'], | |
'class': 'django.utils.log.AdminEmailHandler' | |
}, | |
'console': { | |
'level': 'DEBUG', | |
'filters': ['require_debug_true'], | |
'class': 'logging.StreamHandler', | |
'formatter': 'main_formatter', | |
}, | |
'production_file': { | |
'level': 'INFO', | |
'class': 'logging.handlers.RotatingFileHandler', | |
'filename': 'logs/main.log', | |
'maxBytes': 1024 * 1024 * 5, # 5 MB | |
'backupCount': 7, | |
'formatter': 'main_formatter', | |
'filters': ['require_debug_false'], | |
}, | |
'debug_file': { | |
'level': 'DEBUG', | |
'class': 'logging.handlers.RotatingFileHandler', | |
'filename': 'logs/main_debug.log', | |
'maxBytes': 1024 * 1024 * 5, # 5 MB | |
'backupCount': 7, | |
'formatter': 'main_formatter', | |
'filters': ['require_debug_true'], | |
}, | |
'null': { | |
"class": 'django.utils.log.NullHandler', | |
} | |
}, | |
'loggers': { | |
'django.request': { | |
'handlers': ['mail_admins', 'console'], | |
'level': 'ERROR', | |
'propagate': True, | |
}, | |
'django': { | |
'handlers': ['null', ], | |
}, | |
'py.warnings': { | |
'handlers': ['null', ], | |
}, | |
'': { | |
'handlers': ['console', 'production_file', 'debug_file'], | |
'level': "DEBUG", | |
}, | |
} | |
} |
@gonza8888 you can use logging.NullHandler
https://stackoverflow.com/questions/34348360/cannot-resolve-django-utils-log-nullhandler-in-django-1-9
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For django 1.10+, null handler should be: "class": 'logging.NullHandler',
And another improvement is changing filename to:
'filename': os.path.join(DJANGO_ROOT, 'logs/myfile.log'),
So it is always placed on same folder.