Logging isPythonThe standard module that comes with up to 2.3 can be used to record logs from running programs. The logging module is very powerful and can flexibly output logs to various predefined or custom targets. Using the standard logging module,DjangoThe program can easily implement log output in the runtime environment, which is indispensable for the monitoring and debugging of program running in the development and deployment environments, so here I will summarize some of my experiences.
The basic settings for Django program to use logging output should be set in settings. configure the logging parameter in Py: Loggin. basicconfig (Level = logging. debug,
Format = '% (asctiome) S % (levelname) S % (module) S. % (funcname) s line: % (lineno) d % (Message) s ',
) Logging. basicconfig is a simple method provided by the logging module to configure logging parameters. After the preceding configuration, logs can be output in the Django program by using logging. debug, logging.info, and other methods. Logging. logs of debug and above levels are directly output to the current command window when Django is running. in the production environment, you only need to increase the logging output level to control the log output content, avoid outputting too much log Content. (For more information about the logging level and logging, see pydoc.) When you debug and use manage. py runserver locally, the logging content will appear directly on the console.
The basic settings for outputting logs to files can only be used to directly output logs to the command line window. The easiest way to output logs to a file for storage is logging. basicconfig (Level = logging. debug,
Format = '% (asctiome) S % (levelname) S % (module) S. % (funcname) s line: % (lineno) d % (Message) s ',
Filename = '/path/to/logfile/filelog. log',) in the logging. basicconfig method, if filename is specified, the log is directly output to the specified file.
To save log files cyclically by date in a production environment, you must not only write the logs to files, but also save the log files by date. Such tasks can also be easily implemented using the logging module. Settings. use the following settings in Py: Root = logging. getlogger () If Len (root. handlers) = 0 # avoid repeated level = logging. infofilename = '/path/to/logfile/filelog. log 'format = '% (asctiome) S % (levelname) S % (module) s. % (funcname) s line: % (lineno) d % (Message) s 'hdlr = timedrotatingfilehandler (filename, "Midnight", 1, 5) FMt = formatter (Format) hdlr. setformatter (FMT) root. addhandler (hdlr) root. setlevel (level) uses the predefined timedrot of the logging module. Atingfilehandler class, which is used to scroll the log file every night, while retaining up to five previous log files. Because special handler needs to be specified, the simple method of logging. basicconfig cannot be used here. More benefits of using the logging Module
Using the log function can be of great help for development and debugging. With the logging module, you can easily control the output at the log level without adding or removing any program code. You only need to change the logging-level line of code in settings. py. Therefore, logging. debug can be used as much as possible during the development process to output content helpful for debugging. In the production environment, you can easily analyze some problems in the real running environment through Logs to facilitate debugging and modification of bugs.
If you use the Google App Engine without specifying an output file, the logs output by the program will be saved in the App Engine Running logs. You can also use regular expressions to search for previous logs.
In addition, the Django debug toolbar allows you to conveniently view the output logs directly on each page in the debugging environment, which is very useful.
Therefore, if you are writing a Django program, do not use the original print. Using Logging can get twice the result with half the effort.