Reprint: http://www.cnblogs.com/goodhacker/p/3355660.htmlThe log system in the Python standard library is supported from Python2.3. As long as import logging This module can be used. If you want to Develop a log system, you need to output the log to the console and write to the log file, as long as you use:1ImportLogging23#Create a Logger4 Logger = Logging.getlogger (‘MyLogger‘)5Logger.setlevel (logging
I think it's pretty good. I 've talked about all the basic skills.
The following describes how to use it.CodeIt is easy to write:
1 def initlog ():2 Import Logging34 logger = logging. getlogger ()5 hdlr = logging. filehandler (logfile)6 formatter = logging. formatter ('% (asctime) S % (levelname) S % (Message) s ')
(errormessage);W.writeline ("________________________________________________________");W.flush ();W.close ();}}catch (Exception ex){Writeerror (ex. message);}}
Add the following code to the Application_Error of the Web Global.asax file
Copy Code code as follows:
void Application_Error (object sender, EventArgs e)
{
Code that runs when an unhandled error occurs
Exception objerr = Server.GetLastError (). GetBaseException ();
Log the IP address where the error occurred
be recorded in the file. In this example, the first log will not be recorded. if you want to record the debug log, you can change the log level to DEBUG.
If you want to print logs on the screen and file logs at the same time, you need to understand a little complicated knowledge:
The logging library takes a modular approach and offers several categories of components: loggers, handlers, filters, and formatters.
Loggers expose the interface that app
#coding: Utf-8import loggingclass Testlog (object): ' Logging ' def __init__ after encapsulation (self, logger = None): ' "Specifies the file path to save the log, the log level, and the call file to deposit the log into the specified file ' # ' creates a logger Self.logger = logging. GetLogger (Logger) self.logger.setLevel (
Support for django1.3 and later versions...
I did not get it out for a long time after I started with 1.2.5. It turned out to be a version problem. It was really depressing...
Configure logging in settings. py
Logging = {'Maxbytes ': 1024*1024*5, #5 MB [Python] View plaincopyprint?
Log = logging. getlogger
in 0.1s] Results of Err.log: cat err.log2015-04-02 17:03:29,421 - Err_Logger - DEBUG - just a debug - 292015-04-02 17:03:29,423 - Err_Logger - INFO - just a info - 302015-04-02 17:03:29,423 - Err_Logger - WARNING - waring,please check out - 312015-04-02 17:03:29,423 - Err_Logger - ERROR - error,oh no - 322015-04-02 17:03:29,423 - Err_Logger - CRITICAL - critical,it doesn‘t work - 33 2. Logging detailed2.1 Logger: Provides a log interface for use in c
service is %s!‘, service_name, ‘down‘) # 多参数格式化logger.error(‘{} service is {}‘.format(service_name, ‘down‘)) # 使用format函数,推荐# 2016-10-08 21:59:19,493 ERROR : Booking service is down!Log exception informationWhen you use the logging module to record exception information, you do not need to pass in the exception object, as long as you directly call logger.error() or you logger.exception() can record the current exception.# 记录异常信息try: 1 / 0except: #
layering.
We can uselogging.getLogger() Create a recorder. This character passes a parameter to getLogger, which can be defined by a dot-separated element.
For example,logging.getLogger(“parent.child”) A "child" recorder will be created. Its parent-level recorder is called "parent. the recorder is a global object managed by the logging module, so we can conveniently retrieve them anywhere in the project.
R
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 module is not used properly.
We previously had suc
,formatter;fileconfig is configured by a file, while listen listens on a network port and configures it by receiving network data. Of course, in addition to the above collectivization configuration, you can also directly invoke the methods in the Logger,handler and other objects in the code to explicitly configure.
Use the GetLogger function in the global scope of the logging module to get an instance o
. setLevel (logging. INFO) formatter = logging. formatter ('% (name)-12 s: % (levelname)-8 s % (message) s') console. setFormatter (formatter) logging. getLogger (''). addHandler (console)
Insert the code above to print logs on the screen at the same time.
Screen Printing:
Root: INFO This is info message
Root: WARNING
Python outputs log logging to the console and files at the same time,
This example describes how to export logs to the console and files simultaneously by using Python. Share it with you for your reference. The details are as follows:
Python provides a very convenient Log Module for outputting logs to the console and files at the same time.
#-*-Coding: UTF-8-*-import logging # configure log information
open debug, debug will not go to the syslog, so there will not be too many UDP connections, you will not know that there are memory leaks, let's see why this leads to memory leaks, first look at the GetLogger code:
?
1 2 3 4 5 6 7 8 9 10
def getlogger (Name=none): "" "return a logger with the specified name, creating it if necessary. If No name is specified, return the root logger. ' "' I
python logging modules can be confusing placesfind some interesting phenomena by seeing the code of the Python logging module:1. The logging object is actually a tree structure, and each created logging object is the child node of the root logging object. When constructing
Java programmer's cultivation path: Logging (2/3)-How to Write logs1. A basic example of using the Logging framework to write logs is basically three steps.Introduce the loggerg class and logger factory class declaration logger record logs
The following is an example.
// 1. introduce the Logger and LoggerFactoryimport org of the slf4j interface. slf4j. logger; import org. slf4j. loggerFactory; public class
directory, and two versions of flogging are available for project global use.
The main functions used are:
1, Mustgetlogger ()
function definition
MustGetLogger(module string) *Logger
Use to create a logger object for the ACA.
var acaLogger = logging.MustGetLogger("aca")
Mustgetlogger is like GetLogger (GetLogger creates and returns logger objects based on the module name), but if logge
logging it is necessary to understand the concept of logger,handler,formatter,filterLogging.debug (), Logging.info (), logging.warning (), Logging.error (), logging.critical () (for logging different levels of log information, respectively)Logging.basicconfig () (using the default log format (Formatter) to establish a default stream processor (Streamhandler) for the lo
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.