Python简单日志处理类分享

yipeiwu_com6年前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)

相关文章

教你用Python写安卓游戏外挂

教你用Python写安卓游戏外挂

本次我们选择的安卓游戏对象叫“单词英雄”,大家可以先下载这个游戏。 游戏的界面是这样的: 通过选择单词的意思进行攻击,选对了就正常攻击,选错了就象征性的攻击一下。玩了一段时间之后琢磨可...

对python-3-print重定向输出的几种方法总结

方法1: import sys f=open('test.txt','a+') a='123' b='456' print >> f,a,b f.close...

解决python中用matplotlib画多幅图时出现图形部分重叠的问题

1.解决方法:使用函数 tight_layout() 2.具体使用方法 import matplotlib.pyplot as plt fig = plt.figure()...

pytorch 改变tensor尺寸的实现

改变Tensor尺寸的操作 1.tensor.view tensor.view方法,可以调整tensor的形状,但必须保证调整前后元素总数一致。view不会改变自身数据,返回的新的ten...

python使用xlsxwriter实现有向无环图到Excel的转换

python使用xlsxwriter实现有向无环图到Excel的转换

本程序将使用字典来构建有向无环图,然后遍历图将其转换为对应的Excel文件 最终结果如下: 代码: (py3) [root@7-o-1 py-dag]# cat test.py...