Logging errors and status in Python【logging】

English pages
スポンサーリンク

Hello, this is Yuki (@engineerblog_Yu), a student engineer.

In this article, I explained logging as simply as possible for those who have mastered the basic syntax of Python and want to actually create applications.

スポンサーリンク

Logging Basics

import logging

logging.critical('critical')
logging.error('error')
logging.warning('warning')
logging.info('info')
logging.debug('debug')
CRITICAL:root:critical
ERROR:root:error
WARNING:root:warning

You can change the level of the argument to display info or debag.

import logging

logging.basicConfig(level=logging.INFO)

logging.critical('critical')
logging.error('error')
logging.warning('warning')
logging.info('info')
logging.debug('debug')
CRITICAL:root:critical
ERROR:root:error
WARNING:root:warning
INFO:root:info
import logging

logging.basicConfig(level=logging.DEBUG)

logging.critical('critical')
logging.error('error')
logging.warning('warning')
logging.info('info')
logging.debug('debug')
CRITICAL:root:critical
ERROR:root:error
WARNING:root:warning
INFO:root:info
DEBUG:root:debug

Write logging to file

To write output information to a file, assign the name of the file to the filename argument.

import logging

logging.basicConfig(filename='log.test',level=logging.DEBUG)

logging.critical('critical')
logging.error('error')
logging.warning('warning')
logging.info('info')
logging.debug('debug')
CRITICAL:root:critical
ERROR:root:error
WARNING:root:warning
INFO:root:info
DEBUG:root:debug

logger

Next, we will discuss loggers.

A logger is used to specify the file being executed.

First, let’s use the logger to specify that it is being executed in main.

import logging

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger(__name__)
logger.critical('critical')
logger.error('error')
logger.warning('warning')
logger.info('info')
logger.debug('debug')
CRITICAL:__main__:critical
ERROR:__main__:error
WARNING:__main__:warning
INFO:__main__:info
DEBUG:__main__:debug

At the End

In this article, I explained logging for those who want to create applications.

Once you are familiar with logging, it is a good idea to actually produce it.

If you are interested, you can take a course on Udemy.

多彩な講座から自分に合った講座を探そう!

コメント

タイトルとURLをコピーしました