clockwork 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- clockwork/__init__.py +4 -0
- clockwork/chronicle.py +97 -0
- clockwork/clockwork.py +780 -0
- clockwork/taskmaster.py +537 -0
- clockwork-0.1.0.dist-info/LICENSE +21 -0
- clockwork-0.1.0.dist-info/METADATA +26 -0
- clockwork-0.1.0.dist-info/RECORD +8 -0
- clockwork-0.1.0.dist-info/WHEEL +4 -0
clockwork/__init__.py
ADDED
clockwork/chronicle.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import datetime
|
|
3
|
+
from pathpilot import Folder
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class CustomLogFormatter(logging.Formatter):
|
|
7
|
+
''' the default implementation of logging.Formatter does not allow timestamps to be formatted how I want '''
|
|
8
|
+
|
|
9
|
+
converter = datetime.datetime.fromtimestamp
|
|
10
|
+
|
|
11
|
+
def formatTime(self, record, datefmt=None):
|
|
12
|
+
if datefmt is not None: raise TypeError('datefmt argument must be None')
|
|
13
|
+
return self.converter(record.created).strftime('%Y-%m-%d %I:%M:%S.{} %p').format('%03d' % record.msecs)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Logger(object):
|
|
18
|
+
|
|
19
|
+
instances = {}
|
|
20
|
+
|
|
21
|
+
@classmethod
|
|
22
|
+
def load(cls, name, *args, **kwargs):
|
|
23
|
+
if name not in cls.instances:
|
|
24
|
+
cls.instances[name] = cls(name, *args, **kwargs)
|
|
25
|
+
return cls.instances[name]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def __init__(self, name, clear=False, stream_handler=False):
|
|
29
|
+
self.name = name
|
|
30
|
+
|
|
31
|
+
# create logger
|
|
32
|
+
logger = logging.getLogger(name)
|
|
33
|
+
logger.setLevel(logging.DEBUG)
|
|
34
|
+
|
|
35
|
+
# create custom formatter
|
|
36
|
+
# https://docs.python.org/3/library/logging.html#logrecord-attributes
|
|
37
|
+
formatter = CustomLogFormatter(fmt='%(asctime)s %(levelname)s %(message)s')
|
|
38
|
+
#formatter = CustomLogFormatter(fmt='%(asctime)s %(name)s %(levelname)s %(message)s')
|
|
39
|
+
|
|
40
|
+
# create file handler which logs even debug messages
|
|
41
|
+
self.file = Folder().parent.join('Data', 'Logger', read_only=False).join(f'{name}.log').path
|
|
42
|
+
if clear: self.clear()
|
|
43
|
+
fh = logging.FileHandler(self.file)
|
|
44
|
+
fh.setLevel(logging.DEBUG)
|
|
45
|
+
fh.setFormatter(formatter)
|
|
46
|
+
logger.addHandler(fh) # add handler to the logger
|
|
47
|
+
|
|
48
|
+
# create console handler with a higher log level
|
|
49
|
+
if stream_handler:
|
|
50
|
+
ch = logging.StreamHandler()
|
|
51
|
+
ch.setLevel(logging.ERROR)
|
|
52
|
+
ch.setFormatter(formatter)
|
|
53
|
+
logger.addHandler(ch) # add handler to the logger
|
|
54
|
+
|
|
55
|
+
# disable logging to console
|
|
56
|
+
logger.propagate = False
|
|
57
|
+
|
|
58
|
+
self.logger = logger
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def __getitem__(self, name):
|
|
62
|
+
return self.__dict__[name].logger
|
|
63
|
+
|
|
64
|
+
def __repr__(self):
|
|
65
|
+
return self.file
|
|
66
|
+
|
|
67
|
+
def __str__(self):
|
|
68
|
+
return self.file
|
|
69
|
+
|
|
70
|
+
def clear(self):
|
|
71
|
+
open(self.file, 'w').close()
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def log(logger=None):
|
|
76
|
+
''' Logs decorated function using the passed logging.Logger object.
|
|
77
|
+
If None, a logger object is created (or loaded if already exists)
|
|
78
|
+
using the decorated function's name. '''
|
|
79
|
+
def decorator(func):
|
|
80
|
+
def wrapper(*args, **kwargs):
|
|
81
|
+
nonlocal logger
|
|
82
|
+
logger = logger or Logger.load(func.__name__).logger
|
|
83
|
+
logger.info('start')
|
|
84
|
+
try:
|
|
85
|
+
out = func(*args, **kwargs)
|
|
86
|
+
logger.info('complete')
|
|
87
|
+
return out
|
|
88
|
+
except Exception as e:
|
|
89
|
+
logger.exception('exception')
|
|
90
|
+
return e
|
|
91
|
+
return wrapper
|
|
92
|
+
return decorator
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
if __name__ == '__main__':
|
|
97
|
+
pass
|