Python Logger使用
1. 单文件的logging配置
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='znifeng.log',
filemode='w')
2. 项目的logger配置(一个logger配置多个handler以实现输出不同级别的日志到不同文件)
#!/usr/bin/python
# -*- coding: utf-8 -*
__author__ = 'zni.feng'
import os
# mac 上logging的源码路径 /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging
import logging
import logging.handlers class Logger:
def __init__(self, name="root"):
self._logger = logging.getLogger(name)
#def __init__(self, fmt=None, datefmt=None)
formatter = logging.Formatter('[%(asctime)s] [%(levelname)s] - %(filename)s(line:%(lineno)d) - %(message)s', '%Y-%m-%d %H:%M:%S') #class logging.handlers.TimedRotatingFileHandler(filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False)
handler_info = logging.handlers.TimedRotatingFileHandler('logs/info.log', when='D', interval=1)
handler_warn = logging.handlers.TimedRotatingFileHandler('logs/warn.log', when='D', interval=1) handler_info.setFormatter(formatter)
handler_warn.setFormatter(formatter) #当handler的log level高于logger本身的log level时,此设置才会生效
handler_info.setLevel(logging.INFO)
handler_warn.setLevel(logging.WARN) self._logger.addHandler(handler_info)
self._logger.addHandler(handler_warn) #默认情况下,logger本身的log level是warn,为了让info handler的level等级生效,所以调低logger本身的level
self._logger.setLevel(logging.INFO) def getLogger(self):
return self._logger
Python Logger使用的更多相关文章
- Python logger /logging
# !/user/bin/python # -*- coding: utf-8 -*- ''' subprocess : 需要在linux平台上测试 shell logging ''' import ...
- Python logger模块
1 logging模块简介 logging模块是Python内置的标准模块,主要用于输出运行日志,可以设置输出日志的等级.日志保存路径.日志文件回滚等:相比print,具备如下优点: 可以通过设置不同 ...
- python logger日志工具类
pytest命令行执行默认不会打印log信息,需要加‘-s’参数或者 ‘–capture=no’,即pytest -s #! /usr/bin/env python # coding=gbk impo ...
- python Logger模块单例模式
前言 提前祝大家过个好年 最近忙于项目,今天抽出点时间写写Blog谈谈昨天遇到的问题 项目最近要收尾了,想把Logger规整一下,因为很多地方都有用到 Python的Logger模块是Python自带 ...
- python logger
[loggers] keys=root [handlers] keys=consoleHandler [formatters] keys=simpleFormatter [logger_root] l ...
- Python logger 没打出行数
# !/user/bin/python # -*- coding: utf-8 -*- ''' subprocess : 需要在linux平台上测试 shell logging ''' import ...
- Python根据系统环境配置日志,Python配置日志 Python logger
我们通常在写爬虫的时候,需要配置日志,但是有可能是在windows开发的,但是程序的运行环境可是是在Linux中,这时候我们就需要不停的更换日志的目录了 但是我们可以实现通过判断不同的运行环境,来时间 ...
- python logger日志
直接上代码 import logging import logging.handlers import datetime import time import threading from conf. ...
- python logger日志通用配置文件
阅读须知⚠️ 1.示例代码可直接放在项目py文件中即可使用 2.project_name,logfile_name变量需根据你的项目进行修改 3.日志输出格式format选择(可根据你的需要替换或修改 ...
随机推荐
- TCP三次握手四次挥手详解2
相对应socket开发者,TCP创建过程和连接拆除过程是由TCP/IP协议栈自动创建的,因此开发者并不需要控制这个过程,但是对于理解TCP底层运作机制,相当有帮助 TCP三次握手 所谓三次握手,是指建 ...
- Vim简明教程【CoolShell】(转)
m的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progressiv ...
- PR曲线 ROC曲线的 计算及绘制
在linear model中,我们对各个特征线性组合,得到linear score,然后确定一个threshold,linear score < threshold 判为负类,linear sc ...
- SQL: 拼接列
1. 因工作需要,需把两列(id,created_by)拼接成一列,结果很有意思,前5个值都是null. 2.解决方法:null加减乘除任何值都等于null,所以使用isnull函数先处理下列的值再拼 ...
- nginx重启 failed (98: Address already in use)
启动nginx服务,无法正常启动,一查log日志,发现如题错误信息. 问题描述:地址已被使用.可能nginx服务卡死了,导致端口占用,出现此错误. 查看端口 netstat -ntpl 杀掉进程 ...
- inline用法详解
(一)inline函数(摘自C++ Primer的第三版) 在函数声明或定义中函数返回类型前加上关键字inline即把min()指定为内联. inline int min(int first, int ...
- oracle_多字段统计(多count)
oracle_多字段统计 查询同一张表中同一字段的不同值的综合,方法如下: select o.code 礼品代码, o.name 礼品名称, l.couponactivityid 券活动定义, cou ...
- web上的复制
你可能曾经尝试过复制网页上的一些文字,得到的却是令人沮丧的的结果.这篇文章介绍相关的内容. 不是真正的文字 这可能是最常见的问题,很多人尝试对一张带有文字的图片进行像文字那样的选择,复制当然不行了. ...
- CentOS6.5下Cloudera安装搭建部署大数据集群(图文分五大步详解)(博主强烈推荐)
不多说,直接上干货! 第一步: Cloudera Manager安装之Cloudera Manager安装前准备(CentOS6.5)(一) 第二步: Cloudera Manager安装之时间服务 ...
- P3811 【模板】乘法逆元
P3811 [模板]乘法逆元 线性递推逆元模板 #include<iostream> #include<cstdio> #include<cstring> #def ...