[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [bridgedb/master] 5331 - Add log rotation to BridgeDB



commit 31c55cd3289b606fb44daa336a565bf77b522f96
Author: aagbsn <aagbsn@xxxxxxxx>
Date:   Thu Mar 8 05:37:11 2012 -0800

    5331 - Add log rotation to BridgeDB
    
    Switches BridgeDB logger to use RotatingFileHandler.
    The defaults are to keep 5 backups, of 10MB each.
    Adds LOGFILE_COUNT, LOGFILE_ROTATE_SIZE to bridgedb.conf, and
    sets the default value for LOGLEVEL to WARNING.
    
    Thanks to weasel for this patch.
---
 bridgedb.conf        |    7 ++++++-
 lib/bridgedb/Main.py |   19 ++++++++++++-------
 2 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/bridgedb.conf b/bridgedb.conf
index 5d5dbad..81a2688 100644
--- a/bridgedb.conf
+++ b/bridgedb.conf
@@ -11,8 +11,13 @@ PIDFILE = "bridgedb.pid"
 
 # Either a file to log to, or None if we should log to the console.
 LOGFILE = "bridgedb.log"
+
+# Logfile rotation settings
+LOGFILE_COUNT = 5
+LOGFILE_ROTATE_SIZE = 10000000
+
 # One of "DEBUG", "INFO", "WARNING", "ERROR"...
-LOGLEVEL = "INFO"
+LOGLEVEL = "WARNING"
 
 # Files from which we read bridge descriptors, on start and on SIGHUP.
 BRIDGE_FILES = [ "./bridge-descriptors" ]
diff --git a/lib/bridgedb/Main.py b/lib/bridgedb/Main.py
index 648accd..8ef6fc0 100644
--- a/lib/bridgedb/Main.py
+++ b/lib/bridgedb/Main.py
@@ -11,6 +11,7 @@ import signal
 import sys
 import time
 import logging
+import logging.handlers
 import gettext
 
 from twisted.internet import reactor
@@ -110,14 +111,18 @@ def configureLogging(cfg):
     """
     level = getattr(cfg, 'LOGLEVEL', 'WARNING')
     level = getattr(logging, level)
-    extra = {}
-    if getattr(cfg, "LOGFILE"):
-        extra['filename'] = cfg.LOGFILE
 
-    logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s',
-                        datefmt="%b %d %H:%M:%S",
-                        level=level,
-                        **extra)
+    logging.getLogger().setLevel(level)
+    if getattr(cfg, "LOGFILE"):
+        logfile_count = getattr(cfg, "LOGFILE_COUNT", 5)
+        logfile_rotate_size = getattr(cfg, "LOGFILE_ROTATE_SIZE", 10000000)
+
+        handler = logging.handlers.RotatingFileHandler(cfg.LOGFILE, 'a',
+                                                       logfile_rotate_size,
+                                                       logfile_count)
+        formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(message)s', "%b %d %H:%M:%S")
+        handler.setFormatter(formatter)
+        logging.getLogger().addHandler(handler)
 
 def getKey(fname):
     """Load the key stored in fname, or create a new 32-byte key and store

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits