-
Notifications
You must be signed in to change notification settings - Fork 5
/
IRCLogging.py
131 lines (97 loc) · 5.7 KB
/
IRCLogging.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import time
import datetime
import Queue
import os
import logging
import inspect
class LoggingModule():
def __init__(self, loglevel = "INFO"):
self.typeDict = {"NOTSET" : logging.NOTSET, "DEBUG" : logging.DEBUG, "INFO" : logging.INFO,
"WARNING" : logging.WARNING, "ERROR" : logging.ERROR, "CRITICAL" : logging.CRITICAL}
self.logLevel = self.typeDict[loglevel.upper()]
self.__create_directory__("BotLogs")
self.date = datetime.date.today()
# Each month, a new folder is created
# Each day, a new file is created in the folder
monthfoldername = self.date.strftime("%Y-%m")
dayfilename = self.date.strftime("%d-%m-%Y")
print monthfoldername, dayfilename
self.__create_directory__("BotLogs/{0}".format(monthfoldername))
logging.basicConfig(filename="BotLogs/{0}/{1}.log".format(monthfoldername, dayfilename),level=self.logLevel,
format='%(asctime)s [%(levelname)s] --[%(name)s:%(module)s/%(funcName)s]-- %(message)s', datefmt='%H:%M:%S')
## We create a console handler and configure it
consoleHandler = logging.StreamHandler()
consoleHandler.setLevel(self.logLevel)
format = logging.Formatter('%(asctime)s [%(levelname)s] --[%(name)s:%(module)s/%(funcName)s]-- %(message)s', datefmt='%H:%M:%S')
consoleHandler.setFormatter(format)
logging.getLogger().addHandler(consoleHandler)
## console handler configured, we should be good to go
self.__log_logger__ = logging.getLogger("LoggingModule")
self.__log_logger__.info("IRC Bot Logging Interface initialised.")
offset, tzname = self.local_time_offset()
if offset >= 0: offset = "+"+str(offset)
else: offset = str(offset)
self.__log_logger__.info("All time stamps are in UTC%s (%s)", offset, tzname)
self.num = 0
def __create_directory__(self, dirpath):
if not os.path.exists(dirpath):
os.mkdir(dirpath)
print "created dir"
elif not os.path.isdir(dirpath):
raise RuntimeError("A file with the path {0} already exists, please delete or rename it.".format(dirpath))
else:
print "no dir needs to be created"
pass
def __switch_filehandle_daily__(self, *args):
newDate = datetime.date.today()
if self.date < newDate:
self.__log_logger__.info("Switching Logfile Handler because a new day begins. Old date: %s, new date: %s", self.date, newDate)
monthfoldername = newDate.strftime("%Y-%m")
dayfilename = newDate.strftime("%d-%m-%Y")
if self.date.month != newDate.month:
self.__log_logger__.info("Creating new folder BotLogs/{0}".format(monthfoldername))
self.__create_directory__("BotLogs/{0}".format(monthfoldername))
self.__log_logger__.info("Creating new file BotLogs/{0}/{1}.log".format(monthfoldername, dayfilename))
# We close the file handle of the root logger.
# This should also affect all child loggers
#
# POTENTIAL BUG: is handlers[0] always the file handler pointing to the log file?
# Should be tested with more than one handler.
print logging.getLogger().handlers
logging.getLogger().handlers[0].stream.close()
logging.getLogger().removeHandler(logging.getLogger().handlers[0])
filename="BotLogs/{0}/{1}.log".format(monthfoldername, dayfilename)
# We create a new file handler with the options we used in __init__ which we add to the root logger
# This should affect all custom loggers created in plugins, but it needs more testing
newfile_handler = logging.FileHandler(filename)
newfile_handler.setLevel = self.logLevel
msgformat = logging.Formatter('%(asctime)s [%(levelname)s] --[%(name)s:%(module)s/%(funcName)s]-- %(message)s', datefmt='%H:%M:%S')
newfile_handler.setFormatter(msgformat)
#logging.getLogger().addHandler(newfile_handler)
self.__prependHandler__(logging.getLogger(), newfile_handler)
self.__log_logger__.info("Logfile Handler switched. Continuing writing to new file. Old date: %s, new date: %s", self.date, newDate)
offset, tzname = self.local_time_offset()
if offset >= 0: offset = "+"+str(offset)
else: offset = str(offset)
self.__log_logger__.info("All time stamps are in UTC%s (%s)", offset, tzname)
self.date = newDate
# Returns UTC offset and name of time zone at current time
# Based on http://stackoverflow.com/a/13406277
# Thanks a lot, marr75!
def local_time_offset(self):
t = time.time()
if time.localtime(t).tm_isdst and time.daylight:
return -time.altzone/3600, time.tzname[1]
else:
return -time.timezone/3600, time.tzname[0]
# Modification of logging's appendHandler function
# It will add the handler to the front of the handler list
# It is a bit of a hack, but it is required for making sure
# that the main file handler is added to the front of the list
def __prependHandler__(self, logger, hdlr):
logging._acquireLock()
try:
if not (hdlr in logger.handlers):
logger.handlers.insert(0, hdlr)
finally:
logging._releaseLock()