python实现的系统实用log类实例
作者:liujian0616 时间:2022-08-02 18:50:57
本文实例讲述了python实现的系统实用log类。分享给大家供大家参考。具体如下:
每个系统都必不可少会需要一个log类,方便了解系统的运行状况和排错,python本身已经提供了一个logger了,很强大,只要稍微封装一下就可以放到自己的系统了,下面是我自己的log类
文件名:logger.py
"""This module takes care of the logging
logger helps in creating a logging system for the application
Logging is initialised by function LoggerInit.
"""
import logging
import os
import sys
class logger(object):
"""Class provides methods to perform logging."""
m_logger = None
def __init__(self, opts, logfile):
"""Set the default logging path."""
self.opts = opts
self.myname = 'dxscs'
self.logdir = '.'
self.logfile = logfile
self.filename = os.path.join(self.logdir, self.logfile)
def loginit(self):
"""Calls function LoggerInit to start initialising the logging system."""
logdir = os.path.normpath(os.path.expanduser(self.logdir))
self.logfilename = os.path.normpath(os.path.expanduser(self.filename))
if not os.path.isdir(logdir):
try:
os.mkdir(logdir)
except OSError, e:
msg = ('(%s)'%e)
print msg
sys.exit(1)
self.logger_init(self.myname)
def logger_init(self, loggername):
"""Initialise the logging system.
This includes logging to console and a file. By default, console prints
messages of level WARN and above and file prints level INFO and above.
In DEBUG mode (-D command line option) prints messages of level DEBUG
and above to both console and file.
Args:
loggername: String - Name of the application printed along with the log
message.
"""
fileformat = '[%(asctime)s] %(name)s: [%(filename)s: %(lineno)d]: %(levelname)-8s: %(message)s'
logger.m_logger = logging.getLogger(loggername)
logger.m_logger.setLevel(logging.INFO)
self.console = logging.StreamHandler()
self.console.setLevel(logging.CRITICAL)
consformat = logging.Formatter(fileformat)
self.console.setFormatter(consformat)
self.filelog = logging.FileHandler(filename=self.logfilename, mode='w+')
self.filelog.setLevel(logging.INFO)
self.filelog.setFormatter(consformat)
logger.m_logger.addHandler(self.filelog)
logger.m_logger.addHandler(self.console)
if self.opts['debug'] == True:
self.console.setLevel(logging.DEBUG)
self.filelog.setLevel(logging.DEBUG)
logger.m_logger.setLevel(logging.DEBUG)
if not self.opts['nofork']:
self.console.setLevel(logging.WARN)
def logstop(self):
"""Shutdown logging process."""
logging.shutdown()
#test
if __name__ == '__main__':
#debug mode & not in daemon
opts = {'debug':True,'nofork':True}
log = logger(opts, 'dxscs_source.log')
log.loginit()
log.m_logger.info('hello,world')
执行结果:
终端和文件中都显示有:[2012-09-06 16:56:01,498] dxscs: [logger.py: 88]: INFO : hello,world
如果只需要显示在文件中可以将debug和nofork选项都置为false
希望本文所述对大家的Python程序设计有所帮助。
标签:python,log
0
投稿
猜你喜欢
MySQL中truncate误操作后的数据恢复案例
2024-01-12 20:45:56
python实现会员管理系统
2023-11-13 19:44:46
python程序 线程队列queue使用方法解析
2021-11-16 18:43:02
python 图像处理画一个正弦函数代码实例
2021-08-10 22:17:51
ASP+FSO+框架实现ASP生成htm并分页的方法(批量)
2009-03-09 18:26:00
SQL Server DATEDIFF() 函数用法
2024-01-17 16:18:16
java代码获取数据库表里数据的总数操作
2024-01-14 23:41:31
Python MySQLdb 使用utf-8 编码插入中文数据问题
2023-07-31 11:04:13
PHP实现动态删除XML数据的方法示例
2024-06-05 09:51:28
Python实现复杂对象转JSON的方法示例
2021-11-14 09:55:47
超详细汇总21个值得收藏的mysql优化实践
2024-01-17 21:01:18
Win2003服务器安装及设置教程 MySQL安全设置图文教程
2024-01-28 17:55:46
django云端留言板实例详解
2023-12-13 08:17:59
Go中变量命名规则与实例
2024-05-09 09:55:45
SQL Server主键与外键设置以及相关理解
2024-01-21 10:45:05
python使用json.dumps输出中文问题
2023-11-17 22:04:46
进一步了解Python中的XML 工具
2022-06-25 21:49:17
js实现通过开始结束控制的计时器
2024-04-18 09:39:03
屏蔽Flash 右键菜单的方法
2008-05-24 07:21:00
Python中 join() 函数的使用示例讲解
2023-03-29 02:32:25