Python simple log processing class sharing, python log sharing
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)