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

How to use logging in Python

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

Share

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

This article is about how to use logging in Python. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Import logging

''

By default, Python's logging module prints logs to standard output, and only logs greater than or equal to the WARNING level are displayed

This indicates that the default log level is set to WARNING (log level CRITICAL > ERROR > WARNING > INFO > DEBUG)

The default log format is log level: Logger name: user output message.

It can be seen that the default behavior of logging module can be changed through specific parameters in the logging.basicConfig () function. The available parameters are

Filename: create a FiledHandler with the specified file name (the concept of handler will be explained later) so that the log is stored in the specified file.

Filemode: file opening method, which is used when filename is specified. The default value is "a" and can also be specified as "w".

Format: specifies the log display format used by handler.

Datefmt: specifies the date and time format.

Level: sets the log level of rootlogger (specific concepts will be explained later).

Stream: creates a StreamHandler with the specified stream. You can specify output to sys.stderr,sys.stdout or to a file (f=open ('test.log','w')), which defaults to sys.stderr.

If both filename and stream are listed, the stream parameter is ignored.

Format strings that may be used in the format parameter:

Name of% (name) s Logger

% (levelno) s log level in digital form

% (levelname) s log level in text form

% (pathname) s the full path name of the module that called the log output function, which may not exist

% (filename) s file name of the module that calls the log output function

% (module) s module name that calls the log output function

% (funcName) s calls the function name of the log output function

% (lineno) d the line of code where the statement calling the log output function is located

% (created) f current time, represented by the UNIX standard floating point number of time

The number of milliseconds since Logger was created when% (relativeCreated) d output log information

% (asctime) s current time in string form. The default format is "2003-07-08 16 purge 4945896". The comma is followed by milliseconds.

% (thread) d thread ID. Maybe not.

% (threadName) s thread name. Maybe not.

% (process) d process ID. Maybe not.

% (message) s user output message

The logging library provides several components: Logger, Handler, Filter, Formatter. The Logger object provides an interface that can be directly used by the application, Handler sends logs to the appropriate destination, Filter provides a method to filter log information, and Formatter specifies the log display format.

Logger is a tree hierarchy, and you need to get a Logger before outputting the information (automatically create and use the root Logger if there is no displayed fetch).

Logger = logging.getLogger () returns a default Logger, or root Logger, and applies the default log level, Handler, and Formatter settings.

Of course, you can also specify the lowest log level through Logger.setLevel (lel). The available log levels are logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, and logging.CRITICAL.

Logger.debug (), Logger.info (), Logger.warning (), Logger.error (), Logger.critical () output different levels of logs, and only logs whose log level is greater than or equal to the set log level will be output.

''

Logging.basicConfig (level=logging.DEBUG, # configure log level, default output debug level and higher content

Format='% (asctime) s murmur->% (filename) s [line:% (lineno) d] = >% (levelname) s Groupe Groupe% (message) slots, # configure the log display format

Datefmt='%a,% d% b% Y% HGV% MVA% slots, # configure time format

Filename='test.log', # profile path

Filemode='a') # profile permissions

Logging.debug ('debug message')

Logging.warning ('warning message')

Example 4: log is written only in the screen file, not displayed on the screen

Def logger ():

Logger = logging.getLogger () # create a large object

Fh = logging.FileHandler ("test_log") # sends the content to the file and gives a parameter to define a file name and write the content to the file

Ch=logging.StreamHandler () # sends content to the screen

Fm = logging.Formatter ("% (asctime) Smurf->% (filename) s [line:% (lineno) d] = >% (levelname) Slave Velvet% (message) s") # this is also an object and its purpose is to define the log format

Fh.setFormatter (fm) # write to the file

Ch.setFormatter (fm) # outputs content to the screen

Logger.addHandler (fh) # object, which is similar to sucking other people's internal force and eating fh

Logger.addHandler (ch) # object, which is similar to sucking other people's internal force and eating ch

Logger.setLevel ("DEBUG") # sets the log level and controls how many messages are entered in the log

Return logger

-I have been operating log- from here on.

Logger = logger () # this log is made into an interface, and in other places, just call it directly!

Logger.debug ("debug") # level of output log

Logger.info ("info")

Logger.warning ("warning")

Logger.error ("error")

Logger.critical ("critical")

Thank you for reading! This is the end of this article on "how to use logging in Python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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