Python簡單日志處理類分享
簡單的一個(gè)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)
相關(guān)文章
python3 設(shè)置多進(jìn)程名稱并在ps命令中可見(Centos7 系統(tǒng))
setproctitle 是一個(gè) Python 模塊,用于設(shè)置進(jìn)程標(biāo)題(process title),通過設(shè)置進(jìn)程標(biāo)題,可以讓進(jìn)程在系統(tǒng)級(jí)的進(jìn)程管理工具中展示自定義的名稱,方便用戶查看和管理進(jìn)程,本文介紹python3 設(shè)置多進(jìn)程名稱并在ps命令中可見,感興趣的朋友一起看看吧2024-03-03
Tornado協(xié)程在python2.7如何返回值(實(shí)現(xiàn)方法)
下面小編就為大家?guī)硪黄猅ornado協(xié)程在python2.7如何返回值(實(shí)現(xiàn)方法)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
python實(shí)現(xiàn)中文轉(zhuǎn)換url編碼的方法
這篇文章主要介紹了python實(shí)現(xiàn)中文轉(zhuǎn)換url編碼的方法,結(jié)合實(shí)例形式分析了Python針對(duì)中文的gbk與utf-8編碼轉(zhuǎn)換的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
python 使用正則表達(dá)式按照多個(gè)空格分割字符的實(shí)例
今天小編就為大家分享一篇python 使用正則表達(dá)式按照多個(gè)空格分割字符的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12

