1. Documentation
Do not do development must be inseparable from the log, the following is my work in writing Django project commonly used logging configuration.
# Log configuration BASE_LOG_DIR = (BASE_DIR, "log") LOGGING = { 'version': 1, # Reserved words 'disable_existing_loggers': False, # Whether to disable an existing log instance 'formatters': { # Define the format of the log 'standard': { 'format': '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' '[%(levelname)s][%(message)s]' }, 'simple': { 'format': '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s' }, 'collect': { 'format': '%(message)s' } }, 'filters': { # Define filters for logs 'require_debug_true': { '()': '', }, }, 'handlers': { # Log handlers 'console': { 'level': 'DEBUG', 'filters': ['require_debug_true'], # Print logs on screen only if Django debug is True 'class': '', 'formatter': 'simple' }, 'SF': { 'level': 'INFO', 'class': '', # Saved to file, automatically sliced based on file size 'filename': (BASE_LOG_DIR, "xxx_info.log"), # Log files 'maxBytes': 1024 * 1024 * 500, # Log size 50M (preferably no more than 1G) 'backupCount': 3, # of backups is 3 --> .1 --> .2 --> .3 'formatter': 'standard', 'encoding': 'utf-8', # Encoding format for documentation records }, 'TF': { 'level': 'INFO', 'class': '', # Saved to file, automatically cut according to time 'filename': (BASE_LOG_DIR, "xxx_info.log"), # Log files 'backupCount': 3, # of backups is 3 --> .2018-08-23_00-00-00 --> .2018-08-24_00-00-00 --> ... 'when': 'D', # everything every day, optional values are S/second M/minute H/hour D/day W0-W6/week (0=Monday) midnight/if no time is specified it defaults to midnight 'formatter': 'standard', 'encoding': 'utf-8', }, 'error': { 'level': 'ERROR', 'class': '', # Save to file, auto cut 'filename': (BASE_LOG_DIR, "xxx_err.log"), # Log files 'maxBytes': 1024 * 1024 * 5, # Log size 50M 'backupCount': 5, 'formatter': 'standard', 'encoding': 'utf-8', }, 'collect': { 'level': 'INFO', 'class': '', # Save to file, auto cut 'filename': (BASE_LOG_DIR, "xxx_collect.log"), 'maxBytes': 1024 * 1024 * 50, # Log size 50M 'backupCount': 5, 'formatter': 'collect', 'encoding': "utf-8" } }, 'loggers': { # Examples of logs '': { # The default logger applies the following configuration 'handlers': ['SF', 'console', 'error'], # You can remove 'console' when it's online. 'level': 'DEBUG', 'propagate': True, # Whether to pass logging information to a higher-level logger instance }, 'collect': { # The logger named 'collect' is also handled separately 'handlers': ['console', 'collect'], 'level': 'INFO', } }, }
2. Create a log folder in the Django root directory.
3. Use of logging
With the logging configuration, we will try not to use print statements to print information for bug debugging in future projects. More professionally, we will use the logger object to save and print error messages.
Example.
import logging # Instantiate the logging object and use the name of the current file as the name of the logger instance. logger = (__name__) # Generate a log instance called collect logger_c = ('collect') class Foo(object): def test(self, data): (data) # print data try: ... except Exception as e: (str(e)) # Save and print the error message
This is the whole content of this article.