-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlogfile.py
52 lines (46 loc) · 1.74 KB
/
logfile.py
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
import os
import datetime
from logging.handlers import RotatingFileHandler
class RotatingFile(RotatingFileHandler):
def write(self, buf):
try:
stream = self.stream
buf = buf + '\n'
if (isinstance(buf, unicode) and
getattr(stream, 'encoding', None)):
buf = buf.decode(stream.encoding)
try:
stream.write(buf)
except UnicodeEncodeError:
#Printing to terminals sometimes fails. For example,
#with an encoding of 'cp1251', the above write will
#work if written to a stream opened or wrapped by
#the codecs module, but fail when writing to a
#terminal even when the codepage is set to cp1251.
#An extra encoding step seems to be needed.
stream.write((buf).encode(stream.encoding))
else:
stream.write(buf)
self.flush()
except (KeyboardInterrupt, SystemExit):
raise
except:
import traceback
traceback.print_exc()
def fileno(self):
return self.stream.fileno()
def info(self, msg):
msg = '[%s] %s' % (str(datetime.datetime.now()), msg)
self.write(msg)
def _open(self):
"""
Open the current base file with the (original) mode and encoding.
Return the resulting stream.
"""
import codecs
if self.encoding is None:
stream = open(self.baseFilename, self.mode)
else:
stream = codecs.open(self.baseFilename, self.mode, self.encoding)
stream.seek(0, os.SEEK_END)
return stream