-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLogger.py
114 lines (93 loc) · 4.18 KB
/
Logger.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- coding: utf-8 -*-
""" Script by Toybich Egor
"""
from __future__ import print_function
from datetime import datetime
from datetime import timedelta
__version__ = '1.1.0'
class Logger(object):
"""
A class to write into a log file
Args:
filename: str; name of a log file
alwaysnew: bool; always start a new log file if a new logger is created with
the same log file
info: str; add this strs at the beginning of each message
console: bool; duplicate output to stdout
"""
__version__ = '1.1.0'
__files_in_use = set()
__total_instances = 0
# ---------------------------------------------------------------
# Public attributes
# ---------------------------------------------------------------
@property
def total_instances(self):
"""How many loggers of this class were opened"""
return self.__total_instances
@property
def files_in_use(self):
"""Log files used by this logger class"""
return self.__files_in_use
@property
def filename(self):
"""Name of a log file for this class instance"""
return self.__filename
# ---------------------------------------------------------------
# Magic methods
# ---------------------------------------------------------------
def __init__(self, filename='log.txt', alwaysnew=False, info=None, console=True):
self.__info = info
self._console = console
if alwaysnew or filename not in self.__class__.__files_in_use:
with open(filename, 'w'): pass
self.__filename = filename
self.__files_in_use.add(filename)
self.__total_instances += 1
self.__logger_instance = self.__total_instances
self.__init_time = datetime.now()
self._log_('New Logger instance created ({})'.format(self.__total_instances))
self._log_('Class version: ' + self.__version__ ,1)
def __repr__(self):
return self.__filename
# ---------------------------------------------------------------
# Public methods
# ---------------------------------------------------------------
def runtime(self):
"""Prints a total time since creation of this object"""
t_run = datetime.now() - self.__init_time
t_run = timedelta(days=t_run.days, seconds=t_run.seconds, microseconds=0)
self._log_('Runtime: {}'.format(t_run), 1)
def blank(self):
"""Prints an empty line in a log file"""
with open(self.__filename, 'a') as log_file:
log_file.write('\n')
def log(self, msg_log, newln=0, info=''):
"""
Use this method to write into log file
Args:
msg_log: str; log message
newln: int; if 1 - add an empty line after message,
if 2 - print raw message in a log file
info: add this at message at the beginning of each line; default to self.info
"""
info_msg = info if self.__info is None or info != '' else self.__info
info_msg = info_msg + '|| ' if info_msg else ''
msg_time='[{}]'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
msg_actual = str(msg_log).splitlines() if newln != 2 else str(msg_log)
with open(self.__filename, 'a') as log_file:
if newln == 2:
log_file.write(msg_actual)
if self._console: print(msg_actual, end='')
else:
for msg in msg_actual:
mf = '({}) {} {}{}\n'.format(self.__logger_instance, msg_time, info_msg, msg)
log_file.write(mf)
if self._console: print('{} {}{}'.format(msg_time, info_msg, msg))
if newln == 1: log_file.write('\n')
def _log_(self, msg, newline=0):
"""Prints message with info of Logger class"""
self.log(msg, newline, info = str(self.__class__.__name__))
# ---------------------------------------------------------------
# Static methods
# ---------------------------------------------------------------