Minimum knowledge to get started with the Python logging module

Minimum knowledge to get started with the Python logging module

This article will show you how to get started with the Python logging module for app development with minimal knowledge. Since we have compiled only the minimum knowledge that can be used immediately in development, we hope that you can use it as a starting point while supplementing the missing information.

Basic functions of logging

It's like a print statement (but don't use it this way as logging doesn't make sense)

import logging
logging.warning( 'Watch out!') # Print a message to the console
logging.info( 'I said so') # The default log level is a warning, so nothing should be output to the console

Minimum knowledge and settings to use logging in app development

logger

Templates for using logging

root
├──your_app.py
├──logging.conf
└──logging_dev.conf
root
├──app
│└──your_app.py
└──conf
    ├──logging.conf
    └──logging_dev.conf

your_app.py


import logging

# read the conf file
logging.config.fileConfig('logging_dev.conf', disable_existing_loggers=False)

# create logger
logger = logging.getLogger('app')

def main():
    try:
        1/0  # ZeroDivisionError
        # your code goes here   
    except Exception as e:
        logger.exception(e.args)


if __name__ == '__main__':
    main()

logging config file

point

Sample operation configuration file (Note that the log level is INFO)

logging-prod.conf


[loggers]
keys=root,app

[formatters]
keys=default

[handlers]
keys=frotate,default

[formatter_default]
format=%(asctime)s %(levelname)s %(message)s
datefmt=%Y/%m/%d %H:%M:%S

[handler_frotate]
class=handlers.TimedRotatingFileHandler
formatter=default
args=('logs/logger.log', 'W0', 1, 100)

[handler_default]
class=StreamHandler
formatter=default
args=(sys.stdout,)

[logger_app]
level=INFO
handlers=frotate
qualname=app

[logger_root]
level=INFO
handlers=default

Sample dev config file (note that the log level is NOTSET and is noisier than DEBUG)

logging-dev.conf



[loggers]
keys=root,app

[formatters]
keys=default

[handlers]
keys=frotate,default

[formatter_default]
format=%(asctime)s %(levelname)s %(message)s
datefmt=%Y/%m/%d %H:%M:%S

[handler_frotate]
class=handlers.TimedRotatingFileHandler
formatter=default
args=('logs/logger_dev.log', 'W0', 1, 100)

[handler_default]
class=StreamHandler
formatter=default
args=(sys.stdout,)

[logger_app]
level=NOTSET
handlers=frotate
qualname=app

[logger_root]
level=NOTSET
handlers=default

Summary

I have briefly introduced the minimum usage of the logging module. If you need to know more details, please take a look at the documents introduced in [Reference](see #).

reference

Recommended Posts

Minimum knowledge to get started with the Python logging module
Link to get started with python
How to get started with Python
The easiest way to get started with Django
A layman wants to get started with Python
Get started with Python! ~ ② Grammar ~
I tried to get started with blender python script_Part 01
I tried to get started with blender python script_Part 02
How to get started with the 2020 Python project (windows wsl and mac standardization)
Get started with Python! ~ ① Environment construction ~
Get the weather with Python requests
Get the weather with Python requests 2
How to get the Python version
How to get started with Scrapy
How to get started with Django
How to get into the python development environment with Vagrant
Get started with Python in Blender
[Introduction to Python] How to get data with the listdir function
Get the source of the page to load infinitely with python.
Run the program without building a Python environment! !! (How to get started with Google Colaboratory)
Get started with the Python framework Django on Mac OS X
[Python] A memo that I tried to get started with asyncio
I tried to get started with Bitcoin Systre on the weekend
The fastest way to get camera images regularly with python opencv
PhytoMine-I tried to get the genetic information of plants with Python
Step notes to get started with django
Getting Started with python3 # 1 Learn Basic Knowledge
I tried to get started with Hy
[Python] Get the variable name with str
Get started with the documentation tool Sphinx
Get Started with TopCoder in Python (2020 Edition)
[Python] How to deal with module errors
How Python beginners get started with Python with Progete
How to get started with laravel (Linux)
The road to compiling to Python 3 with Thrift
[Blender x Python] Let's get started with Blender Python !!
[Python] To get started with Python, you must first make sure you can use Python.
How to get the date and time difference in seconds with python
I tried to get the authentication code of Qiita API with Python.
I get a Python No module named'encodings' error with the aws command
I tried to get the movie information of TMDb API with Python
Python hand play (let's get started with AtCoder?)
[Python] Get the files in a folder with Python
The easiest way to synthesize speech with python
Try to solve the man-machine chart with Python
Introduction to Tornado (1): Python web framework started with Tornado
Specify the Python executable to use with virtualenv
Say hello to the world with Python with IntelliJ
I tried to get CloudWatch data with Python
The easiest way to use OpenCV with python
Specifying the module loading destination with GAE python
Introduction to Python with Atom (on the way)
How to get the files in the [Python] folder
[Python] A rough understanding of the logging module
Get started with Python on macOS Big Sur
Examine Python script bottlenecks with the cProfile module
Knowledge notes needed to understand the Python framework
Materials to read when getting started with Python
[Cloud102] # 1 Get Started with Python (Part 1 Python First Steps)
[Minimum configuration] Use the pillow module of python to synthesize the characters "100 yen" with the image of tapioca milk tea.
Python script to get note information with REAPER