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 ,为这些修改创 ...
随机推荐
- LINUX 常用指令学习
目录 0 查找find 1 别名alias 2 变量的设置 3 常用的系统变量 4 通配符及组合按键 5 指令之间的分隔符(;&||) 6 输出重定向(>,>>,1>, ...
- BZOJ1396&2865 识别子串 【后缀自动机 + 线段树】
题目 输入格式 一行,一个由小写字母组成的字符串S,长度不超过10^5 输出格式 L行,每行一个整数,第i行的数据表示关于S的第i个元素的最短识别子串有多长. 输入样例 agoodcookcooksg ...
- oracle存储过程、声明变量、for循环
oracle存储过程.声明变量.for循环 1.创建存储过程 create or replace procedure test(var_name_1 in type,var_name_2 out t ...
- Codeforces Round #364 (Div. 2) A 水
A. Cards time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- 【一个比较bug free的二分写法】
lower_bound: [l, r)区间内大于等于val的第一个位置 int lower_bound(int l, int r, int val){ while(l < r){ ); if(a ...
- C++ Contest Code preprocessor
大概可以拿来方便拉模板 变量名.语法都是瞎整的你感觉有用随便用好了.. #include<bits/stdc++.h> using namespace std; map<string ...
- MongoDB 查询语法
转载 http://blog.163.com/lgh_2002/blog/static/440175262012052116455/ 详见官方的手册:http://www.mongodb.org/di ...
- Linux Mint---安装篇
先前接触过Debian6,埋头折腾已经是好几年前的事情了,后来就放下了,现在工作之余时间比较多,正好debian8.1出来了,于是乎装了个debian用,结果,自己其实还是一个菜蛋而已,连软件源怎么设 ...
- H5 <audio> 音频标签自定义样式修改以及添加播放控制事件
H5 <audio> 音频标签自定义样式修改以及添加播放控制事件 Dandelion_drq 关注 2017.08.28 14:48* 字数 331 阅读 2902评论 3喜欢 3 说明: ...
- 和菜鸟一起学android4.0.3源码之lcd屏幕背光调节
周六的中午还是依旧来了公司,本来也没有打算来的,既然来了,那就把上次遗留下来的一些问题给解决吧,把android下的pwm调lcd背光给总结下吧.关于android的背光,是用pwm波来控制的,通过占 ...