First, define how the log is printed
If we run our own programs, we sometimes need to log the problems or information that occur during the program's operation. Log module logging can be used to record, the module log format can be based on user needs to define their own.
The common form of print log information is as follows:
Import Logginglogging.debug ("======== defines what to print ====debug①===========") logging.info ("========= defines what to print ====info②=== ======== ") logging.warning (" ====== defines what to print ====warning③=========== ")
By default, logging prints the log to the screen with a log level of warning;
The log-level precedence relationship is: Critical>error>warning>info>debug>notset,
For log levels:
CRITICAL = 50FATAL = Criticalerror = 40WARNING = 30WARN = Warninginfo = 20DEBUG = 10NOTSET = 0
Of course, you can define the priority level according to your needs, and print from the above Log priority screen as follows:
warning:root;====== define what to print ====warning③===========
Second, the output format of the log is configured by the Logging.basicconfig function
#!/usr/bin/env python# -*- coding:utf8 -*-import loggingdef logs (ARG): logging.basicconfig (level=logging. debug, format= '% (asctime) s % (message) s ', datefmt= '%y-%m-%d %h:%m:%s ', filename= ' Myapp.log ', filemode= ' W ') logging.debug (ARG) logging.info ( ARG) logging.warning (ARG) logs (' python is Language ')
[[email protected] log]# python log.py[[email protected] log]# cat myapp.log 2016-01-27 10:44:57 python is language2016-01 -27 10:44:57 python is language2016-01-27 10:44:57 python is language
Parameters of the Logging.basicconfig function:
FileName: Specifies the log file name FileMode: The same as the file function, specifying the open mode of the log files, ' W ' or ' A ' format: Specify output formats and contents, format can output a lot of useful information, as shown in the example above:% (Levelno) s: Print Log level value% (levelname) S: Print log level name% (pathname) s: Prints the path of the current executable, which is actually sys.argv[0]% (filename) s: Prints the current executing program name% (funcName) s: Print log current function% (Lineno) d: Print log current line number% (asctime) S: Print log time% (thread) d: Print thread ID% (threadname) s: Print thread name% (process) d: Print process ID% (mess Age) S: Print log information datefmt: Specifies the time format, same as Time.strftime () level: Sets the log levels by default to logging. Warningstream: Specifies the output stream that will log, can specify output to Sys.stderr,sys.stdout or file, default output to Sys.stderr, stream is ignored when stream and filename are specified simultaneously
Third, the operation of the logging module recording program operation
The following log definition file (log.py) is available:
[[Email protected] log]# cat log.py#!/usr/bin/env python# -*- coding:utf8 -*-import loggingdef logs (ARG): Logging.basicconfig (level=logging. debug, format= '% (asctime) s % (message) s ', datefmt= '%y-%m-%d %h:%m:%s ', filename= ' Shopping.log ', &nBsp; filemode= ' a ') logging.info (ARG) [[Email protected] log]#
The application content is as follows:
[email protected] log]# cat shopping.py #!/usr/bin/env python#-*-coding:utf8-*-from log import logsuser_dict={' Apple ': 4.5, ' pear ': 3.3, ' banana ': 2.5, ' tomato ': 1.5}ch_fruit = raw_input ("Please enter the fruit to buy:") Num=int (Raw_input ("Enter the weight of the fruit to buy:")) Total_prices= user_dict[ch_fruit]*numprint "You bought%s to pay%d"% (ch_fruit,total_prices) record= "Purchase%d kg%s"% (num,ch_fruit ) #定义购买记录信息logs (record) #调用log the function logs defined in the. py
Run shopping.py
[email protected] log]# python shopping.py Please enter the fruit you want to buy: Banana Please enter the weight of the fruit to buy: 3 you buy the banana to pay 7 yuan [[email protected] log]# python shopping.py Please enter the fruit you want to buy: Pear Please enter the weight of the fruit to buy: 10 you buy the pear to pay 33 yuan [[email protected] log]# python shopping.py Please enter the fruit to buy: Apple enter the weight of the fruit to be bought: 8 The apple you bought will pay $36
To view the build log record information:
[[email protected] log]# cat shopping.log 2016-01-27 11:10:47 buy 3 kilograms banana2016-01-27 11:10:53 buy 10 kilograms pear2016-01-27 11:11:02 Buy 8 kilograms Apple[[email protected] log]#
Idea: In the production of the logging module can be written into a file, and then directly in the production program to import, and finally according to the requirements to define the record content to write
Python's configuration Log module logging