"""Unified connector to loguru-based logger with various configurations."""
import sys
from uuid import uuid4
from loguru import logger
from owslogger.logger import DSNHandler
from config import LOGGER_NAME, LOGGER_DSN, LOGGER_LEVEL, \
ENVIRONMENT, CONSOLE_LOG_LEVEL, APP_VERSION
default_format = '{time:YYYY-MM-DD at h:mm:ss A zz} | {' \
'level} | {message}'
class LoguruDSNHandler(DSNHandler):
"""Custom DSN handler.
Custom DSN handler that sends a JSON payload complying with OWS1
standard.
"""
def emit(self, record):
"""Emit a record.
From the documentation: do whatever it takes to actually log the
specified logging record. Here: we send it to the provider. The payload
matches Orchard format.
Args:
record (LogRecord): the record to log.
"""
record.correlation_id = record.extra.get('correlation_id')
super().emit(record)
def default_filter(record):
return all(
v not in record["extra"] for v in [
'json_only', 'file_only', 'csv_only', 'other_only', 'multi'
]) and record["level"].no >= logger.level(CONSOLE_LOG_LEVEL).no \
and record["level"].no != logger.level("ERROR").no
def default_error_filter(record):
return all(
v not in record["extra"] for v in [
'json_only', 'file_only', 'csv_only', 'other_only', 'multi'
]) and record["level"].no >= logger.level("ERROR").no
def default_debug_filter(record):
return all(
v not in record["extra"] for v in [
'json_only', 'file_only', 'csv_only', 'other_only', 'multi'
]) and record["level"].no == logger.level("DEBUG").no
def default_multi_filter(record):
return all(
v not in record["extra"] for v in [
'json_only', 'file_only', 'csv_only', 'other_only'
]) and record["level"].no >= logger.level(CONSOLE_LOG_LEVEL).no \
and record["level"].no != logger.level("ERROR").no \
and 'multi' in record['extra']
def default_multi_error_filter(record):
return all(
v not in record["extra"] for v in [
'json_only', 'file_only', 'csv_only', 'other_only'
]) and record["level"].no >= logger.level("ERROR").no \
and 'multi' in record['extra']
def default_multi_debug_filter(record):
return all(
v not in record["extra"] for v in [
'json_only', 'file_only', 'csv_only', 'other_only'
]) and record["level"].no == logger.level("DEBUG").no \
and 'multi' in record['extra']
# Ensure handlers are only added once.
try:
# Remove and replace default logger
logger.remove(0)
except ValueError:
pass
else:
# Default Main Logger
logger.add(sys.stderr,
format=default_format,
filter=default_filter,
colorize=True, level=CONSOLE_LOG_LEVEL)
# Default Error Logger
logger.add(sys.stderr,
filter=default_error_filter,
colorize=True, level='ERROR')
# Default Main Enqueued Logger
logger.add(sys.stderr,
format=default_format,
filter=default_multi_filter,
colorize=True,
enqueue=True, level=CONSOLE_LOG_LEVEL)
# Default Error Enqueued Logger
logger.add(sys.stderr,
filter=default_multi_error_filter,
colorize=True,
enqueue=True, level='ERROR')
# PropogateHandler for ows-logger
if LOGGER_DSN:
# bind context vars for datadog
correlation_id = uuid4()
context = {
'correlation_id': correlation_id
}
logger.add(
LoguruDSNHandler(LOGGER_DSN, ENVIRONMENT, LOGGER_NAME, APP_VERSION),
level=LOGGER_LEVEL,
filter=default_filter)
logger.add(
LoguruDSNHandler(LOGGER_DSN, ENVIRONMENT, LOGGER_NAME, APP_VERSION),
level=LOGGER_LEVEL,
filter=default_multi_filter)
logger.add(
LoguruDSNHandler(LOGGER_DSN, ENVIRONMENT, LOGGER_NAME, APP_VERSION),
level='ERROR',
filter=default_error_filter)
logger.add(
LoguruDSNHandler(LOGGER_DSN, ENVIRONMENT, LOGGER_NAME, APP_VERSION),
level='ERROR',
filter=default_multi_error_filter)
# Bind Correlation_ID to logger
logger = logger.bind(**context)
logger.info(f'Correlation ID: {correlation_id}')
# Welcome message
logger.debug("Loguru logging enabled.")