Django uses logging to print logs
The following is an example of Django using logging to print logs. It has a good reference value and I hope it will be helpful to everyone. Let’s take a look together
Django uses python’s own logging as a log printing tool. Let’s briefly introduce logging.
logging is thread-safe, and it mainly consists of 4 parts:
Logger
The direct interface used by users, Pass the log to Handler
Handler
Control where the log is output, console, file...
A logger can have Multiple Handler
Filter
Control which logs can flow from the logger to the Handler
##Formatter
Control the format of the logUsers use logging.getLogger([name]) to obtain the logger instance. If there is no name, return the root logger (root logger) in the logger hierarchy. Calling this function with the same name always returns the same logger instance. This means that logger instances do not need to be passed around between various parts of the application. Django customizes log output by using LOGGING in the settings file (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 log in code:
logger = logging.getLogger(‘django') logger.info(“This is an error msg”)
[2017-07-15 17:44:51,316] [ERROR] This is an error msg
In this way, the log is printed to the terminal and file.If you want to know more about django logging, please refer to the official website
https://docs.djangoproject.com/en/1.11/topics/logging/Related recommendations:
How Django loads css and js files and static images
The above is the detailed content of Django uses logging to print logs. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The logs of win10 can help users understand the system usage in detail. Many users must have encountered log 6013 when looking for their own management logs. So what does this code mean? Let’s introduce it below. What is win10 log 6013: 1. This is a normal log. The information in this log does not mean that your computer has been restarted, but it indicates how long the system has been running since the last startup. This log will appear once every day at 12 o'clock sharp. How to check how long the system has been running? You can enter systeminfo in cmd. There is one line in it.

Steps to check the Django version: 1. Open a terminal or command prompt window; 2. Make sure Django has been installed. If Django is not installed, you can use the package management tool to install it and enter the pip install django command; 3. After the installation is complete , you can use python -m django --version to check the Django version.

Django and Flask are both leaders in Python Web frameworks, and they both have their own advantages and applicable scenarios. This article will conduct a comparative analysis of these two frameworks and provide specific code examples. Development Introduction Django is a full-featured Web framework, its main purpose is to quickly develop complex Web applications. Django provides many built-in functions, such as ORM (Object Relational Mapping), forms, authentication, management backend, etc. These features allow Django to handle large

The logs of win10 can help users understand the system usage in detail. Many users must have seen a lot of error logs when looking for their own management logs. So how to solve them? Let’s take a look below. . How to solve win10 log event 7034: 1. Click "Start" to open "Control Panel" 2. Find "Administrative Tools" 3. Click "Services" 4. Find HDZBCommServiceForV2.0, right-click "Stop Service" and change it to "Manual Start "

Django is a complete development framework that covers all aspects of the web development life cycle. Currently, this framework is one of the most popular web frameworks worldwide. If you plan to use Django to build your own web applications, then you need to understand the advantages and disadvantages of the Django framework. Here's everything you need to know, including specific code examples. Django advantages: 1. Rapid development-Djang can quickly develop web applications. It provides a rich library and internal

How to upgrade Django version: steps and considerations, specific code examples required Introduction: Django is a powerful Python Web framework that is continuously updated and upgraded to provide better performance and more features. However, for developers using older versions of Django, upgrading Django may face some challenges. This article will introduce the steps and precautions on how to upgrade the Django version, and provide specific code examples. 1. Back up project files before upgrading Djan

How to check the django version: 1. To check through the command line, enter the "python -m django --version" command in the terminal or command line window; 2. To check in the Python interactive environment, enter "import django print(django. get_version())" code; 3. Check the settings file of the Django project and find a list named INSTALLED_APPS, which contains installed application information.

In Linux systems, you can use the following command to view the contents of the log file: tail command: The tail command is used to display the content at the end of the log file. It is a common command to view the latest log information. tail [option] [file name] Commonly used options include: -n: Specify the number of lines to be displayed, the default is 10 lines. -f: Monitor the file content in real time and automatically display the new content when the file is updated. Example: tail-n20logfile.txt#Display the last 20 lines of the logfile.txt file tail-flogfile.txt#Monitor the updated content of the logfile.txt file in real time head command: The head command is used to display the beginning of the log file
