Skip to content
Snippets Groups Projects
Commit 72b7e4dc authored by Jan Mach's avatar Jan Mach
Browse files

Rewritten and enhanced the Hawat logging setup.

The logging setup is now more capable and configurable. (Redmine issue: #4216,#3443)
parent 2eab2e88
No related branches found
No related tags found
No related merge requests found
......@@ -72,8 +72,8 @@ def _setup_app_logging(app):
:return: Modified Hawat application
:rtype: hawat.base.HawatApp
"""
hawat.log.setup_logging_default(app)
hawat.log.setup_logging_file(app)
if not app.debug:
hawat.log.setup_logging_email(app)
......
......@@ -86,6 +86,7 @@ class Config: # pylint: disable=locally-disabled,too-few-public-methods
MAIL_USERNAME = None
MAIL_PASSWORD = None
MAIL_DEFAULT_SENDER = 'mentat@{}'.format(socket.getfqdn())
MAIL_SUBJECT_PREFIX = '[MyDojo]'
# Babel configurations for application localization.
BABEL_DEFAULT_LOCALE = hawat.const.HAWAT_DEFAULT_LOCALE
......@@ -103,12 +104,7 @@ class Config: # pylint: disable=locally-disabled,too-few-public-methods
# Hawat custom configurations.
#
ROLES = [
hawat.const.HAWAT_ROLE_USER,
hawat.const.HAWAT_ROLE_DEVELOPER,
hawat.const.HAWAT_ROLE_MAINTAINER,
hawat.const.HAWAT_ROLE_ADMIN
]
ROLES = hawat.const.HAWAT_ROLES
"""List of all user roles supported by the Hawat application."""
SUPPORTED_LOCALES = collections.OrderedDict([('en', 'English'), ('cs', 'Česky')])
......@@ -215,9 +211,18 @@ class Config: # pylint: disable=locally-disabled,too-few-public-methods
HAWAT_ADMINS = ['root@{}'.format(socket.getfqdn())]
"""List of system administrator emails."""
HAWAT_LOG_DEFAULT_LEVEL = 'info'
"""Default logging level, case insensitive. One of the values ``DEBUG``, ``INFO``, ``WARNING``, ``ERROR``, ``CRITICAL``."""
HAWAT_LOG_FILE = '/var/mentat/log/mentat-hawat.py.log'
"""Log file settings for logging framework."""
HAWAT_LOG_FILE_LEVEL = 'info'
"""File logging level, case insensitive. One of the values ``DEBUG``, ``INFO``, ``WARNING``, ``ERROR``, ``CRITICAL``."""
HAWAT_LOG_EMAIL_LEVEL = 'error'
"""File logging level, case insensitive. One of the values ``DEBUG``, ``INFO``, ``WARNING``, ``ERROR``, ``CRITICAL``."""
HAWAT_LOCAL_DEVELOPER_LOGIN = 'account@local'
"""Developer account for local development login."""
......
......@@ -56,6 +56,14 @@ HAWAT_ROLE_MAINTAINER = 'maintainer'
HAWAT_ROLE_ADMIN = 'admin'
"""Name of the 'admin' role."""
HAWAT_ROLES = [
HAWAT_ROLE_USER,
HAWAT_ROLE_DEVELOPER,
HAWAT_ROLE_MAINTAINER,
HAWAT_ROLE_ADMIN
]
"""List of valid user roles."""
HAWAT_ITEM_ACTION_CREATE = 'create'
"""Name of the item *create* action."""
......
......@@ -9,7 +9,7 @@
"""
This module contains database access functions for Hawat.
This module contains logging functions for Hawat application.
"""
......@@ -18,25 +18,98 @@ __credits__ = "Pavel Kácha <pavel.kacha@cesnet.cz>, Andrea Kropáčová <andrea
import logging
from logging.handlers import SMTPHandler, WatchedFileHandler
from logging.handlers import WatchedFileHandler, SMTPHandler
def setup_logging_default(app):
"""
Setup default application logging features.
"""
log_level_str = app.config['HAWAT_LOG_DEFAULT_LEVEL'].upper()
log_level = getattr(
logging,
log_level_str,
None
)
if not isinstance(log_level, int):
raise ValueError(
'Invalid default log level: %s' % log_level_str
)
app.logger.setLevel(log_level)
app.logger.debug(
'Hawat: Default logging services successfully started with level %s',
log_level_str
)
return app
def setup_logging_file(app):
"""
Setup application logging via watched file (rotated by external command).
"""
log_level_str = app.config['HAWAT_LOG_FILE_LEVEL'].upper()
log_level = getattr(
logging,
log_level_str,
None
)
if not isinstance(log_level, int):
raise ValueError(
'Invalid log file level: %s' % log_level_str
)
file_handler = WatchedFileHandler(app.config['HAWAT_LOG_FILE'])
file_handler.setLevel(log_level)
file_handler.setFormatter(
logging.Formatter(
'%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'
)
)
app.logger.addHandler(file_handler)
app.logger.debug(
'Hawat: File logging services successfully started to file %s with level %s',
app.config['HAWAT_LOG_FILE'],
log_level_str
)
return app
def setup_logging_email(app):
"""
Setup application logging via email.
"""
log_level_str = app.config['HAWAT_LOG_EMAIL_LEVEL'].upper()
log_level = getattr(
logging,
log_level_str,
None
)
if not isinstance(log_level, int):
raise ValueError(
'Invalid log email level: %s' % log_level_str
)
credentials = None
secure = None
if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
credentials = (app.config['MAIL_USERNAME'], app.config['MAIL_PASSWORD'])
if app.config['MAIL_USE_TLS']:
secure = ()
mail_handler = SMTPHandler(
(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
'no-reply@' + app.config['MAIL_SERVER'],
app.config['HAWAT_ADMINS'],
'Hawat failure',
credentials)
mail_handler.setLevel(logging.ERROR)
mail_handler.setFormatter(logging.Formatter('''
mailhost = (app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
fromaddr = app.config['MAIL_DEFAULT_SENDER'],
toaddrs = app.config['HAWAT_ADMINS'],
subject = app.config['MAIL_SUBJECT_PREFIX'] + ' Application Error',
credentials = credentials,
secure = secure
)
mail_handler.setLevel(log_level)
mail_handler.setFormatter(
logging.Formatter('''
Message type: %(levelname)s
Location: %(pathname)s:%(lineno)d
Module: %(module)s
......@@ -49,16 +122,9 @@ Message:
'''))
app.logger.addHandler(mail_handler)
app.logger.debug(
'Hawat: Email logging services successfully started with level %s',
log_level_str
)
def setup_logging_file(app):
"""
Setup application logging via watched file (rotated by external command).
"""
file_handler = WatchedFileHandler(app.config['HAWAT_LOG_FILE'])
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'))
app.logger.setLevel(logging.INFO)
app.logger.addHandler(file_handler)
app.logger.info('Hawat: Logging startup')
return app
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment