']['Compression'])#YesPrint(config['topsecret.server.com']['ForwardX11'])#NoPrint(config['bitbucket.org'])# forKeyinchconfig['bitbucket.org']:#Note that a key with default defaults will Print(Key)Print(Config.options ('bitbucket.org'))#same for loop, find all keys under ' bitbucket.org 'Print(Config.items ('bitbucket.org'))#Find all key-value pairs under ' bitbucket.org 'Print(Config.get ('bitbucket.org','Compression'))#Yes Get method section key corresponding to valueAdding and removing chan
#!/usr/bin/env python#-*-coding:utf-8-*-#@Time: 2017/12/8 0008 14:30#@Author: MingImportLogginglogging.basicconfig ( level=logging. WARNING,#greater than or equal to warning level will be loggedformat='% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s', Datefmt='%a,%d%b%Y%h:%m:%s', filename='Myapp.log', FileMode='W',) Logging.debug ('This is deb
Solve the memory leakage caused by misuse of the logging module in Python, pythonlogging
First, let's introduce how to find it. The online project log is sent to syslog through the logging module. After a while, we found that the syslog UDP connection exceeded 8 W, which is 8 W. this is mainly because the logging modul
. Streamhander (sys.stdout) Print_handler.setformatter (Formatter) # Add the specified log processing method to the instance, you can add multiple log processing methods Logger.addhandler ( File_handler) # Responsible for storing to file Logger.addhandler (Print_handler) # is responsible for printing to the screen # call logger, while the screen is displayed , save the log to file Logger.debug ("This is a Debug test") Logger.info ("This is an info tes
Logging Module As the name implies, for the program log output, you can set the log level, format, output mode, etc., the basic use method is as follows:1, Logging.basicconfig Way simple configuration, can only choose to output to the screen alone or output to a file, cannot be output at the same time. For example: #-*-coding:utf-8-*-#只能选择输出到屏幕或文件import logginglogging.basicconfig (level=logging.
Before using the logging module is just the internet casually searched a post, looked at the approximate method directly used. The problem now is that when you add a handler to a Logger object, you use Streamhandler in the configuration file, and you specify the output to stdout (the default is stderr). And since only one logger and one handler are used in the program, all log levels (INFO,DEBUG,ERROR, etc.
The example mainly solves the problem of changing the log storage location by passing in the log file parameters. If you need it, you can refer to the simple version of logging. config, which supports general configuration through the config file.
Another better way is to use the logging. config. fileConfig (log_config_file) method to read in and modify the handler method.
The Code is as follows:
"""Proje
In this paper, examples of the use of logging modules to share for everyone to reference. Here's how:
Import logging import os log = Logging.getlogger () formatter = logging. Formatter (' [% (asctime) s] [% (name) s]% (levelname) s:% (message) s ') Stream_handler = logging. Streamhandler () File_handler =
, such as Log/myapp.logwhen is a string defined as follows: "S": Seconds "M": Minutes "H": Hours "D": Days " W ": Week Day (0=monday)" Midnight ": Roll over at MidnightiNterval refers to the number of units waiting for when the time, logger will automatically rebuild the file, of course, the creation of this file depends on Filename+suffix, if the file has the same name as the previous file, it will automatically overwrite the previous file, So there are cases where the suffix to be defined cann
LoggingLogging moduleLet's look at an example.Import logginglogging.debug ("This isdebug message") logging.info ( "This isinfo message") logging.warning ("Theiswarning message ")Operation Result:WARNING:root:this is WARNING messageNote: Three sentences are written in the program, but only one warning level log is printed on the screenBy default, logging tells the log to print to the screen, and the log level size relationship is: critical>error>warni
In addition to using the Basicconfig method in the logging module to configure the log, the Dictconfig and Fileconfig methods in Python's Logging.config module support the configuration of Logger, Handl, and files by dictionary and file respectively. ER and Formatter. Here is an example of how to use the file configuration log, more detailed usage reference: https://docs.python.org/2/library/logging.config.htmlLogging.conf#############################
Logging module
1. List all status and print to terminal.ImportLogginglogging.debug ("Test Debug")#debugging, error-removingLogging.info ("Test")#Informationlogging.warning ("user [Xsk] 3")#WarningLogging.error ("Test Error")#Errorlogging.critical ("Server is down")#SeriousNote: Serious warnings are printed directly to the terminal, which is prioritized down to the next level.2. Print the log to a
, there is no information level and other information. Logging supports many replacement values. For details, see the formatter documentation. There are three items: time, information level, and log information. (Three items are commonly used)The 7th line formatter is set to the processor.8th rows of processors are added to the log object.Row 3 sets the log output level. Logging provides multiple levels of
The example of this article tells the usage example of logging module, share for everybody reference. The specific methods are as follows:
Import logging
import os
log = Logging.getlogger ()
formatter = logging. Formatter (' [% (asctime) s] [% (name) s]% (levelname) s:% (message) s ')
Stream_handler = logging
information except Zerodivisionerror. What's going on?
Don't worry, add a line configuration after import logging and try again:
Import Logginglogging.basicconfig (Level=logging.info)
See the output:
$ python err.pyinfo:root:n = 0Traceback (most recent call last): File "err.py", line 8, in
This is the benefit of logging, which allows you to specify the level o
In fact, the two can work together normally.The only drawback is that logging displays such records.
2011 - 07 - 02 23 : 14 : 18 , 154 - Dummy-1 - Thread - Debug - This Is Qthread_1 output 2011 - 07 - 02 23 : 14 : 18 , 467 - Dummy-2 - Thread - Debug - This Is Qthread_2 output 2011 - 07 - 02 23 : 14
ImportLoggingdeflogger (): Logger= Logging.getlogger ('Logger') #括号里面默认是root Logger.setlevel ('DEBUG') #设置等级是DEBUG, the number is 10,info is 20,warning is 30,error is 40,criticle is the _format= Logging. Formatter ('[% (name) s]% (asctime) s% (levelname) -8s:% (message) s') FH= Logging. Filehandler ('Log.txt', encoding
Many programs have logging requirements, and the log contains information that has normal program access logs, there may be errors, warnings and other information output, Python's logging module provides a standard log interface, you can store various formats of the log, logging log can be divided into debug(),,, info(
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.