Recently, in writing a program that uses Python to generate apps, it is found that printing information directly with print is less convenient and prescriptive, so using the logging log module, simply record the usage, The Logging.config configuration log should be used in a formal project to achieve log file size limits like log4j, format control, output location, and so on.1. Simply print the log to the s
This article mainly introduces how to solve the memory leakage caused by misuse of the logging module in Python and the problems caused by excessive UDP connections, if you need it, refer to the following section to describe how to find it. the online project log is sent to syslog through the logging module, after running for a while, we found that the syslog UDP
Python has a logging module to facilitate the recording of program action logs, this article is a simple example of the use of the logging module:
First introduce logging:
Import xlrdImport MySQLdbImport OSImport logging
First initialize the configuration in the main prog
=[log_entry])transport.close()if result == scribe.ResultCode.OK: sys.exit()elif result == scribe.ResultCode.TRY_LATER:print >> sys.stderr, "TRY_LATER" sys.exit(84) # 'T'else: sys.exit("Unknown error code.")
The usage is as follows:
$echo "hello world" | ./scribe_cat [-h host[:port]] category
To make it easier for the program to use the scribe log aggregation architecture, I integrate scribe log processing into Python
Python logging library usage summary, pythonlogging
Preface
Recently, due to work requirements, it is very low to write some python scripts and print is always used to print information. So I took the time to study the python logging library, let's print and record the logs
Use Python logging to record log information such as DebugDebug above information written to fileInfo above info output in consoleImport Osimport loggingvde_logging_name = "vde_logging" Log_file_path = Os.path.join (Os.path.dirname (__file__), "Vde_ Regression.log ") def logger_initialization (): # 1. Logging logger = Logging.getlogger (vde_logging_name) logger.s
Python logging Best PracticesCause
I often joke with my colleagues. I said that in a company, I can clearly understand the log function. So I wrote an article to share some of my knowledge.The official Python article is the clearest among the log documents I have seen currently.
This flowchart is very important. I hope you can take a closer look.1. Applicable sce
OS Module (Supplemental) 1. View current path and switch path>>> Import OS>>> OS.GETCWD () #获取当前文件所在的路径' D:\\python\\lib\\idlelib '>>> Os.chdir ('.. /') #切换当上一层目录, this can be a relative path>>> OS.GETCWD ()' D:\\python\\lib '>>> os.chdir (' D:\Program Files (x86) ') #也可以是绝对路径>>> OS.GETCWD ()' D:\\Program Files (x86) 'Add:Found on Pycharm that OS.GETCWD () and Os.path.realpath ("./") return the absolute pat
My scheduled task will be 5 o'clock in the morning every day to adjust a transaction, trading logs with the logging module, today found that log has a problem, the following troubleshooting.
When viewing the log, I found that I should have logged 5, and the log time was displayed as2016-09-12 21:00:02,379-info-novel.py[line:82]-Select day
Checked the code, did not find the problem, and then found 21 points and 5 spreads 8 hours, that is, the
Python comes with a log processing module loggingThe default log level is debug,info,warning,error,critical, the corresponding function is debug (), INFO (), WARNING (), ERROR (), and CRITICAL ()In [490]: Import Loggingin [491]: log_filename= '/tmp/example.log ' in [492]: Logging.basicconfig (Filename=log_filename, Level=logging. DEBUG) in [493]: logging.debug (' This message should go to the log file ')Vie
This article describes the rapid use of the logging module, If you need in-depth customization, please refer to (http://docs.python.org/2/howto/logging-cookbook.html)
Import logging. basicconfig (format = '[% (asctime) S]' + logging. basic_format) # logging. basic_forma
log information is output, the number of milliseconds since logger was created% (asctime) s The current time in string form. The default format is "2003-07-08 16:49:45,896". The comma is followed by milliseconds%(thread) d thread ID. Probably not .%(threadname) s thread name. Probably not .%(process) d ID. Probably not .% (message) s user-output messageparameter ConfigurationLogging Object ConfigurationImportLoggingdefMy_logger (Filename,file=true,stream =True): Logger=Logging.getlogger () Form
created
% (asctime) s The current time in string form. The default format is "2003-07-08 16:49:45,896". The comma is followed by milliseconds
% (thread) d thread ID. Probably not.
% (threadname) s thread name. Probably not.
% (process) d process ID. Probably not.
% (message) s user-output message
Logger object: Can print the file, also can output the screenImport logging# creates a logger object and is named log =
number 8 o'clock exits the programSys.exit ()Import Syscount = 1while count Output Result:12345678View Code(3) Sys.version methodDefinition: Get version information for the Pyhon interpreter(4) Sys.maxint methodDefinition: the largest int value(5) Sys.path methodDefinition: Returns the search path of the module, initializing the value using the PYTHONPATH environment variableExample 1:Import Sysprint (Sys.path) # Get the search path for the moduleOutput Result:['C:\\users\\william\\pycharmproje
Reference: http://blog.csdn.net/zyz511919766/article/details/25136485Logging moduleThe log has 5 levels, debug,info,warning,error,critical, respectively, and the corresponding level numbers are 10,20,30,40,50Output log standards to the screen#!/usr/bin/env python#-*-coding:utf-8-*-ImportLogginglogging.debug ("Welcome to Akon Debug System") Logging.info ("Welcome to Akon Info System") logging.warning ("Welcome to Akon Warning System") Logging.error ("W
A solution to memory leaks caused by misuse of logging modules in Python
This article mainly introduces to solve the memory leak caused by the misuse of logging module in Python, because of the problem caused by too much UDP connection, the friend need to refer to the
First introduced how to find it, the online proje
AddHandler () methodHandler.setlevel (LEL): Specifies the level of information being processed, and information below the LEL level is ignoredHandler.setformatter (): Choose a format for this HandlerHandler.addfilter (filt), Handler.removefilter (filt): Add or remove a filter objectMultiple handler can be attached to each logger. Next, let's introduce some common handler:1) logging. StreamhandlerUsing this handler, you can output information to any f
Several learning connections:Official Python Link:Https://docs.python.org/3.4/library/logging.html?highlight=loggingTranslation (2.3 version only)http://crazier9527.iteye.com/blog/290018Another person's summary:http://blog.csdn.net/fxjtoday/article/details/6307285Best to understand, write the best:Http://bbs.chinaunix.net/thread-3590256-1-1.htmlMy study summary is based on http://bbs.chinaunix.net/thread-3590256-1-1.html
Take a simple log system
http://www.cnblogs.com/dkblog/archive/2011/08/26/2155018.htmlMulti-module problem: Multiple modules use the Logginglogging module to ensure that within the same Python interpreter, multiple calls to Logging.getlogger ('log_name' ) will return the same logger instance,Even in the case of multiple modules. So the typical multi-module scenario using logging is to configure the
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.