-
Notifications
You must be signed in to change notification settings - Fork 15
/
logconfig.py
41 lines (34 loc) · 1.28 KB
/
logconfig.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
#!/usr/bin/env python
__author__ = 'Jovan Brakus <[email protected]>'
__contact__ = '[email protected]'
__date__ = '31 May 2012'
import os
import sys
import logging
import logging.handlers
DEFAULT_LOG_LEVEL = logging.DEBUG
LOG_DIR = 'logs'
LOG_FILENAME = 'cherrypy-server.log'
def init_logging():
# get root and readings logger, and remove default handlers -- we set our own
try:
if not os.path.exists(LOG_DIR):
print "Log folder %s doesn't exist. Creating it..." % LOG_DIR
os.makedirs(LOG_DIR)
except:
print 'Failed to create log directory: %s' % LOG_DIR
sys.exit(2)
root_log = logging.getLogger()
if root_log.handlers:
for handler in root_log.handlers:
root_log.removeHandler(handler)
root_handler = logging.handlers.RotatingFileHandler(os.path.join(LOG_DIR, LOG_FILENAME), maxBytes=20971520, backupCount=50)
root_formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s")
root_handler.setFormatter(root_formatter)
root_log.addHandler(root_handler)
root_log.setLevel(DEFAULT_LOG_LEVEL)
#Run log initialization function only once, first time module is loaded into memory
logInitDone=False
if not logInitDone:
logInitDone = True
init_logging()