-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathLogger.py
More file actions
59 lines (54 loc) · 2.01 KB
/
Logger.py
File metadata and controls
59 lines (54 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#------------------------------------------------------
#file:logger.py
#desc: Record log information
#author:we.agathe@gmail.com
#------------------------------------------------------
import sys
import traceback
import time
import os
try:
class Logger(object):
def __init__(self, path='./logs'):
ft = time.strftime("%Y%m%d-%H%M%S", time.localtime(time.time()))
self._day =time.strftime("%Y%m%d", time.localtime(time.time()))
self._file = None
self._path = path
if not os.path.exists(self._path):
#os.makedirs(path.decode('utf8','ignore').encode(sys.getfilesystemencoding()))
os.makedirs(self._path)
self.fn = os.path.join(self._path,'log-%s.txt') % ft
def checktime(self, mytime):
today = time.strftime("%Y%m%d", mytime)#
if today != self._day:
ft = time.strftime("%Y%m%d-%H%M%S", mytime)
self.fn = os.path.join(self._path,'log-%s.txt') % ft
self._day = today
self._file.close()
self._file = None
def write(self, s):
curent_time = time.localtime(time.time())
self.checktime(curent_time)
if self._file is None:
try:
self._file= open(Logger.fn,'a')
except:
traceback.print_exc()
if self._file is not None:
t = '['+ time.strftime("%Y-%m-%d %H:%M:%S", curent_time) +']'
txt = s.replace('\n','')
txt = txt.replace('\r','')
c=t + s + '\n'
try:
self._file.write(c.decode('utf8'))
self._file.flush()
except:
self._file.write(c)
self._file.flush()
def flush(self):
if self._file is not None:
self._file.flush()
except:
traceback.print_exc()