[PYTHON] (Minimal) usage of django logger

--To be exact, python logger --In django, write the settings in settings.py and use it. ――There are many other things you can do, but first of all, only the minimum

Write the following settings in settings.py

LOGGING = {
    'version': 1,   #If you do not set this, you will get angry
    'formatters': { #Specify the output format in string format
        'all': {    #For output format`all`Give it the name
            'format': '\t'.join([
                "[%(levelname)s]",
                "asctime:%(asctime)s",
                "module:%(module)s",
                "message:%(message)s",
                "process:%(process)d",
                "thread:%(thread)d",
            ])
        },
    },
    'handlers': {  #Setting where to put the log
        'file': {  #Give a name to the setting of where to put it`file`Is named
            'level': 'DEBUG',  #Meaning to handle logs of DEBUG or higher
            'class': 'logging.FileHandler',  #Specify the class to output the log
            'filename': os.path.join(BASE_DIR, 'django.log'),  #Where to put it out
            'formatter': 'all',  #Specify which output format to output by name
        },
        'console': { #Another setting for where to put it, this setting`console`Name
            'level': 'DEBUG',
            #This specifies the class that outputs to standard output
            'class': 'logging.StreamHandler', 
            'formatter': 'all'
        },
    },
    'loggers': {  #Set what kind of logger you have
        'command': {  #Define a logger named command
            'handlers': ['file', 'console'],  #File mentioned above,Output with console settings
            'level': 'DEBUG',
        },
    },
}

Create and call such a function

def info(msg):
    logger = logging.getLogger('command')
    logger.info(msg)

info("hello!")

Output to django.log and standard output in the following format

[INFO]	asctime:2015-09-23 16:27:11,291	module:my_module_name	message:hello!	process:97649	thread:140735161512704

The official documentation has some configuration examples: https://docs.djangoproject.com/en/1.8/topics/logging/#examples

Recommended Posts

(Minimal) usage of django logger
Summary of pyenv usage
Basic usage of Jinja2
Impressions of touching Django
Usage of Python locals ()
Basic usage of SQLAlchemy
Super basic usage of pytest
Minimal website environment with django
Basic usage of Pandas Summary
Sample usage of Python pickle
Basic usage of Python f-string
[Python] Correct usage of join
Index of certain pandas usage
Prevent double launch of django commands
[Django] Memorandum of environment construction procedure
Implementation of login function in Django
Easy usage memo of Anaconda (conda)
Installation and easy usage of pytest
[python] Correct usage of if statement
The meaning of ".object" in Django