Skip to content

Instantly share code, notes, and snippets.

@mleinart
Created February 4, 2013 14:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mleinart/4707096 to your computer and use it in GitHub Desktop.
Save mleinart/4707096 to your computer and use it in GitHub Desktop.
Patch to Carbon 0.9.10 to support external log rotation
diff --git a/conf/carbon.conf.example b/conf/carbon.conf.example
index 53708a8..7f35e8e 100644
--- a/conf/carbon.conf.example
+++ b/conf/carbon.conf.example
@@ -30,6 +30,9 @@
#
#LOCAL_DATA_DIR = /opt/graphite/storage/whisper/
+# Enable daily log rotation. If disabled, a kill -HUP can be used after a manual rotate
+ENABLE_LOGROTATION = True
+
# Specify the user to drop privileges to
# If this is blank carbon runs as the user that invokes it
# This user must have write access to the local data directory
diff --git a/lib/carbon/conf.py b/lib/carbon/conf.py
index 903fc09..a95434b 100644
--- a/lib/carbon/conf.py
+++ b/lib/carbon/conf.py
@@ -64,6 +64,7 @@ defaults = dict(
USE_WHITELIST=False,
CARBON_METRIC_PREFIX='carbon',
CARBON_METRIC_INTERVAL=60,
+ ENABLE_LOGROTATION=True,
)
diff --git a/lib/carbon/log.py b/lib/carbon/log.py
index 56ff95a..52f1f1f 100644
--- a/lib/carbon/log.py
+++ b/lib/carbon/log.py
@@ -1,16 +1,43 @@
import time
+from os.path import exists
from sys import stdout, stderr
from zope.interface import implements
from twisted.python.log import startLoggingWithObserver, textFromEventDict, msg, err, ILogObserver
from twisted.python.syslog import SyslogObserver
from twisted.python.logfile import DailyLogFile
+
+class CarbonLogFile(DailyLogFile):
+ """Overridden to support logrotate.d"""
+ def __init__(self, *args, **kwargs):
+ DailyLogFile.__init__(self, *args, **kwargs)
+ # avoid circular dependencies
+ from carbon.conf import settings
+ self.enableRotation = settings.ENABLE_LOGROTATION
+
+ def shouldRotate(self):
+ if self.enableRotation:
+ return DailyLogFile.shouldRotate(self)
+ else:
+ return False
+
+ def write(self, data):
+ if not self.enableRotation:
+ if not exists(self.path):
+ self.reopen()
+ DailyLogFile.write(self, data)
+
+ # Backport from twisted >= 10
+ def reopen(self):
+ self.close()
+ self._openFile()
+
class CarbonLogObserver(object):
implements(ILogObserver)
def log_to_dir(self, logdir):
self.logdir = logdir
- self.console_logfile = DailyLogFile('console.log', logdir)
+ self.console_logfile = CarbonLogFile('console.log', logdir)
self.custom_logs = {}
self.observer = self.logdir_observer
@@ -33,7 +60,7 @@ class CarbonLogObserver(object):
log_type = event.get('type')
if log_type is not None and log_type not in self.custom_logs:
- self.custom_logs[log_type] = DailyLogFile(log_type + '.log', self.logdir)
+ self.custom_logs[log_type] = CarbonLogFile(log_type + '.log', self.logdir)
logfile = self.custom_logs.get(log_type, self.console_logfile)
logfile.write(message + '\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment