Use the logging module in Python instead of print (simple logging guide)

Source: Internet
Author: User
This article mainly introduces the benefits of using the logging module in Python to replace print. The main purpose is the simple guide of the logging module. for details about how to use the logging module, refer Replace print? Print what's wrong?

Print may be the first thing that anyone who learns the Python language has come into contact. Its main function is to print a piece of information to the console, such:

The code is as follows:


Print 'Hello, logging! '

Print is also the most widely used thing for debugging your program. it is as natural as writing JavaScript code to use console. log. Many beginners who have just started learning Python and even experienced veterans are using print to debug their code.

For example, this is a small program I wrote to output the Fibonacci series. let's look at its code:

The code is as follows:


#-*-Coding: UTF-8 -*-
"""
A simple maid program
"""
Import argparse

Parser = argparse. ArgumentParser (description = 'I print ononacci sequence ')
Parser. add_argument ('-s',' -- start', type = int, dest = 'start ',
Help = 'start of the sequence ', required = True)
Parser. add_argument ('-E',' -- end', type = int, dest = 'end ',
Help = 'end of the sequence ', required = True)

Def infinite_fib ():
A, B = 0, 1
Yield
Yield B
While True:
# Print 'Before caculation: a, B = % s, % s' % (a, B)
A, B = B, a + B
# Print 'after caculation: a, B = % s, % s' % (a, B)
Yield B


Def fib (start, end ):
For cur in infinite_fib ():
# Print 'cur: % s, start: % s, end: % s' % (cur, start, end)
If cur> end:
Return
If cur> = start:
# Print 'returning result % s' % cur
Yield cur

Def main ():
Args = parser. parse_args ()
For n in fib (args. start, args. end ):
Print n,

If _ name _ = '_ main __':
Main ()


Let's see how it works:

The code is as follows:


$ Python fib. py-s 1-e 100
1 1 2 3 5 8 13 21 34 55 89
$ Python fib. py-s 100-e 1000
144 233 377 610 987


The program correctly completed its functions. But wait, what is the comment-out print statement in the program?

It turns out that this is the output information used for debugging when I write this applet. after I complete this program, I naturally commented out the print. Let's take a look at what will happen if we open this print statement?

The code is as follows:


$ Python fib. py-s 1-e 100
Cur: 0, start: 1, end: 100
Cur: 1, start: 1 and end: 100
Returning result 1
1 Before caculation: a, B = 0, 1
After caculation: a, B = 1, 1
Cur: 1, start: 1 and end: 100
......
......
(Countless output information)

As you can see, all the computing processes are printed out.

Print is added when writing, and you have to remember to delete/comment out the print statement when submitting code. why do we have to endure such troubles? Let's introduce our main character logging, which is almost born for this scenario.

Better practice: Use the logging module

The logging module is a built-in logging module of Python that allows you to easily process and manage log output. The simplest use of the logging module is to directly use the basicConfig method to Configure logging:

The code is as follows:


Import logging

# Set the default level to DEBUG
# Set the log format
Logging. basicConfig (
Level = logging. DEBUG,
Format = "[% (asctime) s] % (name) s: % (levelname) s: % (message) s"
)

# Record log
Logging. debug (...)
Logging.info (...)
Logging. warn (...)
Logging. error (...)
Logging. critical (...)


After logging is configured, use ''logging. debug'' to replace all print statements. The output is as follows:

The code is as follows:


[15:17:45, 216] root: cur: 0, start: 1, end: 100
[15:17:45, 216] root: DEBUG: cur: 1, start: 1, end: 100
[15:17:45, 216] root: DEBUG: Returning result 1
[15:17:45, 216] root: DEBUG: Before caculation: a, B = 0, 1
......

Use real logger

The basicConfig method mentioned above can meet your needs in most scenarios, but basicConfig has a major disadvantage.

Calling basicConfig actually adds a handler to the root logger, so that when your program works with other third-party modules that use logging, it will affect the logger behavior of the third-party module. This is determined by the inheritance feature of logger.

So we need to use the real logger:

The code is as follows:


Import logging

# Use a logger named fib
Logger = logging. getLogger ('fib ')

# Set logger level to DEBUG
Logger. setLevel (logging. DEBUG)

# Create a StreamHandler that outputs logs to the console
Hdr = logging. StreamHandler ()
Formatter = logging. Formatter ('[% (asctime) s] % (name) s: % (levelname) s: % (message) s ')
Hdr. setFormatter (formatter)

# Add handler to logger
Logger. addHandler (hdr)

In this way, you can use logger to output logs. However, the disadvantage is that the amount of code is much larger than basicConfig. Therefore, we recommend that you use basicConfig directly if it is a very simple script. if it is a slightly larger project, we recommend that you configure logger carefully.

Dynamically control all output of the script

After using the logging module, we can easily control program output by modifying the logger's log level. For example, we can add a-v parameter for our Fibonacci series to control all debugging information printing.

The code is as follows:


# Add and receive a verbose parameter
Parser. add_argument ('-v',' -- verbose ', action = 'store _ true', dest = 'verbose ',
Help = 'enable debug info ')

# Verbose judgment
If args. verbose:
Logger. setLevel (logging. DEBUG)
Else:
Logger. setLevel (logging. ERROR)

In this way, by default, our applet will not print debugging information. only when '-v/-- verbose' is passed in will we print additional debug information, like this:

The code is as follows:


$ Python fib. py-s 1-e 100
1 1 2 3 5 8 13 21 34 55 89

$ Python fib. py-s 1-e 100-v
[15:17:45, 216] fib: DEBUG: cur: 0, start: 1, end: 100
[15:17:45, 216] fib: DEBUG: cur: 1, start: 1, end: 100
[15:17:45, 216] fib: DEBUG: Returning result 1
[15:17:45, 216] fib: DEBUG: Before caculation: a, B = 0, 1
......

As you can see, after logging is used, when to print the DEBUG information and when to disable it makes everything quite simple.

So replace the print in your script with logging!

Additional reading

The above just introduces some of the simplest functions of the logging module, which can be used as a substitute for print. the logging module also has many very powerful and useful functions, for example, reading configuration from a file and various Handlers. Read the official logging documentation:

1. logging Logging facility for Python
2. Logging HOWTO

The complete code of the Fibonacci series program using the logging module is attached:

The code is as follows:


#-*-Coding: UTF-8 -*-
"""
A simple maid program
"""
Import argparse

Parser = argparse. ArgumentParser (description = 'I print ononacci sequence ')
Parser. add_argument ('-s',' -- start', type = int, dest = 'start ',
Help = 'start of the sequence ', required = True)
Parser. add_argument ('-E',' -- end', type = int, dest = 'end ',
Help = 'end of the sequence ', required = True)
Parser. add_argument ('-v',' -- verbose ', action = 'store _ true', dest = 'verbose ',
Help = 'enable debug info ')

Import logging

Logger = logging. getLogger ('fib ')
Logger. setLevel (logging. DEBUG)

Hdr = logging. StreamHandler ()
Formatter = logging. Formatter ('[% (asctime) s] % (name) s: % (levelname) s: % (message) s ')
Hdr. setFormatter (formatter)

Logger. addHandler (hdr)


Def infinite_fib ():
A, B = 0, 1
Yield
Yield B
While True:
Logger. debug ('before caculation: a, B = % s, % s' % (a, B ))
A, B = B, a + B
Logger. debug ('After caculation: a, B = % s, % s' % (a, B ))
Yield B


Def fib (start, end ):
For cur in infinite_fib ():
Logger. debug ('cur: % s, start: % s, end: % s' % (cur, start, end ))
If cur> end:
Return
If cur> = start:
Logger. debug ('returning result % s' % cur)
Yield cur

Def main ():
Args = parser. parse_args ()
If args. verbose:
Logger. setLevel (logging. DEBUG)
Else:
Logger. setLevel (logging. ERROR)

For n in fib (args. start, args. end ):
Print n,

If _ name _ = '_ main __':
Main ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.