A simple Python log processing class
Copy CodeThe code is as follows:
#/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)