1 |
slacapra |
1.1 |
import logging,time
|
2 |
|
|
import common
|
3 |
|
|
|
4 |
|
|
class CrabLogger:
|
5 |
|
|
def __init__(self, args):
|
6 |
|
|
|
7 |
|
|
# print 'Creating LOGGER',logging._handlers
|
8 |
|
|
logging.DEBUG_VERBOSE = logging.DEBUG - 1
|
9 |
|
|
logging.addLevelName(logging.DEBUG_VERBOSE,'debug_verbose')
|
10 |
|
|
logging.root.setLevel([logging.DEBUG_VERBOSE, logging.DEBUG, logging.INFO, \
|
11 |
|
|
logging.WARNING,logging.ERROR, logging.CRITICAL])
|
12 |
|
|
|
13 |
|
|
self.logger = logging.getLogger("crab:")
|
14 |
|
|
self.logger.setLevel(logging.DEBUG_VERBOSE)
|
15 |
|
|
|
16 |
|
|
# FileHandler
|
17 |
|
|
log_fname =common.work_space.logDir()+common.prog_name+'.log'
|
18 |
|
|
self.fh=logging.FileHandler(log_fname)
|
19 |
|
|
fh_formatter = logging.Formatter("%(asctime)s [%(levelname)s] \t%(message)s")
|
20 |
|
|
fh_level=logging.DEBUG
|
21 |
|
|
self.fh.setLevel(fh_level)
|
22 |
|
|
self.fh.setFormatter(fh_formatter)
|
23 |
|
|
self.logger.addHandler(self.fh)
|
24 |
|
|
|
25 |
|
|
# StreamerHandler: to be dded _only_ if not already present: otherwise duplicated
|
26 |
|
|
streamenPresent=False
|
27 |
|
|
for x in self.logger.handlers:
|
28 |
|
|
if x.__class__==logging.StreamHandler: streamenPresent=True
|
29 |
|
|
if not streamenPresent:
|
30 |
|
|
self.ch=logging.StreamHandler()
|
31 |
|
|
ch_formatter = logging.Formatter("%(name)s %(message)s")
|
32 |
|
|
ch_level=logging.INFO
|
33 |
|
|
if common.debugLevel > 0:ch_level=logging.DEBUG
|
34 |
|
|
if common.debugLevel > 2:
|
35 |
|
|
fh_level=logging.DEBUG_VERBOSE
|
36 |
|
|
ch_level=logging.DEBUG_VERBOSE
|
37 |
|
|
|
38 |
|
|
self.ch.setLevel(ch_level)
|
39 |
|
|
self.ch.setFormatter(ch_formatter)
|
40 |
|
|
|
41 |
|
|
# add StreamerHandler only if is not yet there
|
42 |
|
|
self.logger.addHandler(self.ch)
|
43 |
|
|
|
44 |
|
|
self.debug('%s\n'%args)
|
45 |
|
|
# print 'LOGGER',logging._handlers
|
46 |
|
|
return
|
47 |
|
|
|
48 |
|
|
def __call__(self):
|
49 |
|
|
return self.logger
|
50 |
|
|
|
51 |
|
|
def delete(self):
|
52 |
|
|
# The trick her is to flush, close, remove from handler list and finally delete _only_ the FileHandler,
|
53 |
|
|
# NOT the StreamerHandler as well, since it is apparently used asynchrounously and will give error in emit(...)
|
54 |
|
|
if self.fh in logging._handlers :
|
55 |
|
|
self.fh.flush()
|
56 |
|
|
self.fh.close()
|
57 |
|
|
self.logger.removeHandler(self.fh)
|
58 |
|
|
del self.fh
|
59 |
|
|
common.logger=None
|
60 |
|
|
|
61 |
|
|
def info(self, msg):
|
62 |
|
|
#print msg
|
63 |
|
|
self.logger.info(msg)
|
64 |
|
|
|
65 |
|
|
def debug(self, msg):
|
66 |
|
|
#print msg
|
67 |
|
|
self.logger.debug(msg)
|
68 |
|
|
|
69 |
|
|
def log(self,i, msg):
|
70 |
|
|
#print msg
|
71 |
|
|
self.logger.log(i,msg)
|
72 |
|
|
|