Commit e961e3de authored by Trevor Cappallo's avatar Trevor Cappallo
Browse files

add setup script

parent 7c483a04
......@@ -33,15 +33,15 @@ class OperationalLogger:
"""Main class for Operational Logging."""
def __init__(self, log_file='log.txt', log_level=logging.DEBUG,
log_format=None, date_format=None, from_address=None):
log_format=None, date_format=None, from_address=None, logger_name=None, console_level=logging.DEBUG):
"""Initialize logging handlers, formats, etc."""
self.__file_name = log_file
self.__file_name = log_file if logger_name is None else logger_name
self.__server_name = socket.getfqdn()
self.__full_buffer = StringIO.StringIO()
self.__error_buffer = StringIO.StringIO()
self.__critical_buffer = StringIO.StringIO()
if log_format is None:
log_format = '[%(asctime)s][%(levelname)8s][p%(process)d][%(filename)s:%(lineno)d]:%(message)s'
log_format = '[%(asctime)s][%(levelname)5s][p%(process)d][%(filename)s:%(lineno)d]:%(message)s'
self.__log_format = log_format
if date_format is None:
date_format = '%Y/%m/%d %H:%M:%S'
......@@ -63,19 +63,19 @@ class OperationalLogger:
critical_trigger.setLevel(logging.CRITICAL)
critical_trigger.setFormatter(formatter)
logging.getLogger().addHandler(error_trigger)
logging.getLogger().addHandler(critical_trigger)
logging.getLogger().addHandler(full_trigger)
logging.getLogger(logger_name).addHandler(error_trigger)
logging.getLogger(logger_name).addHandler(critical_trigger)
logging.getLogger(logger_name).addHandler(full_trigger)
# provide a more concise format for terminal output
if sys.stdout.isatty():
console = logging.StreamHandler(sys.stdout)
console.setLevel(logging.DEBUG)
console.setLevel(console_level)
console.setFormatter(logging.Formatter(
fmt='[%(asctime)s][%(levelname)s][%(filename)s:%(lineno)d]:%(message)s',
fmt='[%(asctime)s][%(levelname)5s][%(filename)s:%(lineno)d]:%(message)s',
datefmt='%m/%d %H:%M:%S'
))
logging.getLogger().addHandler(console)
logging.getLogger(logger_name).addHandler(console)
def has_errors(self):
"""Return whether any ERROR-level log statements in buffer."""
......@@ -87,6 +87,7 @@ class OperationalLogger:
def email_log(self, addresses):
"""Send email to addresses with current buffered errors."""
print "sending email to", addresses
message = "<br />\n".join([OperationalLogger.__colorize(l) for l in self.__error_buffer.getvalue().split('\n')])
OperationalMail.send_status(subject='Errors detected {}:{}'.format(self.__server_name,
os.path.basename(sys.argv[0])),
......@@ -112,7 +113,7 @@ class OperationalLogger:
##########################################################################################
def default_logging(log_root_dir=None, log_file_name=None, addresses=[], critical_addresses=[], from_address=None):
def default_logging(log_file_name, log_root_dir=None, addresses=[], critical_addresses=[], from_address=None, **kwargs):
"""Set an intelligent,context-dependent default logging scheme for the current script.
log_root_dir, log_file_name: log will be created as log_root_dir/log/YYYY-MM-DD/LOG_FILE_NAME.log
......@@ -133,7 +134,7 @@ def default_logging(log_root_dir=None, log_file_name=None, addresses=[], critica
logging.warning('Unable to create log directory {}:{}'.format(log_directory, str(err)))
logger = OperationalLogger(os.path.join(log_directory, '{}.log'.format(log_file_name)),
logging.DEBUG, from_address=from_address)
logging.DEBUG, from_address=from_address, **kwargs)
logging.info('Logging output from: {}'.format(' '.join(sys.argv)))
logging.info('Process id {}, parent process id {}'.format(os.getpid(), os.getppid()))
......@@ -207,6 +208,7 @@ def stack_trace(logging_level=logging.ERROR):
if __name__ == '__main__':
default_logging(addresses=['tcappallo@veriskclimate.com'], from_address='tcappallo@veriskclimate.com')
#default_logging(addresses=['msze@veriskclimate.com', 'msze@aer.com'], from_address='tcappallo@veriskclimate.com')
test = """Blah
foo
bar
......
#!/usr/bin/env python
"""Set up Operational Logging."""
from setuptools import setup
INSTALL_REQUIRES = [
'nose>=1.0',
'html2text',
]
setup(
name='OperationalLogging',
version='0.1.1',
description='Logging and email notification for operational projects',
author='Verisk Climate',
author_email='tcappallo@veriskclimate.com',
install_requires=INSTALL_REQUIRES,
package_dir={'': 'lib'},
py_modules=['OperationalLogging', 'OperationalMail'],
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment