Output python log to both console and file

Introduction

Use logger. ~ To log to a file and print () to log to the console. Have you ever thought it was a hassle?

When making a tool in the company, I want to output the log to both the console and the file, and if I was examining the logging module, if I use both StreamHandler which outputs the log to the console and FileHandler which outputs to the file, the console output I found that both and file output can be done easily, so I will leave a memorandum at that time.

References

-Please stop printing and import logging for log output -Python Official Reference (logging)

environment

Logger setup

If you want to output to either a file or console with logging.basicConfig (), you can specify filename = or stream =, but if you want to output to both a file and a console, etc. When using more than one, give a list of handlers to handlers = without specifying and initialize. If the file handler specifies a folder that does not exist in the save path, an error will occur, so if it does not exist, add the process to create it.

import os
import sys
from datetime import datetime
import logging
from logging import StreamHandler, FileHandler, Formatter
from logging import INFO, DEBUG, NOTSET

#Stream handler settings
stream_handler = StreamHandler()
stream_handler.setLevel(INFO)
stream_handler.setFormatter(Formatter("%(message)s"))

#Check if there is a save destination
if not os.path.isdir('./Log'):
    os.makedirs('./Log', exist_ok=True)

#File handler settings
file_handler = FileHandler(
    f"./Log/log{datetime.now():%Y%m%d%H%M%S}.log"
)
file_handler.setLevel(DEBUG)
file_handler.setFormatter(
    Formatter("%(asctime)s@ %(name)s [%(levelname)s] %(funcName)s: %(message)s")
)

#Root logger settings
logging.basicConfig(level=NOTSET, handlers=[stream_handler, file_handler])

You are now set to output the logging module to both the console and the file. Note the output level set with logging.basicConfig (). Logs are passed in the order of root logger → handler. Therefore, if you set the output level to INFO etc. in the root logger settinglogging.basicConfig (), the log with the output level DEBUG will be played at the time of the root logger. Therefore, even if the output level is set to DEBUG in the file handler settings, the log with the output level DEBUG is not saved in the file.

The program is written in solid here, but I think it's a good idea to put it in a function as appropriate. Because there is no other place to use these objects.

Use a logger

If you want to use a logger, use logging.getLogger (__ name__) to get the logger object and use it. Also, even if you cross functions or files, once you set the root logger, you can use it anywhere, so you can use it by calling logging.getLogger (__name__) at the place you want to use.

logger = logging.getLogger(__name__)

logger.debug("debug")
logger.info("info")
logger.warn("warn")
logger.error("error")
logger.critical("critical")

I think that all the logs are output to the file and the logs from info are output to the console.

Rich console output

Then use rich to make the console output rich. The method is simple, just change logging.StreamHandler to rich.logging.RichHandler.

Install rich.

$ pip install rich

Once installed, replace StreamHandler in the logger setup chapter with RichHandler.

from rich.logging import RichHandler

#Stream handler settings
rich_handler: RichHandler = RichHandler(rich_tracebacks=True)
rich_handler.setLevel(INFO)
rich_handler.setFormatter(Formatter("%(message)s"))

#File handler settings
#abridgement

#Root logger settings
logging.basicConfig(level=NOTSET, handlers=[rich_handler, file_handler])

The output is now rich.

Recommended Posts

Output python log to both console and file
Output Python log to console with GAE
Output to csv file with Python
python input and output
2017-02-19 Python> Link> Redirect to output file immediately> sys.stdout.flush ()
[CentOS8] How to output Python standard output to systemd log
[Python] Concatenate a List containing numbers and write it to an output file.
Procedure to load MNIST with python and output to png
Output log to console with Flask + Nginx on Docker
Read json file with Python, format it, and output json
Output a binary dump in binary and revert to a binary file
Python logging standard library for file output by log level
Change the standard output destination to a file in Python
Python application: Data handling Part 1: Data formatting and file input / output
How to write a metaclass that supports both python2 and python3
[Python] How to output a pandas table to an excel file
[GCP] How to output Cloud Functions log to Cloud Logging (Stackdriver Logging) (Python)
How to log in to AtCoder with Python and submit automatically
Post Jenkins console output to Slack
Python 3.6 on Windows ... and to Xamarin.
[Introduction to Python3 Day 1] Programming and Python
Tips on Python file input / output
[Python] Write to csv file with Python
Write standard output to a file
Output cell to file with Colaboratory
[python] Copy script to generate copy log
[Code] Module and Python version output
Unit test log output with python
Python CSV file reading and writing
Notes for Python file input / output
Python memo ① Folder and file operations
Export and output files in Python
How to overwrite the output to the console
Python logging and dump to json
(Python) ABC162-D Consideration log and solution
Selenium and python to open google
Add TRACE log level to Python ...?
File upload to Azure Storage (Python)
Make it possible to output a log to a file with go echo
Process Splunk execution results using Python and save to a file
Script to organize LDOS and PDOS from VASP output file DOSCAR
I want to use both key and value of Python iterator
When you run diff in python and want both returncode and output
[Python] Conversation using OpenJTalk and Talk API (up to voice output)
[python3] Implement debug log output function easily with logging and Click
[Python] How to scrape a local html file and output it as CSV using Beautiful Soup
[Python scraping] Output the URL and title of the site containing a specific keyword to a text file
[Python] How to convert db file to csv
[Python Kivy] How to get the file path by dragging and dropping
How to package and distribute Python scripts
I want to output to the console coolly
Data handling 1 Data formatting and file input / output
How to install and use pandas_datareader [Python]
Output color characters to pretty with python
Bind methods to Python classes and instances
How to convert Python to an exe file
[Python] Convert csv file delimiters to tab delimiters
Convert psd file to png in Python
Fractal to make and play with Python
Copy file and rewrite cell value @python
Log in to Slack using requests in Python