django中日志使用学习记录
在setting中加入以下代码
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
},
'standard': {
'format': '%(levelname)s %(asctime)s %(message)s'
},
'simple': {
'format': '%(levelname)s %(message)s'
},
},
'filters': {
},
'handlers': {
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
'formatter':'standard',
},
'my_handler': {
'level': 'WRANING',
'class': 'logging.handlers.RotatingFileHandler',
'filename': '/logging/my_handler.log',
'formatter': 'standard',
},
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
'my_logger': {
'handlers': ['my_handler'],
'level': 'WARNNING',
'propagate': False,
},
},
}
简化版
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'standard': {
'format': '%(levelname)s %(asctime)s %(message)s'
},
},
'filters': {
},
'handlers': {
'default': {
'level': 'WRANING',
'class': 'logging.handlers.RotatingFileHandler',
'filename': 'os.path.join(BASE_DIR+'/static/logs/','all.log')',
'formatter': 'standard',
},
},
'loggers': {
'default': {
'handlers': ['default'],
'level': 'WARNNING',
'propagate': False,
},
},
}
使用import logging
logger = logging.getLogger('default')
logger.warnning('test')
version表示版本,一般不用改
disable_existing_loggers表示弃用已经存在的日志,True表示弃用,False表示不弃用。
fomatters表示多个格式化格式,verbose表示详细的格式化,包括文件名,时间,模块,进程,线程,信息,standard表示标准的格式化包括文件名,时间和信息,simple就是简单的只有文件名和信息
filters表示过滤器,如果有需要可以添加,如何添加查看官方文档,一般不需要。
handlers表示处理日志程序定义了两个一个是把日志发送给站点管理员,一个是自定义的。level表示等级,一共五个
DEBUG:用于调试目的的底层系统信息
INFO:普通的系统信息
WARNING:表示出现一个较小的问题。
ERROR:表示出现一个较大的问题。
CRITICAL:表示出现一个致命的问题。
class表示的意思大概是python自带的处理程序吧,filename表示文件位置,formater表示使用哪种格式
以下来自官方文档,翻译的不是很准确,rotate不明白啥意思
In addition to the base Handler class, many useful subclasses are provided:出了基础的handler类,还提供了许多有用的子类
StreamHandlerinstances send messages to streams (file-like objects).实例对象,发送信息给流对象类似文件的对象FileHandlerinstances send messages to disk files.实例对象,发送信息给硬盘文件BaseRotatingHandleris the base class for handlers that rotate log files at a certain point. It is not meant to be instantiated directly. Instead, useRotatingFileHandlerorTimedRotatingFileHandler.基础的类,回溯日志文件到某个特定的点,一般不直接使用,而是使用其他两个类代替RotatingFileHandlerinstances send messages to disk files, with support for maximum log file sizes and log file rotation.实例对象,发送信息给硬盘文件,支持最大日志文件大小和日志文件回溯TimedRotatingFileHandlerinstances send messages to disk files, rotating the log file at certain timed intervals.实例对象,发送信息给硬盘文件,隔一段时间回溯到日志文件某个点SocketHandlerinstances send messages to TCP/IP sockets.实例对象,发送信息给TCP/IP套接字DatagramHandlerinstances send messages to UDP sockets.实例对象,发送信息给UDP套接字SMTPHandlerinstances send messages to a designated email address.实例对象,发送给特定的email地址SysLogHandlerinstances send messages to a Unix syslog daemon, possibly on a remote machine.实例对象,发送日志给unix中syslog守护进程,发送到远程unix机器上使用NTEventLogHandlerinstances send messages to a Windows NT/2000/XP event log.实例对象,发送信息给window event日志MemoryHandlerinstances send messages to a buffer in memory, which is flushed whenever specific criteria are met.实例对象,发送信息给内存的缓冲,满足特定条件将刷新HTTPHandlerinstances send messages to an HTTP server using eitherGETorPOSTsemantics.实例对象,发送信息给使用post或get的http服务WatchedFileHandlerinstances watch the file they are logging to. If the file changes, it is closed and reopened using the file name. This handler is only useful on Unix-like systems; Windows does not support the underlying mechanism used.实例对象,查看登录文件。如果文件更改,则使用文件名关闭并重新打开。此处理程序仅适用于类Unix系统; Windows不支持使用的底层机制NullHandlerinstances do nothing with error messages. They are used by library developers who want to use logging, but want to avoid the ‘No handlers could be found for logger XXX’ message which can be displayed if the library user has not configured logging. See Configuring Logging for a Library for more information实例对象,不对错误信息做处理。想要使用日志记录的图书馆开发人员,但是希望避免如果库用户没有配置日志记录,则可以显示“无记录器XXX的处理程序”消息。的时候使用
New in version 2.7: The NullHandler class.
The NullHandler, StreamHandler and FileHandler classes are defined in the core logging package. The other handlers are defined in a sub- module, logging.handlers. (There is also another sub-module, logging.config, for configuration functionality.)
Logged messages are formatted for presentation through instances of the Formatter class. They are initialized with a format string suitable for use with the % operator and a dictionary.
For formatting multiple messages in a batch, instances of BufferingFormatter can be used. In addition to the format string (which is applied to each message in the batch), there is provision for header and trailer format strings.
When filtering based on logger level and/or handler level is not enough, instances of Filter can be added to both Logger and Handler instances (through their addFilter() method). Before deciding to process a message further, both loggers and handlers consult all their filters for permission. If any filter returns a false value, the message is not processed further.
The basic Filter functionality allows filtering by specific logger name. If this feature is used, messages sent to the named logger and its children are allowed through the filter, and all others dropped.
loggers是日志记录器,用来记录日志,handler表示使用哪个的处理日志程序,level表示等级同上,propergate表示是否向上传递错误信息
默认情况下,Django 的logging 配置如下:
当DEBUG 为True 时:
django的全局logger会向控制台发送级别等于或高于INFO的所有消息。Django 此时不会做任何日志调用(所有的日志要么为DEBUG级别,要么被django.request 和django.security logger 处理)。
py.warnings logger,它处理来自warnings.warn()的消息,会向控制台发送消息。
当DEBUG 为False 时:
django.request 和django.security loggers 向AdminEmailHandler发送带有ERROR 或 CRITICAL级别的消息。这些logger 会忽略任何级别等于或小于WARNING的信息,被记录的日志不会传递给其他logger(它们不会传递给django的全局 logger,即使DEBUG 为 True)。
另见配置日志来了解如何补充或者替换默认的日志配置。
django中日志使用学习记录的更多相关文章
- Django中日志管理
在settings中设置日志的相关信息,然后再逻辑代码区就可以保存相应的信息了 #简单设置: LOGGING = { 'version': 1, 'disable_existing_loggers': ...
- django中日志配置
# ======日志配置====== # 错误优先级:NOTSET < DEBUG < INFO < WARNING < ERROR < CRITICAL # Djang ...
- python 机器学习中的数据处理学习记录
在机器学习中,选择合适的算法固然重要,但是数据的处理也同样重要.通过对数据的处理,能提高计算效率,提高预测识别精确度等等 以下记录下一些数据处理的方法 一.处理缺失值 对于数据集中有缺失值的,粗暴的方 ...
- Django中ORM之查询表记录
查询相关API from django.db import models # Create your models here. class Book(models.Model): title = mo ...
- Django中ORM之操作表记录
添加表记录 添加普通字段 #方法一 book_obj = Book(title='book7',publishDate='2011-05-02',price=200,publish_id=1) boo ...
- django中对数据库生成记录操作失败
在终端执行以下语句时,会发现一点效果也没有,但是在manage.py中会成功: python3 manage.py makemigrations # 仅仅是在小本本上(migrations文件夹)记录 ...
- django前后端数据传输学习记录
在开发过程中会遇到这样的情况 后台返回了一堆的数据,是一个列表 例如 datas = [{"a":1, "b":2}, {"c": 3,&q ...
- 在MVC中使用NHibernate学习记录
NHibernate简介: NHibernate是一个面向.net环境的对象/关系数据库映射工具,对象/关系数据库映射(object/relational mapping,ORM)是一种技术,可以将对 ...
- [Django]模型学习记录篇--基础
模型学习记录篇,仅仅自己学习时做的记录!!! 实现模型变更的三个步骤: 修改你的模型(在models.py文件中). 运行python manage.py makemigrations ,为这些修改创 ...
随机推荐
- vim中插入递增数
假设生成0-9的递增数 1.插入数字1,yy复制,9p 2.输入命令 let i= | g//s//\=i/ | let i=i+1 3.结果:
- redis 集群分配哈希曹
重新分配哈希曹: ip:port 为当前redis集群任意节点ip和port redis-cli --cluster reshard ip:port 操作如图: 分配哈希槽有两种方式: 1.在其他节点 ...
- Unity --yield return
1.yield return null; //暂停协同程序,下一帧再继续往下执行 yield new WaitForFixedUpdate (); //暂停协同程序,等到下一次调用FixedUpdat ...
- 【转】unity自带寻路Navmesh入门教程(二)
http://liweizhaolili.blog.163.com/blog/static/16230744201271210237616/ 上一节简单介绍了NavMesh寻路的基本用法,这次来介绍一 ...
- String类型的XML文件的格式化
在接收到的xml报文中,未经过格式化,不好看 package org.zln.xml.format; import org.dom4j.Document; import org.dom4j.Docum ...
- HDU 3395 Special Fish(拆点+最大费用最大流)
Special Fish Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- Linux 程序编译过程的来龙去脉
大家肯定都知道计算机程序设计语言通常分为机器语言.汇编语言和高级语言三类.高级语言需要通过翻译成机器语言才能执行,而翻译的方式分为两种,一种是编译型,另一种是解释型,因此我们基本上将高级语言分为两大类 ...
- JAVA File方法文本复制读写-解决中文乱码
import java.io.*; public class TextFile { public static void main(String[] args) throws Exception { ...
- 多啦A梦的制作
小叮当简单颜色单一,操作起来也很容易上手.接下来的一个实例就是用css画出一个多啦A梦,首先将其分为头部,和身体.然后,再根据身体各部分细节进行进一步的具体刻画. 由于最近一直在学习JavaWeb方面 ...
- hdu 3535 背包综合题
/* 有n组背包,每组都有限制 0.至少选一项 1.最多选一项 2.任意选 */ #include <iostream> #include <cstdio> #include ...