Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to Python logging Modul

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "introduction to Python logging module". In daily operation, I believe many people have doubts about the introduction of Python logging module. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "introduction to Python logging module"! Next, please follow the editor to study!

Catalogue

I. Log level

1. Level definition

two。 Simple exampl

3. Specify level

II. Basic concepts

1. Logger

2. Handler

3. Formatter

III. Examples

1. Log output to console

two。 Log output to file

4. Default Logger

1. Log level 1. Level definition

The logging module provides five log levels: CRITICAL > ERROR > WARNING > INFO > DEBUG. These levels have different priorities, with CRITICAL having the highest priority and DEBUG having the lowest priority. Specifically,

Debug: prints logs at all levels, usually used when debugging code

Info: prints logs at the info, warning, error, and critical levels to verify that the code works as expected

Warning: print logs at the waring, error, and critical levels to warn of situations

Error: print error and critical level logs to warn of serious errors

Critical: print only critical level logs to warn of some very serious errors

two。 Simple example import logging# defaults to warninglogging.debug ("Debug") logging.info ("Info") logging.warning ("Warning") logging.error ("Error") logging.critical ("Critical")

Output:

WARNING:root:Warning

ERROR:root:Error

CRITICAL:root:Critical

3. Specify the level import logging# specifies that the level is debug, which will output logging.basicConfig (level=logging.DEBUG) logging.debug ("Debug") logging.info ("Info") logging.warning ("Warning") logging.error ("Error") logging.critical ("Critical") for all levels

Output:

DEBUG:root:Debug

INFO:root:Info

WARNING:root:Warning

ERROR:root:Error

CRITICAL:root:Critical

2. Basic concepts 1. Logger

Logger is the main body of the Logging module, and its main responsibilities are:

Provides an interface for logging programs; (that is, logger has five functions: debug, info, warning, error and critical)

Determine the level of the log and determine whether to filter

Distribute logs to different handler based on log level

2. Handler

The main responsibility is to distribute the logs according to the log level. For example, it is decided to distribute the log to the screen or file according to the log level.

3. Formatter

Used to specify the format of the log.

Usually a Logger can contain multiple Handler, and each Handler can specify a Formatter

Third, example 1. Log output to the console import sysimport logginglogger = logging.getLogger (_ name__) formatter = logging.Formatter ("% (asctime) s -% (message) s") handler = logging.StreamHandler (stream=sys.stdout) handler.setFormatter (formatter) logger.addHandler (handler) logger.setLevel (logging.INFO) logger.debug ("Debug") logger.info ("Info") logger.warning ("Warning") logger.error ("Error") logger.critical ("Critical") 2. Log output to file import logginglogger = logging.getLogger (_ _ name__) formatter = logging.Formatter ("% (asctime) s -% (message) s") # you can export log to file handler = logging.FileHandler (". / test.log") using FileHandler Mode='w') handler.setFormatter (formatter) logger.addHandler (handler) logger.setLevel (logging.INFO) logger.debug ("Debug") logger.info ("Info") logger.warning ("Warning") logger.error ("Error") logger.critical ("Critical") IV, default Logger

The Logging module provides the default Logger, which does not require explicit generation of Logger, Handler, Formatter, and so on. Use the example

Import logginglogging.basicConfig (level=logging.INFO, format= "% (asctime) s -% (message) s") logging.debug ("Debug") logging.info ("Info") logging.warning ("Warning") logging.error ("Error") logging.critical ("Critical"), on the "Python logging module introduction" study is over, hope to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report