Repository URL to install this package:
|
Version:
0.8.1 ▾
|
try:
from typing import IO, Any, Callable, Dict, Optional
except ImportError:
pass
import logging
import sys
from os import environ
from supertenant import consts
from supertenant.supermeter import _get_brain
ENV_KEY_LOGGER = "SUPERTENANT_SUPERMETER_LOGGER"
logging_TRACE = -1 # no logging.TRACE
LogLevelToBrainLogLevel = {
logging_TRACE: consts.BRAIN_LOG_LEVEL_TRACE, # python's logging doesn't havea TRACE level
logging.DEBUG: consts.BRAIN_LOG_LEVEL_DEBUG,
logging.INFO: consts.BRAIN_LOG_LEVEL_INFO,
logging.WARN: consts.BRAIN_LOG_LEVEL_WARN,
logging.ERROR: consts.BRAIN_LOG_LEVEL_ERROR,
logging.FATAL: consts.BRAIN_LOG_LEVEL_FATAL,
}
LogLevelToName = {
logging_TRACE: "TRACE",
logging.DEBUG: "DEBUG",
logging.INFO: "INFO",
logging.WARN: "WARN",
logging.ERROR: "ERROR",
logging.FATAL: "FATAL",
}
def log_brain(level, msg):
# type: (int, str) -> None
brain = _get_brain()
if brain is not None:
blevel = LogLevelToBrainLogLevel.get(level, consts.BRAIN_LOG_LEVEL_INFO)
brain.log(blevel, msg)
def log_io(io, level, msg):
# type: (IO[str], int, str) -> None
io.write("[supertenant-supermeter]: %s: %s Source=SuperTenant\n" % (LogLevelToName.get(level, "NOTSET"), msg))
log_brain(level, msg)
def log_logging(level, msg):
# type: (int, str) -> None
logging.log(level, msg)
log_brain(level, msg)
def log_instrumentation_success(integration_module, version, extra=None):
# type: (str, str, *Any) -> None
log(
logging.INFO,
# py2.7 f-string
"instrumentation success integration_module=%s version=%s extra=%s" % (integration_module, version, extra),
)
def log_instrumentation_skipped(integration_module, version, extra=None):
# type: (str, str, *Any) -> None
log(
logging.INFO,
# py2.7 f-string
"instrumentation skipped integration_module=%s version=%s extra=%s" % (integration_module, version, extra),
)
def log_instrumentation_failed(integration_module, version, extra=None):
# type: (str, str, *Any) -> None
log(
logging.INFO,
# py2.7 f-string
"instrumentation failed integration_module=%s version=%s extra=%s" % (integration_module, version, extra),
)
def log_integration_module_skipped_circuit_breaker(integration_module):
# type: (str) -> None
log(
logging.INFO,
# py2.7 f-string
"integration module circuit breaker disabled instrumentation integration_module=%s" % (integration_module,),
)
def log_integration_module_exception(integration_module, msg, exc):
# type: (str, str, Exception) -> None
import traceback
tb = traceback.format_exc()
log(
logging.ERROR,
# py2.7 f-string
"exception: %s integration_module=%s exc=%s tb=%s" % (msg, integration_module, str(exc), tb),
)
def log_integration_module_error(integration_module, msg, extra=None):
# type: (str, str, *Any) -> None
log(
logging.ERROR,
# py2.7 f-string
"%s integration_module=%s extra=%s" % (msg, integration_module, extra),
)
def log_integration_module_warn(integration_module, msg, extra=None):
# type: (str, str, *Any) -> None
log(
logging.WARN,
# py2.7 f-string
"%s integration_module=%s extra=%s" % (msg, integration_module, extra),
)
def log_integration_module_info(integration_module, msg, extra=None):
# type: (str, str, *Any) -> None
log(
logging.INFO,
# py2.7 f-string
"%s integration_module=%s extra=%s" % (msg, integration_module, extra),
)
def log_integration_module_debug(integration_module, msg, extra=None):
# type: (str, str, *Any) -> None
log(
logging.DEBUG,
# py2.7 f-string
"%s integration_module=%s extra=%s" % (msg, integration_module, extra),
)
def log_integration_module_trace(integration_module, msg, extra=None):
# type: (str, str, *Any) -> None
log(
logging_TRACE, # no logging.TRACE
# py2.7 f-string
"%s integration_module=%s extra=%s" % (msg, integration_module, extra),
)
class IntegrationModuleLog:
def __init__(self, integration_module):
# type: (str) -> None
self.integration_module = integration_module
def instrumentation_success(self, version, extra=None):
# type: (str, Optional[Dict[str, Any]]) -> None
log_instrumentation_success(self.integration_module, version, extra)
def instrumentation_skipped(self, version, extra=None):
# type: (str, Optional[Dict[str, Any]]) -> None
log_instrumentation_skipped(self.integration_module, version, extra)
def instrumentation_failed(self, version, extra=None):
# type: (str, Optional[Dict[str, Any]]) -> None
log_instrumentation_failed(self.integration_module, version, extra)
def trace(self, msg, extra=None):
# type: (str, Optional[Dict[str, Any]]) -> None
log_integration_module_trace(self.integration_module, msg, extra)
def debug(self, msg, extra=None):
# type: (str, Optional[Dict[str, Any]]) -> None
log_integration_module_debug(self.integration_module, msg, extra)
def info(self, msg, extra=None):
# type: (str, Optional[Dict[str, Any]]) -> None
log_integration_module_info(self.integration_module, msg, extra)
def warn(self, msg, extra=None):
# type: (str, Optional[Dict[str, Any]]) -> None
log_integration_module_warn(self.integration_module, msg, extra)
def error(self, msg, extra=None):
# type: (str, Optional[Dict[str, Any]]) -> None
log_integration_module_error(self.integration_module, msg, extra)
def exception(self, msg, exc):
# type: (str, Exception) -> None
log_integration_module_exception(self.integration_module, msg, exc)
def _init_logger():
# type: () -> Callable[[int, str], None]
logger_system = environ.get(ENV_KEY_LOGGER, "brain")
if logger_system == "brain":
return log_brain
elif logger_system == "stdout":
return lambda level, msg: log_io(sys.stdout, level, msg)
elif logger_system == "stderr":
return lambda level, msg: log_io(sys.stderr, level, msg)
elif logger_system == "python":
return log_logging
sys.stderr.write("[supertenant-supermeter] ERROR: bad logger name %s, defaulting to brain log\n" % (logger_system,))
return log_brain
log = _init_logger()