This is the first day of my participation in Gwen Challenge

Why not Print()?

  • Invisible after deployment: Print() is only printed to the CONSOLE of the IDE, not invisible after the project is deployed and running.
  • High performance drain: Print() is particularly performance drain and should be deleted or commented out during the actual deployment run time.

Why log output?

Information can be saved to a file for easy traceability.

How does Django use the logger?

The options are first configured in the configuration file (setting.py) and then invoked where needed.

Django log Levels

  • Django uses Python’s built-in logging module for logging.
  • DEBUG < INFO < WARNING < ERROR < CRITICAL (Log information is reduced in sequence)
    • DEBUG: Indicates low-level system information for debugging purposes
    • INFO: indicates general system information
    • WARNING: Information that describes a major problem
    • ERROR: Describes the major problem that occurred
    • CRITICAL: Describes the CRITICAL problem information
  • After a log level is specified, only logs whose value is greater than or equal to the specified log level are recorded.

How do YOU configure log inputs in Django?

Add the following code to the configuration file (setting.py) :

LOGGING = {
    'version': 1.# version
    'disable_existing_loggers': False.# Whether to disable an existing logger
    'formatters': {  Format of log information display
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(lineno)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(module)s %(lineno)d %(message)s'}},'filters': {  # filter
        'require_debug_true': {  # Django only outputs logs in debug mode
            '()': 'django.utils.log.RequireDebugTrue',}},'handlers': {  # Log handling methods
        'console': {  Output logs to the terminal
            'level': 'INFO'.# output level = INFO
            'filters': ['require_debug_true'].'class': 'logging.StreamHandler'.'formatter': 'simple'
        },
        'file': {  Output logs to a file
            'level': 'INFO'.# output level = INFO
            'class': 'logging.handlers.RotatingFileHandler'.'filename': "/logs/msg.log"),  # Log file location
            'maxBytes': 300 * 1024 * 1024.Log file size (300*1024*1024 = 300MB)
            'backupCount': 10.The number of log files will be automatically backed up if the number exceeds the specified maximum. The maximum number of backup files is 10.
            'formatter': 'verbose' # Log output format: uses 'verbose' as defined earlier}},'loggers': {  # logger
        'django': {  # define a logger named Django
            'handlers': ['console'.'file'].Log to terminal and file at the same time
            'propagate': True.# Whether to continue sending log information
            'level': 'INFO'.The lowest log level received by the logger}}},# reference: https://docs.djangoproject.com/en/3.2/topics/logging/
Copy the code

How do I call the log input in Django?

Import and call where needed

import logging # import module

logger = logging.getLogger('django') Use a logger named "Django" as defined in the configuration file

def index(request) :
    logger.info("test") # Call logger.info() to output info-level logs.Copy the code