[Pkg-privacy-commits] [onionbalance] 114/117: Add option to output log messages to a log file

Donncha O'Cearbahill donncha-guest at moszumanska.debian.org
Wed Dec 16 23:18:55 UTC 2015


This is an automated email from the git hooks/post-receive script.

donncha-guest pushed a commit to branch debian/sid
in repository onionbalance.

commit bb27f9393fb38924565ea436b77ad315e4b2aa4f
Author: Donncha O'Cearbhaill <donncha at donncha.is>
Date:   Wed Dec 16 19:18:37 2015 +0100

    Add option to output log messages to a log file
    
    Read environment variable or config file options to configure the location
    for the OnionBalance log file.
---
 onionbalance/config.py  |  4 ++++
 onionbalance/log.py     | 12 ++++++++++++
 onionbalance/manager.py | 12 ++++++++++--
 3 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/onionbalance/config.py b/onionbalance/config.py
index 458f502..44fbebd 100644
--- a/onionbalance/config.py
+++ b/onionbalance/config.py
@@ -1,4 +1,5 @@
 # -*- coding: utf-8 -*-
+import os
 
 """
 Define default config options for the management server
@@ -14,5 +15,8 @@ DESCRIPTOR_UPLOAD_PERIOD = 60 * 60  # Re-upload descriptor every hour
 REFRESH_INTERVAL = 10 * 60
 PUBLISH_CHECK_INTERVAL = 5 * 60
 
+LOG_LOCATION = os.environ.get('ONIONBALANCE_LOG_LOCATION')
+LOG_LEVEL = os.environ.get('ONIONBALANCE_LOG_LEVEL', 'info')
+
 # Store global data about onion services and their instance nodes.
 services = []
diff --git a/onionbalance/log.py b/onionbalance/log.py
index f498eac..1eb9400 100644
--- a/onionbalance/log.py
+++ b/onionbalance/log.py
@@ -1,5 +1,6 @@
 # -*- coding: utf-8 -*-
 import logging
+import logging.handlers
 
 handler = logging.StreamHandler()
 handler.setFormatter(logging.Formatter(fmt="%(asctime)s [%(levelname)s]: "
@@ -17,6 +18,17 @@ def get_logger():
     return logger
 
 
+def setup_file_logger(log_file):
+    """
+    Add log file handler to the existing logger
+    """
+    handler = logging.handlers.RotatingFileHandler(
+        log_file, maxBytes=10485760, backupCount=3)
+    handler.setFormatter(logging.Formatter(fmt="%(asctime)s [%(levelname)s]: "
+                                           "%(message)s"))
+    logging.getLogger('onionbalance').addHandler(handler)
+
+
 def get_config_generator_logger():
     """
     Simplified logger for interactive config generator CLI
diff --git a/onionbalance/manager.py b/onionbalance/manager.py
index cb47281..6e85e43 100644
--- a/onionbalance/manager.py
+++ b/onionbalance/manager.py
@@ -46,7 +46,7 @@ def parse_cmd_args():
                                                "config.yaml"),
                         help="Config file location")
 
-    parser.add_argument("-v", "--verbosity", type=str, default="info",
+    parser.add_argument("-v", "--verbosity", type=str, default=None,
                         help="Minimum verbosity level for logging.  Available "
                              "in ascending order: debug, info, warning, "
                              "error, critical).  The default is info.")
@@ -69,7 +69,15 @@ def main():
         if setting.isupper() and config_file_options.get(setting):
             setattr(config, setting, config_file_options.get(setting))
 
-    logger.setLevel(logging.__dict__[args.verbosity.upper()])
+    # Override the log level if specified on the command line.
+    if args.verbosity:
+        config.LOG_LEVEL = args.verbosity.upper()
+
+    # Write log file if configured in environment variable or config file
+    if config.LOG_LOCATION:
+        log.setup_file_logger(config.LOG_LOCATION)
+
+    logger.setLevel(logging.__dict__[config.LOG_LEVEL.upper()])
 
     # Create a connection to the Tor control port
     try:

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-privacy/packages/onionbalance.git



More information about the Pkg-privacy-commits mailing list