QChris has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/172197

Change subject: Allow additional logging to disk for projectcounts aggregation
......................................................................

Allow additional logging to disk for projectcounts aggregation

The logging to disk is happening at debug level, and hence helps
debugging gone wrong cron jobs.

Bug: 72740
Change-Id: I75ea2605fcc826264eb70360f486a5cd6cc9c825
---
M .gitignore
M bin/aggregate_projectcounts
2 files changed, 38 insertions(+), 10 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/analytics/aggregator 
refs/changes/97/172197/1

diff --git a/.gitignore b/.gitignore
index 6f9f3de..b75af7e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
 .tox
 *.pyc
 /data
+/log
diff --git a/bin/aggregate_projectcounts b/bin/aggregate_projectcounts
index 04b5c6b..ae2bd3f 100755
--- a/bin/aggregate_projectcounts
+++ b/bin/aggregate_projectcounts
@@ -18,7 +18,7 @@
 
 Usage: aggregate_projectcounts [--source SOURCE_DIR] [--target TARGET_DIR]
            [--first-date FIRST_DATE] [--last-date LAST_DATE] [--date DATE]
-           [-v ...] [--help]
+           [--log LOG_FILE] [-v ...] [--help]
 
 Options:
     -h, --help               Show this help message and exit.
@@ -34,6 +34,7 @@
                              [default: yesterday]
     --date DATE              Day to aggregate for (overrides --first-date, and
                              --last-date)
+    --log LOG_FILE           In addition to stdout, also log to LOG_FILE
 
     -v, --verbose            Increase verbosity
 """
@@ -50,22 +51,48 @@
 
 import aggregator
 
-if __name__ == '__main__':
-    arguments = docopt(__doc__)
+LOG_MESSAGE_FORMAT = '%(asctime)s %(levelname)-6s %(message)s'
+LOG_DATE_FORMAT = '%Y-%m-%dT%H:%M:%S'
 
-    # Setting up logging
+
+def setup_logging(verbosity, log_file):
     log_level = logging.ERROR
-    if arguments['--verbose'] >= 1:
-        if arguments['--verbose'] >= 2:
-            if arguments['--verbose'] >= 3:
+    if verbosity >= 1:
+        if verbosity >= 2:
+            if verbosity >= 3:
                 log_level = logging.DEBUG
             else:
                 log_level = logging.INFO
         else:
             log_level = logging.WARNING
-    logging.basicConfig(level=log_level,
-                        format='%(asctime)s %(levelname)-6s %(message)s',
-                        datefmt='%Y-%m-%dT%H:%M:%S')
+
+    if arguments['--log']:
+        log_file_abs = os.path.abspath(log_file)
+
+        # Make sure log file's directory exists
+        if not os.path.exists(os.path.dirname(log_file_abs)):
+            os.makedirs(os.path.dirname(log_file_abs))
+
+        # Root logger at DEBUG
+        logging.basicConfig(level=logging.DEBUG,
+                            format=LOG_MESSAGE_FORMAT,
+                            datefmt=LOG_DATE_FORMAT,
+                            filename=log_file_abs)
+        # Console logger at user specified verbosity
+        console_logger = logging.StreamHandler()
+        console_logger.setLevel(log_level)
+        console_logger.setFormatter(logging.Formatter(LOG_MESSAGE_FORMAT))
+        logging.getLogger('').addHandler(console_logger)
+    else:
+        logging.basicConfig(level=log_level,
+                            format=LOG_MESSAGE_FORMAT,
+                            datefmt=LOG_DATE_FORMAT)
+
+
+if __name__ == '__main__':
+    arguments = docopt(__doc__)
+
+    setup_logging(arguments['--verbose'], arguments['--log'])
 
     logging.debug("Parsed arguments: %s" % (arguments))
 

-- 
To view, visit https://gerrit.wikimedia.org/r/172197
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I75ea2605fcc826264eb70360f486a5cd6cc9c825
Gerrit-PatchSet: 1
Gerrit-Project: analytics/aggregator
Gerrit-Branch: master
Gerrit-Owner: QChris <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to