在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类,还提供了许多有用的子类

  1. StreamHandler instances send messages to streams (file-like objects).实例对象,发送信息给流对象类似文件的对象
  2. FileHandler instances send messages to disk files.实例对象,发送信息给硬盘文件
  3. BaseRotatingHandler is the base class for handlers that rotate log files at a certain point. It is not meant to be instantiated directly. Instead, use RotatingFileHandler or TimedRotatingFileHandler.基础的类,回溯日志文件到某个特定的点,一般不直接使用,而是使用其他两个类代替
  4. RotatingFileHandler instances send messages to disk files, with support for maximum log file sizes and log file rotation.实例对象,发送信息给硬盘文件,支持最大日志文件大小和日志文件回溯
  5. TimedRotatingFileHandler instances send messages to disk files, rotating the log file at certain timed intervals.实例对象,发送信息给硬盘文件,隔一段时间回溯到日志文件某个点
  6. SocketHandler instances send messages to TCP/IP sockets.实例对象,发送信息给TCP/IP套接字
  7. DatagramHandler instances send messages to UDP sockets.实例对象,发送信息给UDP套接字
  8. SMTPHandler instances send messages to a designated email address.实例对象,发送给特定的email地址
  9. SysLogHandler instances send messages to a Unix syslog daemon, possibly on a remote machine.实例对象,发送日志给unix中syslog守护进程,发送到远程unix机器上使用
  10. NTEventLogHandler instances send messages to a Windows NT/2000/XP event log.实例对象,发送信息给window event日志
  11. MemoryHandler instances send messages to a buffer in memory, which is flushed whenever specific criteria are met.实例对象,发送信息给内存的缓冲,满足特定条件将刷新
  12. HTTPHandler instances send messages to an HTTP server using either GET or POST semantics.实例对象,发送信息给使用post或get的http服务
  13. WatchedFileHandler instances 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不支持使用的底层机制
  14. NullHandler instances 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 NullHandlerStreamHandler 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中日志使用学习记录的更多相关文章

  1. Django中日志管理

    在settings中设置日志的相关信息,然后再逻辑代码区就可以保存相应的信息了 #简单设置: LOGGING = { 'version': 1, 'disable_existing_loggers': ...

  2. django中日志配置

    # ======日志配置====== # 错误优先级:NOTSET < DEBUG < INFO < WARNING < ERROR < CRITICAL # Djang ...

  3. python 机器学习中的数据处理学习记录

    在机器学习中,选择合适的算法固然重要,但是数据的处理也同样重要.通过对数据的处理,能提高计算效率,提高预测识别精确度等等 以下记录下一些数据处理的方法 一.处理缺失值 对于数据集中有缺失值的,粗暴的方 ...

  4. Django中ORM之查询表记录

    查询相关API from django.db import models # Create your models here. class Book(models.Model): title = mo ...

  5. Django中ORM之操作表记录

    添加表记录 添加普通字段 #方法一 book_obj = Book(title='book7',publishDate='2011-05-02',price=200,publish_id=1) boo ...

  6. django中对数据库生成记录操作失败

    在终端执行以下语句时,会发现一点效果也没有,但是在manage.py中会成功: python3 manage.py makemigrations # 仅仅是在小本本上(migrations文件夹)记录 ...

  7. django前后端数据传输学习记录

    在开发过程中会遇到这样的情况 后台返回了一堆的数据,是一个列表 例如 datas = [{"a":1, "b":2}, {"c": 3,&q ...

  8. 在MVC中使用NHibernate学习记录

    NHibernate简介: NHibernate是一个面向.net环境的对象/关系数据库映射工具,对象/关系数据库映射(object/relational mapping,ORM)是一种技术,可以将对 ...

  9. [Django]模型学习记录篇--基础

    模型学习记录篇,仅仅自己学习时做的记录!!! 实现模型变更的三个步骤: 修改你的模型(在models.py文件中). 运行python manage.py makemigrations ,为这些修改创 ...

随机推荐

  1. 【Python】locust框架接口性能测试(一)

    本人工作中主要对接口与web进行性能测试,而接口测试主要为http协议接口和webservice接口,本文主要对locust框架http接口测试先进行简单介绍. 1.测试需求 对某系统登录接口进行测试 ...

  2. Leetcode 532.数组中的K-diff数对

    数组中的K-diff数对 给定一个整数数组和一个整数 k, 你需要在数组里找到不同的 k-diff 数对.这里将 k-diff 数对定义为一个整数对 (i, j), 其中 i 和 j 都是数组中的数字 ...

  3. java 四舍五入 保留两位小数

    1. 格式化字符串 java.text.DecimalFormat df = new java.text.DecimalFormat("#0.00"); float val=Flo ...

  4. 【转】Visual Studio 2013 Tools for Unity安装目录,Visual Studio 2013 Tools.unitypackage

    http://blog.csdn.net/dynastyting/article/details/46505349 Visual Studio 2013 Tools for Unity安装目录 D:\ ...

  5. Struts2,get/set 自动获取/设置数据ActionSupport 类

    主页:http://struts.apache.org/在用户请求和模块化处理方面以及页面的展现这块,Struts2 发挥了强大的作用:相对于传统的Jsp+Servlet 模式,Struts2 更适合 ...

  6. linux下解压zip文件

    linux下解压zip文件 linux自带的unzip命令可以解压windows下的zip格式的压缩文件. unzip命令 语法:unzip [选项] 压缩文件名.zip 各选项的含义分别为: -x ...

  7. this关键字、this()、super()

    对于下面的代码怎么区分是哪个对象调用当前方法: Class Banana { void peel(int i); } publci Class BananaPeel { public static v ...

  8. 关于getSystemResource, getResource 的总结

    项目中, 有时候要读取当前classpath下的一些配置文件. 之前用的读取配置文件的代码如下 public static Properties loadPropertiesFile(String f ...

  9. 关于main函数的参数

    #include <stdio.h> int main(int argc, char const *argv[]) { int i; for ( i = 0; i < argc; i ...

  10. LeetCode OJ-- N-Queens **

    https://oj.leetcode.com/problems/n-queens/ n皇后问题,1皇后有1个解,4皇后2个解,8皇后也有解…… 每个皇后不能在同一行上,同一列上,以及同一条45度线上 ...