One, log levelThe log is divided into 5 levels, from low to High, respectively: DEBUG INFO WARNING ERROR CRITICAL.DEBUG: Detailed information, usually appearing only on diagnostic issuesINFO: Make sure everything works as expectedWARNING: A sign that some unexpected things have happened, or indicate some problems in the near future (for example. Low disk space "). The software will work as expected.ERROR: A more serious problem, the software does not
Common modules1 Logging ModuleLog level: Noset (not set)Debug---(debug information)----can also be expressed as 10info--(Message information)----can also be expressed as 20Warning---(warning message)----can also be expressed as 30The error----------can also be expressed as 40Critical---(Critical error)---can also be expressed as 50Default level is warning, default print to Terminal1 ImportLogging2 3Logging.debug ('Debugging Debug')4Logging.info ('Mess
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 =
Logging.basicconfig configuration logging to file A, and then using Logging.filehandler to generate records to file B loggerWhen using this logger log, both files A and B are logged simultaneously, and it feels like the mechanism is quite unexpected.#-*-Coding:utf8-*-import logginglogging.basicconfig ( level=logging. DEBUG, format= ' [% (asctime) s][% (process) d:% (thread) d][% (levelname) s
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
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
be used to throw exceptions and abstract classes + abstract methods to constrainSecond, the encryption mechanismOur password only we know others do not know, but in Python the existence of the password is too exposed, we need a way to make the password complex can not be cracked1 ImportHashlib#to import the encryption module first2 3obj = Hashlib.md5 (b"ASDAOIJSO")#add salt to prevent the collision of the library, resulting in password leakage4Obj.up
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
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
logs are also exported to three destinations defined by handler. If you call Logging.getloggeer (' Simpleexample '), the logger instance that matches the name ' Simpleexample ' in the configuration file is created, and its handler is console, which is the output log on the console.Logging.handlers.RotatingFileHandler can implement automatic log file coverage, if you need to implement log file changes (such as deleted) automatically generated, you need to use Logging.handlers.WatchedFileHandler,
logger with a different name, each time it is a new logger, there is no problem adding multiple handler. PS: That's a stupid way to do it, but that's what I did before. )
Call RemoveHandler () to remove the handler from the logger after each log has been recorded as above.
In the log method, if the logger already has handler, the handler is no longer added.
Same as Method 2, but remove the handler from the Logger handler list with pop.
The following is a code example of Method
Python's logging module is used to write logs and is a standard Python module.Structure of the logging
View logging python source code, there are four main classes to realize the function;
Loggers: Provides interfaces directly used by the application,
located
% (created) f
Current time, represented by the UNIX standard floating-point number representing the time
% (relativecreated) d
The number of milliseconds since logger was created when the log information is output
% (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
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
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.