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
|
# -*- coding: utf-8 -*-
"""Just a simple logger
Author: G.J.J. van den Burg
License: See LICENSE file.
Copyright: 2019, G.J.J. van den Burg
"""
# NOTE: I know about the logging module, but this was easier because one of the
# dependencies was using that and it became complicated. This one is obviously
# not thread-safe and is very simple.
import datetime
import sys
class Singleton(type):
# https://stackoverflow.com/q/6760685
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(
*args, **kwargs
)
return cls._instances[cls]
class Logger(metaclass=Singleton):
def __init__(self):
self.enabled = True
def enable(self):
self.enabled = True
def disable(self):
self.enabled = False
def _log(self, msg, mode):
if not self.enabled:
return
if not mode in ("info", "warn"):
raise ValueError("Unknown logging mode: %s" % mode)
file = sys.stdout if mode == "info" else sys.stderr
now = datetime.datetime.now()
nowstr = now.strftime("%Y-%m-%d %H:%M:%S")
print("%s - %s - %s" % (nowstr, mode.upper(), msg), file=file)
file.flush()
def info(self, msg):
self._log(msg, "info")
def warning(self, msg):
self._log(msg, "warn")
|