Python-->logging....实例应用

本文详细介绍了Python中logging模块的使用方法,包括日志级别的设置、日志输出格式的定制及如何通过配置文件来管理日志。同时,还提供了丰富的代码示例帮助读者更好地理解和运用这些知识。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import logging
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')
 
输出:
E:\Python>python lianxi.py
WARNING:root:warning message
ERROR:root:error message
CRITICAL:root:critical message
 
#默认情况下Python的logging模块将日志输出,且只输出大于等于WARNING级别的日志。(日志级别等级critical>error>warning>info>debug)( 危急>错误>警告>信息>调试>没有设置),默认输出的日志格式为 日志级别:logger名称:用户输出消息。
#设置日志的输出格式
 
 
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',
                #r'xxx'   r后接一个字符串表示其中的转义字符不解析
    filename=r'E:\Python\test.log',
    filemode='w')
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')
 
 
文件内容为:
2016-05-15 10:05:24,843 lianxi.py[line:7] DEBUG debug message
2016-05-15 10:05:24,843 lianxi.py[line:8] INFO info message
2016-05-15 10:05:24,843 lianxi.py[line:9] WARNING warning message
2016-05-15 10:05:24,843 lianxi.py[line:10] ERROR error message
2016-05-15 10:05:24,843 lianxi.py[line:11] CRITICAL critical message
 
#可见在logging.basicConfig()函数中可以通过具体参数来更改logging模块的默认行为,可用参数有:
#ilename:用指定的文件名创建FileHandler,这样日志就会被储存在指定的文件中。
#filemode:文件打开的方式,在指定了filename时使用这个参数,默认为‘a’。
#format:  指定handler使用日期格式。
#datefmt:指定日期格式。
#level:     设置rootlogger的日志级别。
#stream: 用指定的stream创建streamhandler。当filename和stream两个参数,则stream参数会被忽略。
 
#format参数中可能用到的格式化串:
#%(name)s Logger的名字
#%(levelno)s 数字形式的日志级别
#%(levelname)s 文本形式的日志级别
#%(pathname)s 调用日志输出函数的模块的完整路径名,可能没有
#%(filename)s 调用日志输出函数的模块的文件名
#%(module)s 调用日志输出函数的模块名
#%(funcName)s 调用日志输出函数的函数名
#%(lineno)d 调用日志输出函数的语句所在的代码行
#%(created)f 当前时间,用UNIX标准的表示时间的浮 点数表示
#%(relativeCreated)d 输出日志信息时的,自Logger创建以 来的毫秒数
#%(asctime)s 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒
#%(thread)d 线程ID。可能没有
#%(threadName)s 线程名。可能没有
#%(process)d 进程ID。可能没有
#%(message)s用户输出的消息
#Logging的实例应用过程。。。。。
 
import logging            #1、申请logging模块   
 
logger = logging.getLogger("My_log")  #2、创建一个logging       logging.getLogger(name),name可不要
 
logger.setLverl(logging.INFO)     #3、设置logging的日志显示的级别
  
Filelog = logging.FileHandler(logfilename)  #4、创建一个日志的文件句柄 logfilename 是日志文件的路径
 
Streamlog = logging.StreamHandler()         #4、创建呢一个日志输出的句柄
 
Fmatter = logging.Formatter("%(asctime)s - %(filename)s - %(levelname)-8s: %(message)s",
                                             datefmt='%Y-%m-%d %I:%M:%S %p')   
                                             #5、设定日志输出到文件的格式
Smatter = logging.Formatter("%(asctime)s %(filename)s %(levelname)s:%(message)s",datefmt='%Y-%m-%d %I:%M:%S')    #5、设定日志输出的格式
 
Filelog.setFormatter(Fmatter)   #6、将日志文件格式加载
Streamlog.setFormattrt(Fmatter)     #6、将输出日志格式加载
 
 
Filelog.setLevel(logging.INFO)     #7、设置日志文件的级别,将决定输出的消息
Stream.setLevel(logging.INFO)  #7、设置日志输出的级别
 
logger.addHandler(Filelog)  #8、设置文件的输出方向
logger.addHandler(Stream) 
 
 
logger.info("msg")  #9、设置日志的输出内容,还有critical、error、warning、info、debug级别
                                #上面设定的日志输出级别决定了在这里需要定义那种级别的日志
 
 
logger.removeHandler(Filelog) #10、将句柄销毁
logger.removeHandler(Streamlog)






本文转自 nw01f 51CTO博客,原文链接:http://blog.51cto.com/dearch/1775695,如需转载请自行联系原作者
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-3-aae99b2c4409> in <cell line: 8>() 6 from matplotlib.pyplot import rc_context 7 import scanpy as sc ----> 8 import scvelo as scv 9 import scipy.io 10 import os /disk221/lihx/anaconda3/envs/pyscenic/lib/python3.8/site-packages/scvelo/__init__.py in <module> 2 from anndata import AnnData 3 ----> 4 from scvelo import datasets, logging 5 from scvelo import plotting as pl 6 from scvelo import preprocessing as pp /disk221/lihx/anaconda3/envs/pyscenic/lib/python3.8/site-packages/scvelo/datasets/__init__.py in <module> ----> 1 from ._datasets import ( 2 bonemarrow, 3 dentategyrus, 4 dentategyrus_lamanno, 5 forebrain, /disk221/lihx/anaconda3/envs/pyscenic/lib/python3.8/site-packages/scvelo/datasets/_datasets.py in <module> 7 from scanpy import read 8 ----> 9 from scvelo.core import cleanup 10 from scvelo.read_load import load 11 /disk221/lihx/anaconda3/envs/pyscenic/lib/python3.8/site-packages/scvelo/core/__init__.py in <module> ----> 1 from ._anndata import ( 2 clean_obs_names, 3 cleanup, 4 get_df, 5 get_initial_size, /disk221/lihx/anaconda3/envs/pyscenic/lib/python3.8/site-packages/scvelo/core/_anndata.py in <module> 12 from anndata import AnnData 13 ---> 14 from scvelo import logging as logg 15 from ._arithmetic import sum 16 from ._utils import deprecated_arg_names /disk221/lihx/anaconda3/envs/pyscenic/lib/python3.8/site-packages/scvelo/logging/__init__.py in <module> ----> 1 from ._logging import ( 2 error, 3 hint, 4 info, 5 msg, /disk221/lihx/anaconda3/envs/pyscenic/lib/python3.8/site-packages/scvelo/logging/_logging.py in <module> 209 210 # Adapted from https://github.com/theislab/cellrank/blob/main/src/cellrank/logging/_logging.py#L98-L128 --> 211 def _versions_dependencies(dependencies: Iterable[str]): 212 # this is not the same as the requirements! 213 for mod in dependencies: TypeError: 'ABCMeta' object is not subscriptable这个错误怎么解决
最新发布
06-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值