|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # -*- coding:cp949 -*- |
3 | | -import glob |
4 | 3 | import logging |
5 | 4 | import logging.handlers |
6 | 5 |
|
| 6 | + |
7 | 7 | class Singleton(type): |
8 | | - _instances = {} |
| 8 | + _instances = {} |
9 | 9 |
|
10 | | - def __call__(cls, *args, **kwargs): |
11 | | - if cls not in cls._instances.keys(): |
12 | | - cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) |
13 | | - return cls._instances[cls] |
| 10 | + def __call__(cls, *args, **kwargs): |
| 11 | + if cls not in cls._instances.keys(): |
| 12 | + cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) |
| 13 | + return cls._instances[cls] |
14 | 14 |
|
15 | 15 |
|
16 | 16 | class LOG(object): |
17 | | - __metaclass__ = Singleton |
18 | | - |
19 | | - _loggers = {} |
20 | | - |
21 | | - def __init__(self, *args, **kwargs): |
22 | | - pass |
23 | | - |
24 | | - @staticmethod |
25 | | - def getLogger(name=None, ROLE=""): |
26 | | - name = name+ROLE |
27 | | - if name not in LOG._loggers.keys(): |
28 | | - LOG_FILENAME = ROLE + '_logging_rotatingfile.log' |
29 | | - logging.basicConfig(level=logging.DEBUG, |
30 | | - format='%(asctime)s %(threadName)-12s %(name)-16s %(levelname)-6s {%(filename)-12s %(lineno)-4d} %(message)-100s', |
31 | | - datefmt='%m-%d %H:%M', |
32 | | - filename=LOG_FILENAME, |
33 | | - filemode='w') |
34 | | - |
35 | | - # Add the log message handler to the logger |
36 | | - handler = logging.handlers.RotatingFileHandler( |
37 | | - LOG_FILENAME, maxBytes=1073741824, backupCount=2) |
38 | | - |
39 | | - console = logging.StreamHandler() |
40 | | - formatter = logging.Formatter('%(asctime)s %(threadName)-14s %(name)-19s %(message)s') |
41 | | - console.setLevel(logging.INFO) |
42 | | - console.setFormatter(formatter) |
43 | | - |
44 | | - formatter = logging.Formatter('%(asctime)s - %(name)16s - %(message)s') |
45 | | - LOG._loggers[name] = logging.getLogger(str(name)) |
46 | | - LOG._loggers[name].setLevel(logging.DEBUG) |
47 | | - LOG._loggers[name].addHandler(handler) |
48 | | - handler.setFormatter(formatter) |
49 | | - LOG._loggers[name].addHandler(console) |
50 | | - |
51 | | - return LOG._loggers[name] |
| 17 | + __metaclass__ = Singleton |
| 18 | + |
| 19 | + _loggers = {} |
| 20 | + |
| 21 | + def __init__(self, *args, **kwargs): |
| 22 | + pass |
| 23 | + |
| 24 | + @staticmethod |
| 25 | + def getLogger(role=None, filename="", testlogger=False, filepath=""): |
| 26 | + role += filename |
| 27 | + |
| 28 | + if role not in LOG._loggers.keys(): |
| 29 | + if testlogger: |
| 30 | + log_filename = filepath + filename + '_logging_rotatingfile.log' |
| 31 | + else: |
| 32 | + log_filename = filename + '_logging_rotatingfile.log' |
| 33 | + |
| 34 | + logging.basicConfig(level=logging.DEBUG, |
| 35 | + format='%(asctime)s %(threadName)-12s %(name)-16s %(levelname)-6s {%(filename)-12s %(lineno)-4d} %(message)-100s', |
| 36 | + datefmt='%m-%d %H:%M', |
| 37 | + filename=log_filename, |
| 38 | + filemode='w') |
| 39 | + |
| 40 | + # Add the log message handler to the logger |
| 41 | + handler = logging.handlers.RotatingFileHandler( |
| 42 | + log_filename, maxBytes=1073741824, backupCount=2) |
| 43 | + |
| 44 | + console = logging.StreamHandler() |
| 45 | + formatter = logging.Formatter('%(asctime)s %(threadName)-14s %(name)-19s %(message)s') |
| 46 | + console.setLevel(logging.INFO) |
| 47 | + console.setFormatter(formatter) |
| 48 | + |
| 49 | + formatter = logging.Formatter('%(asctime)s %(levelname)-s : %(message)-100s','%m-%d %H:%M') |
| 50 | + LOG._loggers[role] = logging.getLogger(str(role)) |
| 51 | + LOG._loggers[role].setLevel(logging.DEBUG) |
| 52 | + |
| 53 | + LOG._loggers[role].addHandler(handler) |
| 54 | + handler.setFormatter(formatter) |
| 55 | + LOG._loggers[role].addHandler(console) |
| 56 | + |
| 57 | + return LOG._loggers[role] |
| 58 | + |
| 59 | + |
| 60 | + @staticmethod |
| 61 | + def removeLogger(role, filename): |
| 62 | + role += filename |
| 63 | + if role in LOG._loggers.keys(): |
| 64 | + del LOG._loggers[role] |
52 | 65 |
|
0 commit comments