Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(48)

Unified Diff: plaso/lib/loggers.py

Issue 331400043: [plaso] Added CompressedFileHandler for logging #1549 (Closed)
Patch Set: Changes after review and merge Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « plaso/engine/configurations.py ('k') | plaso/multi_processing/base_process.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: plaso/lib/loggers.py
diff --git a/plaso/lib/loggers.py b/plaso/lib/loggers.py
new file mode 100644
index 0000000000000000000000000000000000000000..2daf53de618d44bf30d2abb22f1cd425430aa3d5
--- /dev/null
+++ b/plaso/lib/loggers.py
@@ -0,0 +1,89 @@
+# -*- coding: utf-8 -*-
+"""Logging related classes and functions."""
+
+from __future__ import unicode_literals
+
+import gzip
+import logging
+
+
+class CompressedFileHandler(logging.FileHandler):
+ """Compressed file handler for logging."""
+
+ def __init__(self, filename, mode='a', encoding=None):
+ """Initializes a compressed file logging handler.
+
+ Args:
+ filename (str): name of the log file.
+ mode (Optional[str]): file access mode.
+ encoding (Optional[str]): encoding of the log lines.
+ """
+ super(CompressedFileHandler, self).__init__(
+ filename, mode=mode, encoding=encoding, delay=True)
+
+ def _open(self):
+ """Opens the compressed log file.
+
+ Returns
+ file: file-like object of the resulting stream.
+ """
+ # The gzip module supports directly setting encoding as of Python 3.3.
+ return gzip.open(self.baseFilename, self.mode)
+
+ def emit(self, record):
+ """Emits a record.
+
+ Args:
+ record (logging.LogRecord): log record.
+ """
+ if self.encoding:
+ record = record.encode(self.encoding)
+
+ super(CompressedFileHandler, self).emit(record)
+
+
+def ConfigureLogging(
+ debug_output=False, filename=None, mode='w', quiet_mode=False):
+ """Configures the logging root logger.
+
+ Args:
+ debug_output (Optional[bool]): True if the logging should include debug
+ output.
+ filename (Optional[str]): log filename.
+ mode (Optional[str]): log file access mode.
+ quiet_mode (Optional[bool]): True if the logging should not include
+ information output. Note that debug_output takes precedence over
+ quiet_mode.
+ """
+ # Remove all possible log handlers. The log handlers cannot be reconfigured
+ # and therefore must be recreated.
+ for handler in logging.root.handlers:
+ logging.root.removeHandler(handler)
+
+ logger = logging.getLogger()
+
+ if filename and filename.endswith('.gz'):
+ handler = CompressedFileHandler(filename, mode=mode)
+ elif filename:
+ handler = logging.FileHandler(filename, mode=mode)
+ else:
+ handler = logging.StreamHandler()
+
+ format_string = (
+ '%(asctime)s [%(levelname)s] (%(processName)-10s) PID:%(process)d '
+ '<%(module)s> %(message)s')
+
+ formatter = logging.Formatter(format_string)
+ handler.setFormatter(formatter)
+
+ if debug_output:
+ level = logging.DEBUG
+ elif quiet_mode:
+ level = logging.WARNING
+ else:
+ level = logging.INFO
+
+ logger.setLevel(level)
+ handler.setLevel(level)
+
+ logger.addHandler(handler)
« no previous file with comments | « plaso/engine/configurations.py ('k') | plaso/multi_processing/base_process.py » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b