Permalink
Browse files

Resolves #115 logging moved to configuration file.

  • Loading branch information...
1 parent 375a7fe commit 183d41c1cf0cabc8344433a1c12a8d9bb6cdd453 @dnephin dnephin committed Mar 28, 2012
View
@@ -2,6 +2,7 @@ include *.txt
include *.md
include Makefile
include tron/default_config.yaml
+include tron/logging.conf
recursive-include debian *
recursive-include test *.py *.yaml
recursive-include docs *.txt *.rst *.yaml *.1 *.8
View
@@ -9,9 +9,10 @@ from __future__ import with_statement
import errno
import logging
+import logging.config
import optparse
import os
-from pkg_resources import resource_string
+import pkg_resources
import signal
import sys
@@ -27,16 +28,20 @@ import tron
from tron import commands
from tron import mcp
from tron import www
-from tron.utils import log_handler
-logger = logging.getLogger('bin.trond')
+logger = logging.getLogger('trond')
+TRON = 'tron'
+DEFAULT_CONF = 'default_config.yaml'
+DEFAULT_WORKING_CONF = 'tron_config.yaml'
+DEFAULT_LOGGING_CONF = 'logging.conf'
+
def create_default_config(config_path):
"""Create a default empty configuration for first time installs"""
- default_config = resource_string('tron', 'default_config.yaml')
+ default = pkg_resources.resource_string(TRON, DEFAULT_CONF)
with open(config_path, "w") as config_file:
- config_file.write(default_config)
+ config_file.write(default)
def parse_options():
@@ -46,9 +51,8 @@ def parse_options():
help="Directory where tron's state and output is stored"
" (default %default)",
default="/var/lib/tron/")
- parser.add_option("--log-file", "-l", action="store", dest="log_file",
- help="Where the logs are stored (default %default)",
- default="/var/log/tron/tron.log")
+ parser.add_option("--log-conf", "-l", action="store", dest="log_conf",
+ help="Custom logging.conf file to use")
parser.add_option("--config-file", "-c", action="store",
dest="config_file", default=None,
help="Configuration file to load (default in working"
@@ -81,16 +85,17 @@ def parse_options():
parser.error("Bad working-dir option")
if options.config_file is None:
- options.config_file = os.path.join(options.working_dir,
- "tron_config.yaml")
+ options.config_file = os.path.join(
+ options.working_dir, DEFAULT_WORKING_CONF)
return options
def setup_logging(options):
- level = logging.WARNING
- twist_level = logging.WARNING
+ default = pkg_resources.resource_filename(TRON, DEFAULT_LOGGING_CONF)
+ logfile = options.log_conf or default
+ level = twist_level = None
if options.verbose > 0:
level = logging.INFO
twist_level = logging.WARNING
@@ -100,32 +105,22 @@ def setup_logging(options):
if options.verbose > 2:
twist_level = logging.DEBUG
- root = logging.getLogger('')
-
- try:
- handler = log_handler.ReOpeningFileHandler(options.log_file)
- except IOError, e:
- print >>sys.stderr, e
- sys.exit()
+ tron_logger = logging.getLogger('tron')
+ twisted_logger = logging.getLogger('twisted')
- fmt_str = '%(asctime)s %(name)s %(levelname)s %(message)s'
- formatter = logging.Formatter(fmt_str)
- handler.setFormatter(formatter)
-
- root.addHandler(handler)
- root.setLevel(level)
- logging.getLogger('twisted').setLevel(twist_level)
+ logging.config.fileConfig(logfile)
+ if level is not None:
+ tron_logger.setLevel(level)
+ if twist_level is not None:
+ twisted_logger.setLevel(twist_level)
# Hookup twisted to standard logging
- observer = log.PythonLoggingObserver()
- observer.start()
+ log.PythonLoggingObserver().start()
# Show stack traces for errors in twisted deferreds.
if options.debug:
defer.setDebugging(True)
- return handler
-
# This is rather crazy thing to do, but I don't really like 'twistd', but it
# has all kinds of useful functions in it for daemonizing stuff. So rather than
@@ -199,7 +194,7 @@ class TronApplicationRunner(_twistd_unix.UnixApplicationRunner):
self.preApplication()
logger.debug("init: setup_logging")
- self.log_handler = setup_logging(self.options)
+ setup_logging(self.options)
logger.debug("init: postApplication")
self.postApplication()
@@ -240,7 +235,7 @@ class TronApplicationRunner(_twistd_unix.UnixApplicationRunner):
def sighup_handler(signum, frame):
logger.info("SIGHUP Caught!")
reactor.callLater(0, master_control.live_reconfig)
- reactor.callLater(0, self.log_handler.reopen)
+ reactor.callLater(0, setup_logging, self.options)
signal.signal(signal.SIGHUP, sighup_handler)
View
@@ -205,23 +205,6 @@ Context variables only available to Services:
total number of instances).
-.. _config_logging:
-
-Logging
--------
-
-**syslog_address** (optional)
- Include this if you want to enable logging to syslog. Accepts paths as
- strings and ``[address, port]`` lists for sockets. Typical values for
- various platforms are::
-
- Linux: "/dev/log"
- OS X: "/var/run/syslog"
- Windows: ["localhost", 514]
-
-Example::
-
- syslog_address: "/dev/log"
Nodes
-----
View
@@ -31,10 +31,10 @@ directory structure with your test jobs and configs. To put everything in one
directory, launch `trond` like this::
> mkdir wd
- > bin/trond --working-dir=wd --log-file=wd/tron.log --pid-file=wd/tron.pid --verbose
+ > bin/trond --working-dir=wd --pid-file=wd/tron.pid --verbose
You may be tempted to run with ``--nodaemon``, but all the interesting output
-goes to ``tron.log``, so you're better off running ``tail -f wd/tron.log`` in a
+goes to ``tron.log``, so you're better off running ``tail -f tron.log`` in a
terminal. Kill ``trond`` when you're done with ``cat wd/tron.pid | xargs
kill``.
View
@@ -25,8 +25,8 @@ Options
--working-dir=WORKING_DIR
Directory where tron's state and output is stored (default /var/lib/tron/)
--l LOG_FILE, --log-file=LOG_FILE
- Where the logs are stored (default /var/log/tron/tron.log)
+-l LOG_CONF, --log-conf=LOG_CONF
+ Logging configuration file to setup python logger
-c CONFIG_FILE, --config-file=CONFIG_FILE
Configuration file to load (default in working dir)
@@ -67,6 +67,12 @@ State file
trond saves state here when it terminates and reloads it when it starts
up again.
+
+Logging
+-------
+TODO documentation
+
+
Bugs
----
@@ -48,13 +48,6 @@ command_context
the command string. For example, if you include `animal: cat`, then the
command `cat %(animal)s` will become `cat cat`.
-syslog_address
- Include this if you want to enable logging to syslog. Accepts paths as strings
- and [address, port] lists for sockets. Typical values for various platforms are::
-
- Linux: "/dev/log"
- OS X: "/var/run/syslog"
- Windows: ["localhost", 514]
notification_options
Who to email failures to.
@@ -1,5 +1,4 @@
import datetime
-import platform
import shutil
import StringIO
import tempfile
@@ -39,15 +38,6 @@
"""
-def syslog_address_for_platform():
- if platform.system() == 'Darwin':
- return '/var/run/syslog'
- elif platform.system() == 'Windows':
- return ['localhost', 514]
- else:
- return '/dev/log'
-
-
class OldConfigTest(TestCase):
OLD_BASE_CONFIG = """
--- !TronConfiguration
@@ -157,7 +147,6 @@ def test_attributes(self):
test_config = load_config(StringIO.StringIO(self.config))
expected = TronConfig(
working_dir='/tmp',
- syslog_address=None,
command_context=FrozenDict(**{
'python': '/usr/bin/python',
'batch_dir': '/tron/batch/test/foo'
@@ -311,7 +300,6 @@ def test_attributes(self):
# we could just do a big assert_equal here, but it would be hella hard
# to debug failures that way.
assert_equal(test_config.working_dir, expected.working_dir)
- assert_equal(test_config.syslog_address, expected.syslog_address)
assert_equal(test_config.command_context, expected.command_context)
assert_equal(test_config.ssh_options, expected.ssh_options)
assert_equal(test_config.notification_options, expected.notification_options)
@@ -7,7 +7,6 @@
from testify import TestCase, run, setup, assert_equal, teardown
from tron import mcp
from tron.config import config_parse
-from tests.config.config_parse_test import syslog_address_for_platform
class ConfigTest(TestCase):
@@ -67,7 +66,6 @@ def config_2(self, wd):
agent=True,
identities=['tests/test_id_rsa'],
),
- syslog_address=syslog_address_for_platform(),
nodes=[
dict(name='node0', hostname='batch0'),
dict(name='node1', hostname='batch1'),
View
@@ -0,0 +1,33 @@
+[loggers]
+keys=root, twisted, tron
+
+[handlers]
+keys=fileHandler
+
+[formatters]
+keys=defaultFormatter
+
+[logger_root]
+level=DEBUG
+handlers=fileHandler
+
+[logger_twisted]
+level=WARNING
+handlers=fileHandler
+qualname=twisted
+propagate=0
+
+[logger_tron]
+level=INFO
+handlers=fileHandler
+qualname=tron
+propagate=0
+
+[handler_fileHandler]
+class=logging.FileHandler
+level=DEBUG
+formatter=defaultFormatter
+args=('tron.log',)
+
+[formatter_defaultFormatter]
+format=%(asctime)s %(name)s %(levelname)s %(message)s
View
@@ -84,8 +84,9 @@ def __init__(self):
self.trond_bin = os.path.join(self.tron_bin, 'trond')
self.tronfig_bin = os.path.join(self.tron_bin, 'tronfig')
self.tronview_bin = os.path.join(self.tron_bin, 'tronview')
-
self.log_file = os.path.join(self.tmp_dir, 'tron.log')
+ self.log_conf = 'tests/logging.conf'
+
self.pid_file = os.path.join(self.tmp_dir, 'tron.pid')
self.config_file = os.path.join(self.tmp_dir, 'tron_config.yaml')
@@ -95,11 +96,11 @@ def __init__(self):
self.run_time = None
self.trond_debug_args = ['--working-dir=%s' % self.tmp_dir,
- '--log-file=%s' % self.log_file,
'--pid-file=%s' % self.pid_file,
'--port=%d' % self.port,
'--host=%s' % self.host,
'--config=%s' % self.config_file,
+ '--log-conf=%s' % self.log_conf,
'--verbose', '--verbose']
self.tron_server_address = '%s:%d' % (self.host, self.port)
@@ -82,7 +82,6 @@ def config_object_factory(name, required=None, optional=None):
'TronConfig',
optional=[
'working_dir', # str
- 'syslog_address', # str
'command_context', # FrozenDict of str
'ssh_options', # ConchOptions
'notification_options',# NotificationOptions or None
@@ -365,10 +364,6 @@ def valid_working_dir(wd):
return valid_str('working_dir', wd, optional=True)
-def valid_syslog(syslog):
- return valid_str('syslog', syslog, optional=True)
-
-
def valid_command_context(context):
# context can be any dict.
return FrozenDict(**valid_dict('command_context', context or {}))
@@ -632,7 +627,6 @@ class ValidateConfig(Validator):
config_class = TronConfig
defaults = {
'working_dir': None,
- 'syslog_address': None,
'command_context': None,
'ssh_options': valid_ssh_options({}),
'notification_options': None,
@@ -644,7 +638,6 @@ class ValidateConfig(Validator):
}
validators = {
'working_dir': valid_working_dir,
- 'syslog_address': valid_syslog,
'command_context': valid_command_context,
'ssh_options': valid_ssh_options,
'notification_options': valid_notification_options,
@@ -6,13 +6,6 @@ ssh_options:
# - /home/tron/.ssh/id_dsa
agent: true
-## Uncomment if you want logging to syslog. Typical values for different
-## platforms:
-## Linux: "/dev/log"
-## OS X: "/var/run/syslog"
-## Windows: ["localhost", 514]
-# syslog_address: /dev/log
-
# notification_options:
## In case of trond failures, where should we send notifications to ?
# smtp_host: localhost
Oops, something went wrong.

0 comments on commit 183d41c

Please sign in to comment.