Django uses logging to print an instance of the log

Source: Internet
Author: User
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/

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.