mirror of https://github.com/CIRCL/AIL-framework
update pubsublogger with the last version
parent
c10003a630
commit
97f3a3df9e
|
@ -11,7 +11,10 @@ To use this module, you have to define at least a channel name.
|
||||||
import redis
|
import redis
|
||||||
from logbook import Logger
|
from logbook import Logger
|
||||||
import ConfigParser
|
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
|
import os
|
||||||
|
|
||||||
# use a TCP Socket by default
|
# use a TCP Socket by default
|
||||||
|
@ -32,6 +35,7 @@ smtp_server = None
|
||||||
smtp_port = 0
|
smtp_port = 0
|
||||||
src_server = None
|
src_server = None
|
||||||
|
|
||||||
|
|
||||||
def setup(name, path='log', enable_debug=False):
|
def setup(name, path='log', enable_debug=False):
|
||||||
"""
|
"""
|
||||||
Prepare a NestedSetup.
|
Prepare a NestedSetup.
|
||||||
|
@ -53,17 +57,22 @@ def setup(name, path = 'log', enable_debug = False):
|
||||||
# if we run out of setup handling
|
# if we run out of setup handling
|
||||||
NullHandler(),
|
NullHandler(),
|
||||||
# then write messages that are at least info to to a logfile
|
# 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
|
# 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
|
# 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
|
# 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:
|
if enable_debug:
|
||||||
debug = path_tmpl.format(name=name, level='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 \
|
if src_server is not None and smtp_server is not None \
|
||||||
and smtp_port != 0 and len(dest_mails) != 0:
|
and smtp_port != 0 and len(dest_mails) != 0:
|
||||||
mail_tmpl = '{name}_error@{src}'
|
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
|
# errors should then be delivered by mail and also be kept
|
||||||
# in the application log, so we let them bubble up.
|
# in the application log, so we let them bubble up.
|
||||||
setup.append(MailHandler(from_mail, dest_mails, subject,
|
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)
|
return NestedSetup(setup)
|
||||||
|
|
||||||
|
|
||||||
def mail_setup(path):
|
def mail_setup(path):
|
||||||
"""
|
"""
|
||||||
Set the variables to be able to send emails.
|
Set the variables to be able to send emails.
|
||||||
|
@ -93,6 +104,7 @@ def mail_setup(path):
|
||||||
smtp_port = config.get('mail', 'smtp_port')
|
smtp_port = config.get('mail', 'smtp_port')
|
||||||
src_server = config.get('mail', 'src_server')
|
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.
|
Run a subscriber and pass the messages to the logbook setup.
|
||||||
|
|
Loading…
Reference in New Issue