Python3 logging 模块
Python3 logging模块
日志模块:
用于便捷记录日志且线程安全的模块
CRITICAL = 50
FATAL = CRITICAL
ERROR = 40
WARNING = 30
WARN = WARNING
INFO =20
DEBUG = 10
NOTSET = 0
设置为debug
import logging
import logging
logging.debug('debug')
logging.info('info')
logging.warning('warning')
logging.error('error')
logging.critical('critical') logging.basicConfig(filename = 'access.log',format = '%(asctime)s-%(name)s-%(levelname)s-%(module)s:%(message)s',
datefmt = '%Y-%m-%d %H:%M:%S %p',
level = 10)
logging.debug('debug')
formatter1=logging.Formatter('%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S %p',)
fh1 = logging.FileHandler('test1.log')
fh2 = logging.FileHandler('test2.log')
fh3 = logging.FileHandler('test3.log')
ch = logging.StreamHandler() fh1.setFormatter(formatter1)
fh2.setFormatter(formatter1)
fh3.setFormatter(formatter1)
ch.setFormatter(formatter1) logger1 = logging.getLogger('egon')
logger1.setLevel(10)
logger1.addHandler(fh1)
logger1.addHandler(fh2)
logger1.addHandler(fh3)
logger1.addHandler(ch)
logger1.debug('debug')
logger1.info('info')
logger1.warning('warning')
logger1.critical('critical')
logging配置模板
import os import logging.config # 定义三种日志输出格式 开始 standard_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' \ '[%(levelname)s][%(message)s]' #其中name为getlogger指定的名字 simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s' id_simple_format = '[%(levelname)s][%(asctime)s] %(message)s' # 定义日志输出格式 结束 logfile_dir = os.path.dirname(os.path.abspath(__file__)) # log文件的目录 logfile_name = 'all2.log' # log文件名 # 如果不存在定义的日志目录就创建一个 if not os.path.isdir(logfile_dir): os.mkdir(logfile_dir) # log文件的全路径 logfile_path = os.path.join(logfile_dir, logfile_name) # log配置字典 LOGGING_DIC = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'standard': { 'format': standard_format }, 'simple': { 'format': simple_format }, 'id_simple': { 'format': id_simple_format }, }, 'filters': {}, 'handlers': { #打印到终端的日志 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', # 打印到屏幕 'formatter': 'simple' }, #打印到文件的日志,收集info及以上的日志 'default': { 'level': 'DEBUG', 'class': 'logging.handlers.RotatingFileHandler', # 保存到文件 'formatter': 'standard', 'filename': logfile_path, # 日志文件 'maxBytes': 1024*1024*5, # 日志大小 5M 'backupCount': 5, 'encoding': 'utf-8', # 日志文件的编码,再也不用担心中文log乱码了 }, 'boss': { 'level': 'DEBUG', 'class': 'logging.handlers.RotatingFileHandler', # 保存到文件 'formatter': 'standard', 'filename': 'boss.log', # 日志文件 'maxBytes': 1024 * 1024 * 5, # 日志大小 5M 'backupCount': 5, 'encoding': 'utf-8', # 日志文件的编码,再也不用担心中文log乱码了 }, }, 'loggers': { #logger1=logging.getLogger(__name__)拿到的logger配置 '': { 'handlers': ['default', 'console'], # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕 'level': 'DEBUG', 'propagate': True, # 向上(更高level的logger)传递 }, #logger1=logging.getLogger('collect')拿到的logger配置 'collect': { 'handlers': ['boss',], # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕 'level': 'DEBUG', 'propagate': True, # 向上(更高level的logger)传递 }, }, } def load_my_logging_cfg(): logging.config.dictConfig(LOGGING_DIC) # 导入上面定义的logging配置 # logger = logging.getLogger(__name__) # 生成一个log实例 # logger.info('It works!') # 记录该文件的运行状态 if __name__ == '__main__': load_my_logging_cfg()
调用 logging 配置模板
import logging
import my_log_settings
my_log_settings.load_my_logging_cfg() logger1=logging.getLogger(__name__)
logger2=logging.getLogger('collect') logger1.debug('默认日志的debug')
logger2.debug('给老板一封信')
logger2.debug('给前台MM一封信')
Python3 logging 模块的更多相关文章
- Python3 logging模块&ConfigParser模块
''' 博客园 Infi_chu ''' ''' logging模块 该模块是关于日志相关操作的模块 ''' import logging # logging.debug('debug') # log ...
- python3 logging模块
很多程序都有记录日志的需求,并且日志包含的信息有正常的程序访问日志还可能有错误,警告等信息输出,python的logging模块提供了标准的日志接口,可以通过它存储各种格式的日志,日志级别等级:cri ...
- python3:logging模块 输出日志到文件
python自动化测试脚本运行后,想要将日志保存到某个特定文件,使用python的logging模块实现 参考代码: import logging def initLogging(logFilenam ...
- Python3.x:logging模块对运行过程记录
Python3.x:logging模块对运行过程记录 示例: import logging # 设置 logger = logging.getLogger() #set loghandler #默认路 ...
- Python3之logging模块浅析
Python3之logging模块浅析 目录 Python3之logging模块浅析 简单用法 日志与控制台同时输出 一个同时输出到屏幕.文件的完成例子 日志文件截取 日志重复打印问题解决 问题分 ...
- python3之xml&ConfigParser&hashlib&Subprocess&logging模块
1.xml模块 XML 指可扩展标记语言(eXtensible Markup Language),标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言. XML 被设计用来传输和存储 ...
- python3学习-logging模块
1.logging模块的使用非常简单,引入模块就可以使用. import logging logging.debug('This is debug message') logging.info('Th ...
- s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译
时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...
- python+selenium自动化软件测试(第9章) :Logging模块
9.1 Logging模块 什么是日志记录?记录是跟踪运行时发生的事件的一种手段.该软件的开发人员将记录调用添加到其代码中,以指示某些事件已发生.事件由描述性消息描述,该消息可以可选地包含可变数据(即 ...
随机推荐
- java中获取文件路径的几种方式
http://xyzroundo.iteye.com/blog/1116159关于绝对路径和相对路径: 绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:C:xyz es ...
- Java GUI编程SwingUtilities.invokeLater作用
1 http://blog.micxp.com/index.php/archives/109/ 2
- 工作总结 sql 中过滤条件 中的 (where中的) and
总结: 在where 后面做过滤的时候 如果 有 字段1 必须满足某种值 字段2 要满足 某种或某值的时候 直接 and 字段1 = ‘a’ and 字段2 = ‘b’ or 字 ...
- 防止Spinner初始化加载
spinner在初始化的时候会自动调用一次OnItemSelectedListener,我们很多时候需要他不加载这一次,使用下面的代码spinner.setSelection(0, true);
- angularjs2中的父子组件通信
父组件模板中引用子组件 // father template: ... <child-item [name] = "fatherItemName" > </chi ...
- spring的xml配置文件出现故障
今天在断网的情况下,spring的applicationContext.xml文件开头部分出现红叉 <span style="font-size:18px;">< ...
- linux的用户、群组
1. 用户及passwd文件 1) 掌握/etc/passwd文件的功能:存储所有用户的相关信息,该文件也被称为用户信息数据库(Database). 2) /etc/pa ...
- (二)spark算子 分为3大类
transgormation的算子对key-value类型的数据有三种: (1)输入 与 输出为一对一关系 mapValue();针对key-value类型的数据并只对其中的value进行操作,不对 ...
- C#中Lock静态字段和实例字段
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 《HBase in Action》 第三章节的学习总结 ---- 如何编写和运行基于HBase的MapReduce程序
HBase之所以与Hadoop是最好的伙伴,我理解就因为两点:1.HADOOP的HDFS,为HBase提供了分布式的存储方式:2.HADOOP的MR为HBase提供的分布式的计算方法.u 其中第一点, ...