Python简单日志处理类分享

yipeiwu_com5年前Python基础

简单的一个python日志处理类

复制代码 代码如下:

#/usr/bin/python
#coding=utf-8

import time,types

class logsys:

    def __init__(self, project, logfilename = 'sys_log.txt'):
        self.project = project
        self.logfilename = logfilename

    def get_log_time(self):
        return time.strftime("%Y-%m-%d %X", time.localtime())

    def write2file(self, *formart):
        s = self.formart_string(*formart)
        if s:
            encoding = 'utf8'
            out = open(self.logfilename, 'a+')
            out.write(s + "\n")
            out.close()
        else:
            pass

    def formart_string(self, *formart):
        string = ''
        encoding = 'utf8'
        for str in formart:
            if not type(str) in [types.UnicodeType, types.StringTypes, types.StringType]:
                s = repr(str)
            else:
                s = str
            if type(s) == type(u''):
                string += s.encode(encoding) + "\t"
            else:
                string += s + "\t"
        return string

    def w(self,notice,*formart):
        self.write2file(self.get_log_time(), '[' + notice + ']', self.project, *formart)

相关文章

django删除表重建的实现方法

正确的方法如下: 先到数据库把表删掉:drop table 注释django中对应的Model 执行以下命令: python manage.py makemigrations p...

PyQt5实现让QScrollArea支持鼠标拖动的操作方法

PyQt5实现让QScrollArea支持鼠标拖动的操作方法

如下所示: #!/usr/bin/evn python3 # -*- coding: utf-8 -*- import sys from PyQt5.QtWidgets impo...

python实现跨文件全局变量的方法

在使用Python编写的应用的过程中,有时候会遇到多个文件之间传递同一个全局变量的情况。本文就此给出了如下的解决方法供大家参考。 文件1:globalvar.py #!/usr/bi...

解决Matplotlib图表不能在Pycharm中显示的问题

解决Matplotlib图表不能在Pycharm中显示的问题

初学者可能都会遇到一个小问题就是:在用IPython的时候,可以使用类似 %matplotlib inline 的Magic Function(魔法函数)来显示Matplotlib...

python3 中文乱码与默认编码格式设定方法

python默认编码格式是utf-8。在python2.7中,可以通过sys.setdefaultencoding('gbk')设定默认编码格式,而在python3.3中sys.setd...