Python simple log processing class sharing

Source: Internet
Author: User
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)

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.