python实现的系统实用log类实例
作者:liujian0616 发布时间:2022-08-02 18:50:57
标签:python,log
本文实例讲述了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程序设计有所帮助。


猜你喜欢
- 人脸磨皮是最基础的人脸美颜效果。主要分为祛斑,祛痘,淡化黑眼圈等步骤。通过前面的学习相信大家一眼都看得出来我们需要干什么才能识别人脸磨皮效果
- 1.configparser介绍configparser是python自带的配置参数解析器。可以用于解析.config文件中的配置参数。in
- 如下所示:from keras import backend as Kfrom keras.models import load_model
- 本文实例讲述了python中MySQLdb模块用法。分享给大家供大家参考。具体用法分析如下:MySQLdb其实有点像php或asp中连接数据
- 背景随着Web技术的发展和移动互联网的发展,Hybrid技术已经成为一种前端开发的主流技术方案。那什么是Hybrid App呢?Hybrid
- 这个我早就改好了一直没有放上来.现在发给大家用用注意这几个变量它们影响到提示框的效果代码:var rT=true;//允许图像过渡
- skimage的transform模块图像的形变与缩放,使用的是skimage的transform模块,函数比较多,功能齐全。1、改变图片尺
- 1.什么是并发编程并发编程是实现多任务协同处理,改善系统性能的方式。Python中实现并发编程主要依靠进程(Process):进程是计算机中
- 1. 新建文件夹if not os.path.exists(feature_dir): os.makedirs(f
- 本文主要介绍通过预训练的ImageNet模型实现图像分类,主要使用到的网络结构有:VGG16、InceptionV3、ResNet50、Mo
- Python的全局变量:int string, list, dic(map) 如果存在global就能够修改它的值。而不管这个global是
- 介绍本文主要介绍Python中迭代的基本知识和使用什么是迭代在Python中,如果给定一个list或tuple,我们可以通过for循环来遍历
- 前期准备在虚拟开发环境中安装:pip install django-filter在Django的项目配置文件中安装并配置django_fil
- MatrixOne是一个新一代超融合异构数据库,致力于打造单一架构处理TP、AP、流计算等多种负载的极简大数据引擎。MatrixOne由Go
- 前言:本文为 HITwh 网络空间安全专业网络空间安全设计与实践 I 的选题之一,主要实现了远程监控局域网内的主机桌面与网络情况、简单键鼠控
- httpwatch 的页面元素加载时间表里面有一堆的英文,平时也没注意看,今天瞟了一眼,觉得应该有些用处,就看了看,随便用蹩脚的英语水平翻译
- 环境与开发工具在抓包的时候,开始使用的是Chrome开发工具中的Network,结果没有抓到,后来使用Fiddler成功抓取数据。下面逐步来
- 吐槽一下企业微信的api文档真的不好读······企业微信本来是有功能,可以直接把图片显示到正文的,但是那个api我调用一直出错,各种折腾也
- 一、基本用法1.注释Python中,#+语句 即为一条注释,也可以用 ‘'‘注释块 '''#人生苦短,我用P
- 我的风格,废话不多说了,直接给大家贴代码了,并在一些难点上给大家附了注释,具体代码如下所示:#!/usr/bin/env python#-*