日志配置

#!/usr/bin/python2.7

import os
import logging def get_logger(path='./', filename='access.log', level='debug'):
log_level = {
'debug':logging.DEBUG,
'info':logging.INFO,
'warning':logging.WARNING,
'error':logging.ERROR,
'critical':logging.CRITICAL
}
level = 'debug' if level not in log_level else level file = os.path.join(path,filename)
formatter = logging.Formatter('"%(asctime)s" "%(filename)s[line:%(lineno)d]" "%(levelname)s" "%(message)s"')
handler = logging.FileHandler(file, mode='a')
# handler.setLevel(log_level[level])
handler.setFormatter(formatter) logger = logging.getLogger(__name__)
logger.setLevel(log_level[level])
logger.addHandler(handler) return logger

注意logger.setLevel 和 handler.setLevel 是不一样的。

给日志每个字段加上双引号,方便后面结合awk进行统计分析

awk -F \" '{print $2}' analysis.log

项目里的应用

定义

import logging

logger = logging.getLogger("HorizonLog")  # 指定一个名称
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s :: %(process)s :: %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
handler = logging.FileHandler(settings.LOG_PATH + '/horizon.log')
handler.setFormatter(formatter)
logger.addHandler(handler)

使用

# Return a logger with the specified name or,
# if no name is specified, return a logger which is the root logger of the hierarchy. logger = logging.getLogger("HorizonLog") logger.info(...)
logger.warning(...)
logger.error(...)

logging模块简单使用的更多相关文章

  1. logging模块简单用法

    logging模块功能比较多,但一般情况下使用其简单功能就已经足够了. 最简单的用法如下: import logging logging.baiscConfig(level=logging.DEBUG ...

  2. python之logging模块简单用法

    前言: python引入logging模块,用来记录自己想要的信息.print也可以输入日志,但是logging相对print来说更好控制输出在哪个地方.怎么输出以及控制消息级别来过滤掉那些不需要的信 ...

  3. python日志记录-logging模块

    1.logging模块日志级别 使用logging模块简单示例: >>>import logging >>>logging.debug("this's a ...

  4. Python的 Datetime 、 Logging 模块

    Datetime模块 datetime是python处理时间和日期的标准库 类名 date类        日期对象,常用的属性有 year . month . day time类        时间 ...

  5. Python Logging模块的简单使用

    前言 日志是非常重要的,最近有接触到这个,所以系统的看一下Python这个模块的用法.本文即为Logging模块的用法简介,主要参考文章为Python官方文档,链接见参考列表. 另外,Python的H ...

  6. python中logging模块的一些简单用法

    用Python写代码的时候,在想看的地方写个print xx 就能在控制台上显示打印信息,这样子就能知道它是什么了,但是当我需要看大量的地方或者在一个文件中查看的时候,这时候print就不大方便了,所 ...

  7. logging 模块误用导致的内存泄露

    首先介绍下怎么发现的吧, 线上的项目日志是通过 logging 模块打到 syslog 里, 跑了一段时间后发现 syslog 的 UDP 连接超过了 8W, 没错是 8 W. 主要是 logging ...

  8. python logging模块详解[转]

    一.简单将日志打印到屏幕: import logging logging.debug('debug message') logging.info('info message') logging.war ...

  9. python logging模块

    1.logging模块提供了四个组件logger:日志类,有两个功能1)配置日志的等级,处理器handler,过滤器filterlogger.setLevel(logging.INFO)logger. ...

随机推荐

  1. LeetCode算法题-Design HashSet(Java实现)

    这是悦乐书的第298次更新,第317篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第166题(顺位题号是705).不使用任何内建的hash表库设计一个hash集合,应包含 ...

  2. Error:Cannot run program "svn" (in directory "E:demo\Hello"): CreateProcess error=2,

    file-->settings-->version controller --> subversion

  3. 关于模块安装及cmd安装pip3模块失败的 Read timed out.的补救方法

    自己在安装pip中的request模块时,安装到一半老是报错.我看了下报错的代码最后一句写的是 Read timed out. 就是读取超时,从网上查了一下,原因是由于中国的网比较慢,下载超时.需要在 ...

  4. springboot项目屏蔽mq或者mongodb的监控日志输出

    最近写项目,用的是springboot,其中用到了rabbitmq和mongodb,配置完成 项目启动后,会输出如下日志: mongodb和mq的检测,会一直打印日志,这样会影响开发人员的测试. 如何 ...

  5. kunbernetes-快速入门

    1.入门概述 本文以在容器云上部署一个nexus3应用为例,通过通过一步步的操作,帮助用户快速的对Kubernetes有一个快速和整体的认识.通过快速入门,可以提供如下知识内容: 在集群中部署一个容器 ...

  6. 使用idea搭建Scala 项目

    主要内容 Intellij IDEA开发环境简介 Intellij IDEA Scala开发环境搭建 Intellij IDEA常见问题及解决方案 Intellij IDEA常用快捷键 1. Inte ...

  7. linux -- 添加、修改、删除路由

    在日常的使用中,或者在服务器中,有两个网卡配置两个地址,访问不同的网络段,这种情况是非常常见的现象,但是,我们需要额外的添加路由表来决定发送的数据包经过正确的网关和interface才能正确的进行通信 ...

  8. identity server4 证书

    我们需要对token进行签名, 这意味着identity server需要一对public和private key. 幸运的是, 我们可以告诉identity server在程序的运行时候对这项工作进 ...

  9. 在Mac OS X中完善PHP环境:memcache、mcrypt、igbinary

    本文环境: Mac OS X 10.8.5 Xcode 5.0 Mac OS X升级到10.8.5之后,内置的Apache升级到2.2.24,PHP升级到了5.3.26.本文以此环境为基础. 本文简介 ...

  10. Python exe2shellcode,shellcode2exe

    exe2shellcode #! /usr/bin/env python # -*- coding: utf-8 -*- import os import sys def payload(files) ...