-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.py
More file actions
54 lines (39 loc) · 1.29 KB
/
Copy pathlogger.py
File metadata and controls
54 lines (39 loc) · 1.29 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
"""Define the Logger class.
This class allows to create objects that would be used as standard
and error output. Behind it are files so the message writen in it
will be writen in a file.
By Sylvain MAUDUIT (Swop)
"""
from datetime import datetime
class Logger:
"""Represent a logger, meaning an object in which you can write.
Attributes:
path -- files's path to write
file -- the file object
Methods:
write -- write in the file object
Properties:
now -- return the formated date and time
"""
def __init__(self, path):
self.path = path
self.file = open(self.path, "a")
self.__nl = True
def write(self, message):
"""Write in the file."""
if self.__nl:
now = self.now
self.file.write(now + " ")
self.file.write(message)
self.file.flush()
self.__nl = message.endswith("\n")
@property
def now(self):
"""Return the date and time."""
now = datetime.now()
ret = "{0}-{1:02}-{2:02} {3:02}:{4:02}:{5:02}".format(
now.year, now.month, now.day, now.hour, now.minute, now.second)
return ret
def flush(self):
"""Flush the file."""
self.file.flush()