Below for you to share a Django Use logging print log instance, has a very good reference value, I hope to help you. Come and see it together.
Django uses Python's own logging as a log printing tool. A brief introduction to the next logging.
Logging is thread-safe and consists mainly of 4 parts:
Logger
The direct interface used by the user to pass the log to handler
Handler
Control log output to where, console,file ...
A logger can have multiple handler
Filter
control which logs can flow from logger to handler
Formatter
Format of the control log
The user obtains the logger instance using Logging.getlogger ([name]).
If there is no name, return to root logger (root logger) in the logger hierarchy. Calling the function with the same name always returns the same logger instance. This means that the logger instance does not need to be passed between parts of the application.
Django uses logging in the settings file to customize the log output (including defining logger, handler, formatter, etc.)
For example, the settings file is defined as follows:
LOGGING = {' Version ': 1, ' disable_existing_loggers ': False, ' formatters ': { ' verbose ': {' format ': ' [% (Asctime] S] [% (levelname) s]% (message) s ' }, ' handlers ': { ' console ': {' Level ': ' INFO ', ' class ': ' Logging. Streamhandler ', ' formatter ': ' Verbose ' }, ' file ': { ' level ': ' INFO ', ' class ': ' Logging. Filehandler ', ' filename ': ' D:/monitor.log ', ' formatter ': ' Verbose ' }, ' email ': { ' level ' : ' ERROR ', ' class ': ' Django.utils.log.AdminEmailHandler ', ' include_html ': True, }}, ' loggers ': { ' Django ': { ' handlers ': [' console ', ' file ', ' email ', ' level ': ' INFO ', ' propagate ': True, },},}
Print the log in the code:
Logger = Logging.getlogger (' Django ') Logger.info ("This was an error msg")
[2017-07-15 17:44:51,316] [ERROR] This is an error msg
The log is then printed to the terminal and to the file.
For more information about Django logging, refer to our website
https://docs.djangoproject.com/en/1.11/topics/logging/