python中logging日志基本用法,和进程安全问题
低配版
import logging logging.debug('debug message') # 调试模式
logging.info('info message') # 正常运转模式
logging.warning('warning message') # 警告模式
logging.error('error message') # 错误模式
logging.critical('critical message') # 致命的 崩溃模式 while 1:
try:
num = input('>>>')
num = int(num) except ValueError:
logging.warning('输入非数字警告')
标准版
import logging
# 1.产生logger对象
logger = logging.getLogger() # 2 产生其他对象(屏幕对象,文件对象)
sh = logging.StreamHandler()
fh1 = logging.FileHandler('staff.log', encoding='utf-8')
fh2 = logging.FileHandler('boss.log', encoding='utf-8') # 3,设置显示格式
formater = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
formater1 = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
formater2 = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') # 4,给对象绑定格式
sh.setFormatter(formater)
fh1.setFormatter(formater1)
fh2.setFormatter(formater2) # 5 给logger对象绑定其他对象
logger.addHandler(sh)
logger.addHandler(fh1)
logger.addHandler(fh2) # 6 设置显示级别
# 其他对象的级别要高于logger的级别
logger.setLevel(10)
sh.setLevel(20)
fh1.setLevel(20)
fh2.setLevel(30) logging.debug('debug message') # 调试模式
logging.info('info message') # 正常运转模式
logging.warning('warning message') # 警告模式
logging.error('error message') # 错误模式
logging.critical('crit
高配版
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' # boss版格式(lowb版)
id_simple_format = '[%(levelname)s][%(asctime)s] %(message)s' # 定义日志输出格式 结束 file_path = os.path.dirname(__file__) logfile_name = file_path + '\staff.log' # log文件名 # log配置字典 LOGGING_DIC = {
'version': 1, # 版本
'disable_existing_loggers': False, # 可否重复使用之前的logger对象
'formatters': {
'standard': {
'format': standard_format
},
'simple': {
'format': simple_format
},
'boss_formatter': {
'format': id_simple_format
},
},
'filters': {},
'handlers': {
#打印到终端的日志
'stream': {
'level': 'DEBUG',
'class': 'logging.StreamHandler', # 打印到屏幕
'formatter': 'simple'
},
#打印到文件的日志,收集info及以上的日志 文件句柄
'file': {
'level': 20,
'class': 'logging.handlers.RotatingFileHandler', # 保存到文件
'formatter': 'standard', # 标准
'filename': logfile_name, # 日志文件
'maxBytes': 300, # 日志大小 300 bit
'backupCount': 5, #轮转文件数
'encoding': 'utf-8', # 日志文件的编码,再也不用担心中文log乱码了
},
},
'loggers': {
# logging.getLogger(__name__)拿到的logger配置
'': {
'handlers': ['stream', 'file'], # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
'level': 'DEBUG', # 总级别
'propagate': True, # 向上(更高level的logger)传递
},
},
}
# 字典中第一层的所有key都是固定不可变的。 logging.config.dictConfig(LOGGING_DIC)
logger = logging.getLogger() # 这个logger对象是通过自己个性化配置的logger对象
logger.info('运转正常') # def load_my_logging_cfg():
# logging.config.dictConfig(LOGGING_DIC) # 导入上面定义的logging配置
# logger = logging.getLogger(__name__) # 生成一个log实例
# logger.info('It works!') # 记录该文件的运行状态
#### settings.LOGGING_DIC['handlers']['file']['filename'] = 路径地址 # 通过操作字典 可实现动态设置log文件的存储路径,和文件名
# if __name__ == '__main__':
# load_my_logging_cfg()
关于logging模块的进程安全问题
logging 是线程安全的,也就是说,在一个进程内的多个线程同时往同一个文件写日志是安全的。
但是多个进程往同一个文件写日志不是安全的。
https://www.cnblogs.com/restran/p/4743840.html
python中logging日志基本用法,和进程安全问题的更多相关文章
- Python 中 logging 日志模块在多进程环境下的使用
因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,Python 中 logging 日志模块在多进程环境下的使用 使用 Pytho ...
- python中logging模块的用法
很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,loggin ...
- Python中logging日志模块的使用
参考https://www.cnblogs.com/CJOKER/p/8295272.html
- Python中logging模块的基本用法
在 PyCon 2018 上,Mario Corchero 介绍了在开发过程中如何更方便轻松地记录日志的流程. 整个演讲的内容包括: 为什么日志记录非常重要 日志记录的流程是怎样的 怎样来进行日志记录 ...
- python的logging日志模块(一)
最近修改了项目里的logging相关功能,用到了Python标准库里的logging模块,在此做一些记录.主要是从官方文档和stackoverflow上查询到的一些内容. 官方文档 技术博客 基本用法 ...
- Python中的日志处理
在日常项目中,总是需要记录下一些细小信息或者错误码.错误信息的,这个时候就需要进行日志的操作.python中用于日志创建.设置和记录等功能的模块,就是logging了,下面是对其基本使用方法的介绍: ...
- Python中 sys.argv[]的用法
Python中 sys.argv[]的用法 因为是看书自学的python,开始后不久就遇到了这个引入的模块函数,且一直在IDLE上编辑了后运行,试图从结果发现它的用途,然而结果一直都是没结果,也在网上 ...
- 31 Python中 sys.argv[]的用法简明解释(转)
Python中 sys.argv[]的用法简明解释 因为是看书自学的python,开始后不久就遇到了这个引入的模块函数,且一直在IDLE上编辑了后运行,试图从结果发现它的用途,然而结果一直都是没结果, ...
- python中的日志操作和发送邮件
1.python中的日志操作 安装log模块:pip install nnlog 参数:my_log = nnlog.Logger('server_log.log',level='debug',bac ...
随机推荐
- 结合业务,精炼SQL
现代网站,性能的瓶颈都围绕着数据库的性能来谈.数据库是存储的核心部件,在日益增长的流量中会凸显数据库的性能瓶颈.从<淘宝技术十年>书中来看,淘宝发展历程中从MYSQL换成了ORACLE又换 ...
- python3.6使用scrapy报错
用python做爬虫的,肯定熟悉scrapy,不过新手安装总是会遇到各种奇葩错误. 错误一:building 'twisted.test.raiser' extensionerror: Microso ...
- 2016级算法期末上机-F.中等·AlvinZH's Fight with DDLs II
1118 AlvinZH's Fight with DDLs II 思路 中等题,贪心. 理解题意,每次攻击中,可以使某个敌人生命值-1,自己生命值减去∑存活敌人总攻击力. 贪心思想,血量少攻击高的要 ...
- 2016级算法第六次上机-C.AlvinZH的学霸养成记II
1032 AlvinZH的学霸养成记II 思路 中等题,贪心. 所有课程按照DDL的大小来排序. 维护一个当前时间curTime,初始为0. 遍历课程,curTime加上此课程持续时间d,如果这时cu ...
- java无符号Byte
1.无符号byte, 实现了将byte(-128~127) 转换为 (0~255) class UnsignedByte { private short value; private byte raw ...
- [转] org.scalatest.FunSuite Scala Examples - Scala FunSuite 测试的例子
[From] https://www.programcreek.com/scala/org.scalatest.FunSuite org.scalatest.FunSuite Scala Examp ...
- MySQL error2003错误原因以及解决方案
转自:http://hi.baidu.com/tianxia339/item/8e8849111461ea7e7a5f2540 出现ERROR 2003 (HY000): Can't connect ...
- .net core 多平台部署
首先下载地址 https://dotnet.microsoft.com/download 下载.net core 和 .net core runtime 然后安装他们 控制台运行项目: 找到你的工 ...
- Spring声明式事务为何不回滚
- STL中deque 解析
一.deque的中控器 deque是连续空间(至少逻辑上看来如此),连续线性空间总令我们联想到array或vector.array无法成长,vector虽可成长,却只能向尾端成长,而且其所谓的成长原是 ...