Python simple log processing class sharing

Source: Internet
Author: User
This article mainly introduces the Python simple log processing class. This article provides the implementation code directly. if you need it, you can refer to a simple python log processing class.

The 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.