python日志

简介:
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
97
98
99
100
101
102
log4py.py日志重构类
import  datetime  
import  sys  
import  traceback  
import  codecs  
import  types  
import  logging
import  os
import  time 
#log编码全部按utf8处理  
loglevels  =  { 'stdout' :[ 'info' , 'debug' , 'warn' , 'error' , 'fatal' ],  
     'file' :[ 'info' , 'debug' , 'warn' , 'error' , 'fatal' ]  
     }  
#print os.getcwd()+'/logs/logs.txt' 
logfile  =  os.getcwd() + '/logs/logs.' + time.strftime( '%Y-%m-%d' ,time.localtime(time.time())) + '.txt'  
class  log4py():  
     def  __init__( self ,modulename = "gloabal" ):  
         self .filename  =  logfile  
         #self.flag = set(loglevel['stdout']+loglevel['file'])  
         self .loglevel  =  loglevels  
         self .modulename  =  modulename  
         self .fcname  =  None  
     class  function():  
         def  __init__( self ,fcname,parent):  
             parent.debug( 'enter ' ,fcname)  
             self .fcname  =  fcname  
             self .parent  =  parent  
         def  __del__( self ):  
             self .parent.debug( 'exit ' , self .fcname)  
     def  dbgfc( self ,fcname):  
         '''''set debug function name'''  
         =  None  
         if  'debug'  in  self .flag:  
             =  self .function(fcname, self )  
         return  f  
     def  _gettime( self ):  
         return  datetime.datetime.now().isoformat()  
     def  outstd( self , * fmt):  
         =  self .fmtstr( * fmt)  
         print  s  
     def  outfile( self , * fmt):  
         =  self .fmtstr( * fmt)  
         #print 'before outfile '+s  
         if  s:  
             #print 'outfile '+s  
             encoding  =  'utf8'  
             out  =  open (logfile,  'a+' ) #, encoding  
             out.write(s)  
             out.write( '\n' )  
             out.close()  
     def  fmtstr( self * fmt):  
         str  =  ''  
         encoding  =  'utf8' #缺省utf8编码  
         for  in  fmt:  
             if  not  type (i)  in  [types.UnicodeType, types.StringTypes, types.StringType]:  
                 s =  repr (i)  
             else :  
                 =  i  
             if  type (s)  = =  type (u''):  
                 str  + =  s.encode(encoding)  
             else :  
                 str  + =  s  
             str  + =  '.'  
         #str += '/n'  
         #print 'fmtstr:'+str  
         return  str  
     def  debug( self , * fmt):  
         if  'debug'  in  self .loglevel[ 'stdout' ]:  
             self .outstd( self ._gettime(), '[DEBUG]' , self .modulename, * fmt)  
         if  'debug'  in  self .loglevel[ 'file' ]:  
             #print 'debug file ...'  
             self .outfile( self ._gettime(), '[DEBUG]' , self .modulename, * fmt)  
     def  warn( self , * fmt):  
         if  'warn'  in  self .loglevel[ 'stdout' ]:  
             self .outstd( self ._gettime(), '[WARN]' , self .modulename, * fmt)  
         if  'warn'  in  self .loglevel[ 'file' ]:  
             self .outfile( self ._gettime(), '[WARN]' , self .modulename, * fmt)  
     def  info( self , * fmt):  
         if  'info'  in  self .loglevel[ 'stdout' ]:  
             self .outstd( self ._gettime(), '[INFO]' , self .modulename, * fmt)  
         if  'info'  in  self .loglevel[ 'file' ]:  
             self .outfile( self ._gettime(), '[INFO]' , self .modulename, * fmt)  
     def  error( self , * fmt):  
         #print '/033[0;30;41m',  
         if  'error'  in  self .loglevel[ 'stdout' ]:  
             self .outstd( self ._gettime(), '[ERROR]' , self .modulename, * fmt)  
         if  'error'  in  self .loglevel[ 'file' ]:  
             self .outfile( self ._gettime(), '[ERROR]' , self .modulename, * fmt)  
         #print '/033[0m'  
     def  fatal( self , * fmt):  
         if  'fatal'  in  self .loglevel[ 'stdout' ]:  
             self .outstd( self ._gettime(), '[FATAL' , self .modulename, * fmt)  
         if  'fatal'  in  self .loglevel[ 'file' ]:  
             self .outfile( self ._gettime(), '[FATAL' , self .modulename, * fmt)  
#unit test  
if  __name__  = =  '__main__' :  
     log = log4py()  
     log.outstd( 'INFO' , 'stdout' , 'test' )  
     log.outfile( 'INFO' , 'stdout' , 'test' )  
     log.debug( 'debug information 调试' )  
     log.error( 'errorrrrrrrrrrrrrrr' )  
     log.debug( 'hello' )


用法:

1
2
3
from  log4py  import  log4py
log = log4py( '所在的python文件' )


本文转自 luoguo 51CTO博客,原文链接:http://blog.51cto.com/luoguoling/1945089
相关实践学习
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
相关文章
|
7月前
|
监控 安全 程序员
Python日志模块配置:从print到logging的优雅升级指南
从 `print` 到 `logging` 是 Python 开发的必经之路。`print` 调试简单却难维护,日志混乱、无法分级、缺乏上下文;而 `logging` 支持级别控制、多输出、结构化记录,助力项目可维护性升级。本文详解痛点、优势、迁移方案与最佳实践,助你构建专业日志系统,让程序“有记忆”。
634 0
|
存储 监控 API
【Azure App Service】分享使用Python Code获取App Service的服务器日志记录管理配置信息
本文介绍了如何通过Python代码获取App Service中“Web服务器日志记录”的配置状态。借助`azure-mgmt-web` SDK,可通过初始化`WebSiteManagementClient`对象、调用`get_configuration`方法来查看`http_logging_enabled`的值,从而判断日志记录是否启用及存储方式(关闭、存储或文件系统)。示例代码详细展示了实现步骤,并附有执行结果与官方文档参考链接,帮助开发者快速定位和解决问题。
354 22
|
API 开发工具 Python
|
监控 数据挖掘 数据安全/隐私保护
Python脚本:自动化下载视频的日志记录
Python脚本:自动化下载视频的日志记录
|
Python
python读写操作excel日志
主要是读写操作,创建表格
228 2
|
Python Windows
python知识点100篇系列(24)- 简单强大的日志记录器loguru
【10月更文挑战第11天】Loguru 是一个功能强大的日志记录库,支持日志滚动、压缩、定时删除、高亮和告警等功能。安装简单,使用方便,可通过 `pip install loguru` 快速安装。支持将日志输出到终端或文件,并提供丰富的配置选项,如按时间或大小滚动日志、压缩日志文件等。还支持与邮件通知模块结合,实现邮件告警功能。
470 0
python知识点100篇系列(24)- 简单强大的日志记录器loguru
|
数据采集 机器学习/深度学习 存储
使用 Python 清洗日志数据
使用 Python 清洗日志数据
480 2
|
开发者 Python
基于Python的日志管理与最佳实践
日志是开发和调试过程中的重要工具,然而,如何高效地管理和利用日志常常被忽略。本文通过Python中的logging模块,探讨如何使用日志来进行调试、分析与问题排查,并提出了一些实际应用中的优化建议和最佳实践。
|
消息中间件 Kafka API
python之kafka日志
python之kafka日志
327 3
Python如何将日志输入到文件里
Python如何将日志输入到文件里

热门文章

最新文章

推荐镜像

更多