Python --Quick start of logging

Ref: [1] - Good logging practice in Python

A very simple file logger:

import logging

logger = logging.getLogger(__name__) # this will show current module in the log line
logger.setLevel(logging.INFO)

# create a file handler

handler = logging.FileHandler('hello.log')
handler.setLevel(logging.INFO)

# create a logging format

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)

# add the handlers to the logger

logger.addHandler(handler)

logger.info('Happy logging!')

Logging in a catch

try:
    open('/path/to/does/not/exist', 'rb')
except (SystemExit, KeyboardInterrupt):
    raise
except Exception, e:
    logger.error('Failed to open file', exc_info=True)

Recommended Posts

Python --Quick start of logging
Python quick start
Start python
Python Logging
Python3 compatible memo of "python start book"
Introduction of Python
Start using Python
Basics of Python ①
[Python] Start studying
Basics of python ①
Copy of python
Introduction of Python
Beware of disable_existing_loggers when setting up Python logging
[Python] A rough understanding of the logging module
[Python] Operation of enumerate
List of python modules
Unification of Python environment
Copy of python preferences
Logging properly in Python
Basics of Python scraping basics
[python] behavior of argmax
Usage of Python locals ()
the zen of Python
Installation of Python 3.3 rc1
I implemented Python Logging
# 4 [python] Basics of functions
Basic knowledge of Python
Suppress python3 flask logging
Sober trivia of python3
Summary of Python arguments
Basics of python: Output
Installation of matplotlib (Python 3.3.2)
Application of Python 3 vars
Various processing of Python
A quick comparison of Python and node.js test libraries
Working with Azure CosmosDB from Python (quick start digging)
Tasks at the start of a new python project
[Python] Correct usage of map
Towards the retirement of Python2
Summary of python file operations
Recommendation of binpacking library of python
[python] Value of function object (?)
Automatic update of Python module
Python --Check type of values
[Python] Etymology of python function names
About the ease of Python
I want to start a lot of processes from python
Static analysis of Python programs
About various encodings of Python 3
Equivalence of objects in Python
Start to Selenium using python
[Gimp] Start scripting in Python
Introduction of activities applying Python
python> Handling of 2D arrays
Start / stop GCE from python
Install multiple versions of Python
Version upgrade of python Anaconda
Handling of python on mac
python: Basics of using scikit-learn ①
3 Reasons Beginners to Start Python
2.x, 3.x character code of python