-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
82 lines (59 loc) · 2.3 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
#!/usr/bin/python3
"""
PyLogger
______________
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Project Author/Architect: Navjot Singh <[email protected]>
"""
import logging
import os
import datetime
#
# A singleton logger that will be used globally by the project
# All the log files are created inside ./logs/ dir with current date
#
class SingletonType(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(
SingletonType, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class Logger(object, metaclass=SingletonType):
_logger = None
def __init__(self):
self._logger = logging.getLogger("crumbs")
self._logger.setLevel(logging.DEBUG)
# creating a logging format
fmt = "[%(levelname)s] %(asctime)s :: %(filename)s:%(lineno)d -" \
" %(funcName)s() | %(message)s"
formatter = logging.Formatter(fmt)
# ensuring that logs dir exist
now = datetime.datetime.now()
dirname = "./logs"
if not os.path.isdir(dirname):
os.mkdir(dirname)
# setting handlers
fileHandler = logging.FileHandler(
dirname + "/log_" + now.strftime("%Y-%m-%d")+".log")
streamHandler = logging.StreamHandler()
fileHandler.setFormatter(formatter)
streamHandler.setFormatter(formatter)
self._logger.addHandler(fileHandler)
self._logger.addHandler(streamHandler)
def get_logger():
return Logger.__call__()._logger
if __name__ == "__main__":
logger = get_logger()
logger.debug("some debug log")
logger.info("Hello Navi")
logger.warning("here is a warning !!!")