update pubsublogger with the last version

pull/8/head
Starow 2014-08-07 14:49:34 +02:00
parent c10003a630
commit 97f3a3df9e
1 changed files with 29 additions and 17 deletions

View File

@ -11,7 +11,10 @@ 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
@ -32,6 +35,7 @@ smtp_server = None
smtp_port = 0
src_server = None
def setup(name, path='log', enable_debug=False):
"""
Prepare a NestedSetup.
@ -53,17 +57,22 @@ def setup(name, path = 'log', enable_debug = False):
# 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'))
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}'
@ -72,10 +81,12 @@ def setup(name, path = 'log', enable_debug = False):
# 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,6 +104,7 @@ 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):
"""
Run a subscriber and pass the messages to the logbook setup.