python實現(xiàn)的系統(tǒng)實用log類實例
本文實例講述了python實現(xiàn)的系統(tǒng)實用log類。分享給大家供大家參考。具體如下:
每個系統(tǒng)都必不可少會需要一個log類,方便了解系統(tǒng)的運行狀況和排錯,python本身已經(jīng)提供了一個logger了,很強(qiáng)大,只要稍微封裝一下就可以放到自己的系統(tǒng)了,下面是我自己的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')
執(zhí)行結(jié)果:
終端和文件中都顯示有:[2012-09-06 16:56:01,498] dxscs: [logger.py: 88]: INFO : hello,world
如果只需要顯示在文件中可以將debug和nofork選項都置為false
希望本文所述對大家的Python程序設(shè)計有所幫助。
相關(guān)文章
Django restframework 框架認(rèn)證、權(quán)限、限流用法示例
這篇文章主要介紹了Django restframework 框架認(rèn)證、權(quán)限、限流用法,結(jié)合實例形式詳細(xì)分析了Djangorestframework 框架認(rèn)證、權(quán)限、限流的具體使用方法及相關(guān)操作注意事項,需要的朋友可以參考下2019-12-12python爬蟲利器之requests庫的用法(超全面的爬取網(wǎng)頁案例)
這篇文章主要介紹了python爬蟲利器之requests庫的用法(超全面的爬取網(wǎng)頁案例),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12