打印日志的五个级别:

import logging

logging.debug('test debug')
logging.info('test info')
logging.warning('test warning')# WARNING:root:test warning
logging.error('test error') # ERROR:root:test error
logging.critical('test critical')# CRITICAL:root:test critical

把日志信息写入到文件:

import logging
logging.basicConfig(filename='app.log',level=logging.DEBUG)
logging.debug('test debug')
logging.info('test info')
logging.warning('test warning')
logging.error('test error')
logging.critical('test critical')

app.log

DEBUG:root:test debug
INFO:root:test info
WARNING:root:test warning
ERROR:root:test error
CRITICAL:root:test critical

添加日志添加时间:

import logging
logging.basicConfig(filename='app.log',level=logging.DEBUG,format='%(asctime)s %(message)s',datefmt='%Y/%m/%d %I:%M:%S %p')
#设置写入的文件、默认级别 (小于这个级别的不写入)、输出格式和时间
logging.debug('test debug') # 2017/08/05 05:02:31 PM test debug
logging.info('test info') # 2017/08/05 05:02:31 PM test info
logging.warning('test warning') # 2017/08/05 05:02:31 PM test warning
logging.error('test error') # 2017/08/05 05:02:31 PM test error
logging.critical('test critical') # 2017/08/05 05:02:31 PM test critical

logging.basicConfig函数各参数:
filename: 指定日志文件名
filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a'
format: 指定输出的格式和内容,format可以输出很多有用信息,如上例所示:

%(levelno)s: 打印日志级别的数值
 %(levelname)s: 打印日志级别名称
 %(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]
 %(filename)s: 打印当前执行程序名
 %(funcName)s: 打印日志的当前函数
 %(lineno)d: 打印日志的当前行号
 %(asctime)s: 打印日志的时间
 %(thread)d: 打印线程ID
 %(threadName)s: 打印线程名称
 %(process)d: 打印进程ID
 %(message)s: 打印日志信息
datefmt: 指定时间格式,同time.strftime()
level: 设置日志级别,默认为logging.WARNING
stream: 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略

将日志同时输出到文件和屏幕

import logging

logger = logging.getLogger('TEST-LOG')
logger.setLevel(logging.DEBUG) ch = logging.StreamHandler() #设置屏幕的handler
ch.setLevel(logging.WARNING) fh = logging.FileHandler('access.log',encoding="utf-8") #设置输出文件的handler
fh.setLevel(logging.ERROR)
#设置两种输出格式
fh_formatter =logging.Formatter('%(asctime)s %(process)d %(filename)s %(levelname)s %(message)s')
ch_formatter =logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# Handler和Formatter关联
fh.setFormatter(fh_formatter)
ch.setFormatter(ch_formatter)
#添加到logger
logger.addHandler(fh)
logger.addHandler(ch) logger.warning('warning')#2017-08-07 15:15:25,693 - TEST-LOG - WARNING - warning
logger.error('error')#2017-08-07 15:15:25,693 - TEST-LOG - ERROR - error

access.log

2017-08-07 15:15:25,693 5380 01.py ERROR error

自动截断

import logging
from logging import handlers
logger = logging.getLogger("TEST") log_file = 'timelog.log'
fh = handlers.RotatingFileHandler(filename=log_file,maxBytes=10,backupCount=3,encoding="utf-8")
#fh = handlers.TimedRotatingFileHandler(filename=log_file,when="S",interval=5,backupCount=3) formatter = logging.Formatter('%(asctime)s %(module)s:%(lineno)d %(message)s') fh.setFormatter(formatter) logger.addHandler(fh) logger.warning('test11')
logger.warning('test12')
logger.warning('test13')
logger.warning('test14')

timelog.log

2017-08-07 15:41:48,742 02:19 test14

timelog.log1

2017-08-07 15:41:48,735 02:18 test13

timelog.log2

2017-08-07 15:41:48,715 02:17 test12

timelog.log3

2017-08-07 15:41:48,712 02:16 test11

其它详细操作:python 的日志logging模块学习

python模块详解 logging的更多相关文章

  1. python模块详解 | selenium(持续更新中)

    目录: 关于selenium Selenium 安装Selenium 安装浏览器驱动 配置环境变量 selenium方法详解 定位元素 元素操作 浏览器操作 鼠标事件 浏览器事件 设置元素等待 多表单 ...

  2. python模块详解 random os

    random模块 常用方法 random.random() 随机产生一个小于1的浮点数 import random print(random.random()) #0.4153761818276826 ...

  3. python模块详解

    什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的代码(.p ...

  4. python模块详解 sys shutil

    sys模块 sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0) sys.version 获取Python解释程序的版本信息 sy ...

  5. python模块详解 | shutil

    简介: shutil是python的一个内置模块,提供了许多关于文件和文件集合的高级操作,特别提供文件夹与文件操作.归档操作了支持文件复制和删除的功能. 文件夹与文件操作: copyfileobj(f ...

  6. 小白的Python之路 day5 python模块详解及import本质

    一.定义 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能) 本质就是.py结尾的python文件(文件名:test.py,对应的模块名:test) 包:用来从逻辑上组织模块 ...

  7. Python 模块详解及import本质

    同在当前目录下的模块和包导入 模块定义 本质就是.py结尾的python文件. 用来从逻辑上组织python代码(变量,函数,类,逻辑) 文件名: test.py;  对应的模块名 : test 模块 ...

  8. Python模块详解以及import本质,获得文件当前路径os.path.abspath,获得文件的父目录os.path.dirname,放到系统变量的第一位sys.path.insert(0,x)

    模块介绍 1.定义: 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名:test.py,对应的模块名:test) 包:用来从逻 ...

  9. python模块详解 re

    摘自:python中的正则表达式(re模块) 一.简介 正则表达式本身是一种小型的.高度专业化的编程语言,而在python中,通过内嵌集成re模块,程序媛们可以直接调用来实现正则匹配.正则表达式模式被 ...

随机推荐

  1. C++基础学习7:new/delete操作符

    在C语言中,动态分配和释放内存的函数是malloc.calloc和free,而在C++语言中,new.new[].delete和delete[]操作符通常会被用来动态地分配内存和释放内存. 需要注意的 ...

  2. PAT天梯赛L1-020 帅到没朋友

    题目链接:点击打开链接 当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为太帅而没有朋友.本题就要求你找出那些帅到没有朋友的人. 输入格式: 输入第一行给出一个正整数N(<=100),是已知朋 ...

  3. js 多张图片加载 环形进度条

    css 部分使用 svg 绘制环形 svg{width:100px; height: 100px; margin:15% auto 25%; box-sizing:border-box; displa ...

  4. linux防火墙添加端口

     防火墙配置文件: /etc/sysconfig/iptables 1.使用命令查看端口开启情况(下图为安装时未选择开启防火墙) [root@fullstack ~]# iptables -L -n ...

  5. C语言中的预处理命令

    预处理功能是C语言的重要功能. 问:为什么要预处理,什么是预处理? 答:我们知道高级语言的运行过程是通过编译程序(编译器)把源代码翻译成机器语言,实现运行的.编译程序的工作包含:语法分析.词法分析.代 ...

  6. 【笔记】如何在for语句中迭代多个可迭代对象

    并行=>使用内置函数zip,它能将多个可迭代对象合并,每次迭代返回一个元组. for i,j,k in zip(a,b,c): TODO 穿行=>使用标准库中的itertools.chai ...

  7. 江西财经大学第一届程序设计竞赛 B

    链接:https://www.nowcoder.com/acm/contest/115/B来源:牛客网 题目描述 给出一个出生日期,比如:1999-09-09, 问:从出生那一天开始起,到今天2018 ...

  8. 兼容IE,chrome,ff的设为首页、加入收藏及保存到桌面

    // JavaScript Document// 加入收藏 < a onclick="AddFavorite(window.location,document.title)" ...

  9. hdu1016 Prime Ring Problem(DFS)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  10. MongoDB安装为windows服务

    MongoDB 下载 下载地址:http://www.mongodb.org/downloads 下载安装完成之后 第一步 创建D:\Program Files\mongodb\data 目录第二步 ...