Source code for fplaneserver.flask.logger
"""
Logger implementation.
IMPORTANT:
In the format of the output two custom fields are added:
* ip: the ip address of the sender
* req: what kind of request is processed
* path: the page required
Every message logged must be submitted with the keyword
`extra = {"ip": request.remote_addr,
"method": request.method,
"path": request.path}`
`extra` is created by cureweb.before_after:before and set into the `g`
object
"""
import logging
import logging.handlers as lh
import os
# Formatters for the loggers
fmt = "[%(asctime)s] - %(levelname)s"
fmt += " - [%(request)s on %(path)s from %(ip)s]"
fmt += ": %(message)s"
dfmt = "%Y-%m-%d %H:%M:%S"
# do the formatter. It's common to each logger
formatter = logging.Formatter(fmt=fmt, datefmt=dfmt)
[docs]def set_logger(app):
"""
Create and add the logging handlers to the default flask logger.
Also deal with the fall-back logger
"""
# create the logger
logger = logging.getLogger('default')
# set the lower logger level to the of the file_log
logger.setLevel(app.config.get("LOG_LOWER_LEVEL"))
# create the file logger
logdir = app.config.get("LOG_DIR", '')
if logdir != '': # Disable logging if LOG_DIR is empty
if not os.path.exists(logdir): # pragma: nocover
print('Warning: Logdir %s not found, disabling logging!' % logdir)
return
logfile = os.path.join(logdir, 'fplaneserver.log')
file_log = lh.RotatingFileHandler(logfile, maxBytes=1000000,
backupCount=10)
file_log.setLevel(app.config.get("LOG_LOWER_LEVEL"))
file_log.setFormatter(formatter)
# add the file logger to the app logger
logger.addHandler(file_log)
# Create the mailing logger
# from hetwise.util.MailHandler import BufferingSMTPHandler
#
# logFormat = '%(asctime)s [%(levelname)s] %(message)s'
# _logger = logging.getLogger('ingest')
# _logger.setLevel('DEBUG')
# hndlr = BufferingSMTPHandler('localhost', 'hetdex@mpe.mpg.de',
# 'snigula@mpe.mpg.de',
# 'Ingestion finished successfully',
# logFormat, 5000)
# hndlr.setLevel(logging.INFO)
# _logger.addHandler(hndlr)