A memorandum when writing experimental code ~ Logging in python

Introduction

When I write new experimental code, I always forget the magic around logging, so I write it so that I can copy it.

It is assumed that the situation is as follows.

code

Write the following in the file of the library. The logging level for this file only is set at logger.setLevel (log.INFO). (Useful when you want to debug each function)

module.py


import logging as log

#Logger settings for each file
logger = log.getLogger(__name__)
logger.setLevel(log.INFO) #Change the display level for each module

def func():
    logger.info("From module.func()")
    logger.info("Key1\tValue")
    logger.debug("This is not displayed")

And for the main script that imports and uses this library, write as follows. The definition around formatter is your choice. Tab-separating the message and earlier, as in this example, will make it easier to process the log file later.

** 2020/06/03 Fix: It seems that the level of the log to be displayed had to be set on the handler side, so I fixed it that way. ** **

main.py


import logging as log

import module

#Logger settings for each file
logger = log.getLogger(__name__)

if __name__=="__main__":
    formatter = "[%(asctime)s] %(levelname)s - %(name)s\t%(message)s"
    handlers = [log.StreamHandler(), log.FileHandler("logfile")]
    for handler in handlers:
        handler.setLevel(log.INFO) #Level of log to display
    log.basicConfig(
        level = log.DEBUG, format = formatter, handlers = handlers
    )

    #Log output
    logger.info("Call module.func() from main.")
    module.func()
    logger.debug("This is displayed")

Output example

The following logs are output to standard output and file respectively. It is convenient because you can see which file the log was sent from.

[2020-05-15 12:39:20,676] INFO - __main__       Call module.func() from main.
[2020-05-15 12:39:20,677] INFO - module From module.func()
[2020-05-15 12:39:20,678] INFO - module Key1    Value
[2020-05-15 12:39:20,680] DEBUG - __main__This is displayed

Recommended Posts

A memorandum when writing experimental code ~ Logging in python
When writing a program in Python
Precautions when pickling a function in python
What Emacs users should know when writing python code in Sublime Text
[Tips] Easy-to-read writing when connecting functions in Python
Python variadic memorandum when inheriting a defined class
A memo about writing merge sort in Python
Split files when writing vim plugin in python
Logging properly in Python
Before writing Python code
A collection of code often used in personal Python
A memorandum to run a python script in a bat file
Things to note when initializing a list in Python
What's in that variable (when running a Python script)
Use communicate () when receiving output in a Python subprocess
Take a screenshot in Python
Create a dictionary in Python
A memorandum about correlation [Python]
Make a bookmarklet in Python
Attention when os.mkdir in Python
Thorough logging strategy in Python
Generate QR code in Python
A memorandum about Python mock
Draw a heart in Python
Character code learned in Python
[Django] A memorandum when you want to communicate asynchronously [Python3]
A memo when creating a directed graph using Graphviz in Python
Do something like a Python interpreter in Visual Studio Code
Code block when writing console (shell) command execution in Qiita
[GCP] A memorandum when running a Python program on Cloud Functions
Convenient writing method when appending to list continuously in Python
Create code that outputs "A and pretending B" in python
A memo of writing a basic function in Python using recursion
Summary of points to keep in mind when writing a program that runs on Python 2.5
[Subprocess] When you want to execute another Python program in Python code
Create a Python environment for professionals in VS Code on Windows
Write a binary search in Python
A memorandum when using beautiful soup
Precautions when using pit in Python
Hit a command in Python (Windows)
[Python] logging in your own module
[Python] Generate QR code in memory
Create a DI Container in Python
Behavior when listing in Python heapq
Draw a scatterplot matrix in python
Mode line when you open the appropriate Python code in Vim
Settings when writing Google App Engine / Python apps in Intellij Idea
ABC166 in Python A ~ C problem
Write A * (A-star) algorithm in Python
[Python] Execution time when a function is entered in a dictionary value
Create a binary file in Python
Timezone specification when converting a string to datetime type in python
[Python ORM] Notation when writing SQL using subquery in IN clause in SQLAlchemy
Write selenium test code in python
Solve ABC036 A ~ C in Python
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
A note for writing Python-like code
I get a can't set attribute when using @property in python
Precautions when creating a Python generator