LEFT | RIGHT |
1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
2 """Custom logger.""" | 2 """Logging related classes and functions.""" |
3 | 3 |
4 from __future__ import unicode_literals | 4 from __future__ import unicode_literals |
5 | 5 |
6 import gzip | 6 import gzip |
7 import logging | 7 import logging |
8 | 8 |
9 | 9 |
10 class CompressedFileHandler(logging.FileHandler): | 10 class CompressedFileHandler(logging.FileHandler): |
11 """Compressed file handler for logging.""" | 11 """Compressed file handler for logging.""" |
12 | 12 |
13 def __init__(self, filename, mode='a', encoding=None): | 13 def __init__(self, filename, mode='a', encoding=None): |
14 """Initializes a compressed file logging handler. | 14 """Initializes a compressed file logging handler. |
15 | 15 |
16 Args: | 16 Args: |
17 filename (str): name of the log file. | 17 filename (str): name of the log file. |
18 mode (Optional[str]): file access mode. | 18 mode (Optional[str]): file access mode. |
19 encoding (Optional[str]): encoding of the log lines. | 19 encoding (Optional[str]): encoding of the log lines. |
20 """ | 20 """ |
21 super(CompressedFileHandler, self).__init__( | 21 super(CompressedFileHandler, self).__init__( |
22 filename, mode=mode, encoding=encoding, delay=True) | 22 filename, mode=mode, encoding=encoding, delay=True) |
23 | 23 |
24 def _open(self): | 24 def _open(self): |
25 """Opens the current base file with the (original) mode and encoding. | 25 """Opens the compressed log file. |
26 | 26 |
27 Returns | 27 Returns |
28 file: file-like object of the resulting stream. | 28 file: file-like object of the resulting stream. |
29 """ | 29 """ |
30 # The gzip module supports directly setting encoding as of Python 3.3. | 30 # The gzip module supports directly setting encoding as of Python 3.3. |
31 return gzip.open(self.baseFilename, self.mode) | 31 return gzip.open(self.baseFilename, self.mode) |
32 | 32 |
33 def emit(self, record): | 33 def emit(self, record): |
34 """Emits a record. | 34 """Emits a record. |
35 | 35 |
36 If the stream was not opened because 'delay' was specified in the | |
37 constructor, open it before calling the superclass's emit. | |
38 | |
39 Args: | 36 Args: |
40 record (logging.LogRecord): log record. | 37 record (logging.LogRecord): log record. |
41 """ | 38 """ |
42 if self.encoding: | 39 if self.encoding: |
43 record = record.encode(self.encoding) | 40 record = record.encode(self.encoding) |
44 | 41 |
45 super(CompressedFileHandler, self).emit(record) | 42 super(CompressedFileHandler, self).emit(record) |
| 43 |
| 44 |
| 45 def ConfigureLogging( |
| 46 debug_output=False, filename=None, mode='w', quiet_mode=False): |
| 47 """Configures the logging root logger. |
| 48 |
| 49 Args: |
| 50 debug_output (Optional[bool]): True if the logging should include debug |
| 51 output. |
| 52 filename (Optional[str]): log filename. |
| 53 mode (Optional[str]): log file access mode. |
| 54 quiet_mode (Optional[bool]): True if the logging should not include |
| 55 information output. Note that debug_output takes precedence over |
| 56 quiet_mode. |
| 57 """ |
| 58 # Remove all possible log handlers. The log handlers cannot be reconfigured |
| 59 # and therefore must be recreated. |
| 60 for handler in logging.root.handlers: |
| 61 logging.root.removeHandler(handler) |
| 62 |
| 63 logger = logging.getLogger() |
| 64 |
| 65 if filename and filename.endswith('.gz'): |
| 66 handler = CompressedFileHandler(filename, mode=mode) |
| 67 elif filename: |
| 68 handler = logging.FileHandler(filename, mode=mode) |
| 69 else: |
| 70 handler = logging.StreamHandler() |
| 71 |
| 72 format_string = ( |
| 73 '%(asctime)s [%(levelname)s] (%(processName)-10s) PID:%(process)d ' |
| 74 '<%(module)s> %(message)s') |
| 75 |
| 76 formatter = logging.Formatter(format_string) |
| 77 handler.setFormatter(formatter) |
| 78 |
| 79 if debug_output: |
| 80 level = logging.DEBUG |
| 81 elif quiet_mode: |
| 82 level = logging.WARNING |
| 83 else: |
| 84 level = logging.INFO |
| 85 |
| 86 logger.setLevel(level) |
| 87 handler.setLevel(level) |
| 88 |
| 89 logger.addHandler(handler) |
LEFT | RIGHT |