From 97f3a3df9e4ef388a42b80ccbd2b7b90a5a0f6e6 Mon Sep 17 00:00:00 2001 From: Starow Date: Thu, 7 Aug 2014 14:49:34 +0200 Subject: [PATCH] update pubsublogger with the last version --- bin/pubsublogger/subscriber.py | 46 +++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/bin/pubsublogger/subscriber.py b/bin/pubsublogger/subscriber.py index 8c13cd53..a150cc68 100644 --- a/bin/pubsublogger/subscriber.py +++ b/bin/pubsublogger/subscriber.py @@ -11,13 +11,16 @@ To use this module, you have to define at least a channel name. import redis from logbook import Logger import ConfigParser -from logbook import NestedSetup, NullHandler, FileHandler, MailHandler +from logbook import NestedSetup +from logbook import NullHandler +from logbook import TimedRotatingFileHandler +from logbook import MailHandler import os # use a TCP Socket by default use_tcp_socket = True -#default config for a UNIX socket +# default config for a UNIX socket unix_socket = '/tmp/redis.sock' # default config for a TCP socket hostname = 'localhost' @@ -32,7 +35,8 @@ smtp_server = None smtp_port = 0 src_server = None -def setup(name, path = 'log', enable_debug = False): + +def setup(name, path='log', enable_debug=False): """ Prepare a NestedSetup. @@ -43,39 +47,46 @@ def setup(name, path = 'log', enable_debug = False): :return a nested Setup """ path_tmpl = os.path.join(path, '{name}_{level}.log') - info = path_tmpl.format(name = name, level = 'info') - warn = path_tmpl.format(name = name, level = 'warn') - err = path_tmpl.format(name = name, level = 'err') - crit = path_tmpl.format(name = name, level = 'crit') + info = path_tmpl.format(name=name, level='info') + warn = path_tmpl.format(name=name, level='warn') + err = path_tmpl.format(name=name, level='err') + crit = path_tmpl.format(name=name, level='crit') # a nested handler setup can be used to configure more complex setups setup = [ # make sure we never bubble up to the stderr handler # if we run out of setup handling NullHandler(), # then write messages that are at least info to to a logfile - FileHandler(info, level='INFO', encoding='utf-8'), + TimedRotatingFileHandler(info, level='INFO', encoding='utf-8', + date_format='%Y-%m-%d'), # then write messages that are at least warnings to to a logfile - FileHandler(warn, level='WARNING', encoding='utf-8'), + TimedRotatingFileHandler(warn, level='WARNING', encoding='utf-8', + date_format='%Y-%m-%d'), # then write messages that are at least errors to to a logfile - FileHandler(err, level='ERROR', encoding='utf-8'), + TimedRotatingFileHandler(err, level='ERROR', encoding='utf-8', + date_format='%Y-%m-%d'), # then write messages that are at least critical errors to to a logfile - FileHandler(crit, level='CRITICAL', encoding='utf-8'), + TimedRotatingFileHandler(crit, level='CRITICAL', encoding='utf-8', + date_format='%Y-%m-%d'), ] if enable_debug: - debug = path_tmpl.format(name = name, level = 'debug') - setup.insert(1, FileHandler(debug, level='DEBUG', encoding='utf-8')) + debug = path_tmpl.format(name=name, level='debug') + setup.insert(1, TimedRotatingFileHandler(debug, level='DEBUG', + encoding='utf-8', date_format='%Y-%m-%d')) if src_server is not None and smtp_server is not None \ and smtp_port != 0 and len(dest_mails) != 0: mail_tmpl = '{name}_error@{src}' - from_mail = mail_tmpl.format(name = name, src = src_server) + from_mail = mail_tmpl.format(name=name, src=src_server) subject = 'Error in {}'.format(name) # errors should then be delivered by mail and also be kept # in the application log, so we let them bubble up. setup.append(MailHandler(from_mail, dest_mails, subject, - level='ERROR', bubble=True, server_addr=(smtp_server, smtp_port))) + level='ERROR', bubble=True, + server_addr=(smtp_server, smtp_port))) return NestedSetup(setup) + def mail_setup(path): """ Set the variables to be able to send emails. @@ -93,7 +104,8 @@ def mail_setup(path): smtp_port = config.get('mail', 'smtp_port') src_server = config.get('mail', 'src_server') -def run(log_name, path, debug = False, mail = None): + +def run(log_name, path, debug=False, mail=None): """ Run a subscriber and pass the messages to the logbook setup. Stays alive as long as the pubsub instance listen to something. @@ -110,7 +122,7 @@ def run(log_name, path, debug = False, mail = None): if use_tcp_socket: r = redis.StrictRedis(host=hostname, port=port) else: - r = redis.StrictRedis(unix_socket_path = unix_socket) + r = redis.StrictRedis(unix_socket_path=unix_socket) pubsub = r.pubsub() pubsub.psubscribe(channel + '.*')