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)

相关文章

使用IDLE的Python shell窗口实例详解

使用IDLE的Python shell窗口实例详解

启动IDLE后会打开Python shell窗口。当键入代码 时,它会基于Python语法提供自动缩进和代码着色功能。 使用IDLE中的Python shell。代码在输入时会自动着...

Python内置函数Type()函数一个有趣的用法

今天在网上看到type的一段代码 ,然后查了一下文档,才知道type还有三个参数的用法。 http://docs.python.org/2/library/functions.html...

运行django项目指定IP和端口的方法

运行django项目指定IP和端口的方法

一、django项目启动命令 默认IP和端口 python manage.py runserver 指定端口 python manage.py runserver 192.1...

Python3 串口接收与发送16进制数据包的实例

如下所示: import serial import string import binascii s=serial.Serial('com4',9600) s.open() #接收...

python实现挑选出来100以内的质数

这里给大家分享的是使用python实现将100以内的质数挑选出来 代码非常简单,就不多废话了。 """ 使用filter将100以内的质数挑选出来 """ # coding =...