# TuneGO API Core Logging for Python
import inspect
import time
import logging.config

from structlog import get_logger, configure, processors, stdlib, threadlocal

from api.settings import APP_NAME, APP_VER, LOG_LEVEl
from api.vars import current_user, tid, current_user_role, response_status, current_request_method, span_id, trace_id

logging.config.dictConfig({
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'json': {
            'format': '%(levelname)s %(message)s %(lineno)d %(pathname)s',
            'class': 'pythonjsonlogger.jsonlogger.JsonFormatter'
        }
    },
    'handlers': {
        'json': {
            'class': 'logging.StreamHandler',
            'formatter': 'json'
        }
    },
    'loggers': {
        '': {
            'handlers': ['json'],
            'level': LOG_LEVEl
        }
    }
})

configure(
    context_class=threadlocal.wrap_dict(dict),
    logger_factory=stdlib.LoggerFactory(),
    wrapper_class=stdlib.BoundLogger,
    processors=[
        stdlib.filter_by_level,
        stdlib.add_logger_name,
        stdlib.add_log_level,
        stdlib.PositionalArgumentsFormatter(),
        processors.TimeStamper(fmt="iso"),
        processors.StackInfoRenderer(),
        processors.format_exc_info,
        processors.UnicodeDecoder(),
        stdlib.render_to_log_kwargs]
)

LOG_CHANNEL_APPLOG = "Application"
LOG_CHANNEL_API_ERROR = "API Errors"
LOG_CHANNEL_RBAC = "RBAC"
LOG_CHANNEL_APICORE = "API Core"
LOG_CHANNEL_AUDIT = "Object Audit"
LOG_CHANNEL_CUSTOMER = "Customer"
LOG_CHANNEL_REQUEST = "Request"
LOG_CHANNEL_METRICS = "Metrics"

LOG_FIELD_CHANNEL = "Channel"
LOG_FIELD_APPNAME = "Application"
LOG_FIELD_APPVER = "Version"
LOG_FIELD_COMPONENT = "Component"
LOG_FIELD_RESOURCE = "Resource"
LOG_FIELD_ROLE = "Role"
LOG_FIELD_PERSON = "Person"
LOG_FIELD_METHOD = "Method"
LOG_FIELD_REMOTEADDR = "Client"
LOG_FIELD_SIZE = "Size"
LOG_FIELD_STATUS = "StatusCode"
LOG_FIELD_TRANSACTION = "Transaction"
LOG_FIELD_REQUEST_PAYLOAD = "Request"
LOG_FIELD_RESPONSE_PAYLOAD = "Response"
LOG_FIELD_USERAGENT = "UserAgent"


def timestamper(logger, log_method, event_dict):
    """Add a timestamp to each log entry."""
    event_dict["timestamp"] = time.time()
    return event_dict


global log
log = get_logger()


def LogApplicationInfo(message):

    log.bind(channel=LOG_CHANNEL_APPLOG)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.info(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
             Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogRequest(message):
    log.bind(channel=LOG_CHANNEL_REQUEST)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.info(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
             StatusCode=response_status.get(),
             Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogApplicationWarn(message):

    log.bind(channel=LOG_CHANNEL_APPLOG)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.warning(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
                Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogApplicationDebug(message):

    log.bind(channel=LOG_CHANNEL_APPLOG)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.debug(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogApiError(message):

    log.bind(channel=LOG_CHANNEL_API_ERROR)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.error(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogEndPointAction(message):

    log.bind(channel=LOG_CHANNEL_APPLOG)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.error(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogEndPointWarning(message):

    log.bind(channel=LOG_CHANNEL_APPLOG)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.error(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogEndPointDebug(message):

    log.bind(channel=LOG_CHANNEL_APPLOG)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.error(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogEndPointStartSegment(message):
    log.bind(channel=LOG_CHANNEL_APPLOG)

    log.bind(channel=LOG_CHANNEL_APPLOG)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.debug(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogEndPointEndSegment(message):

    log.bind(channel=LOG_CHANNEL_APPLOG)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.debug(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogCustomerError(message):

    log.bind(channel=LOG_CHANNEL_CUSTOMER)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.error(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogCustomerAction(message):

    log.bind(channel=LOG_CHANNEL_CUSTOMER)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.error(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogResourceAccessSuccess(message):

    log.bind(channel=LOG_CHANNEL_RBAC)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.error(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogResourceAccessFailure(message):

    log.bind(channel=LOG_CHANNEL_RBAC)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.error(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def logHttpreq(message):

    log.bind(channel=LOG_CHANNEL_APPLOG)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.error(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def logHttpResponse(message):

    log.bind(channel=LOG_CHANNEL_REQUEST)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.error(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def logContextRBACtrace(message):

    log.bind(channel=LOG_CHANNEL_RBAC)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.error(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def logContextRBACfailure(message):

    log.bind(channel=LOG_CHANNEL_RBAC)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.error(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())

# TuneGO API Core Logging for Python
import inspect
import time
import logging.config

from structlog import get_logger, configure, processors, stdlib, threadlocal

from api.settings import APP_NAME, APP_VER, LOG_LEVEl
from api.vars import current_user, tid, current_user_role, response_status, current_request_method, span_id, trace_id

logging.config.dictConfig({
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'json': {
            'format': '%(levelname)s %(message)s %(lineno)d %(pathname)s',
            'class': 'pythonjsonlogger.jsonlogger.JsonFormatter'
        }
    },
    'handlers': {
        'json': {
            'class': 'logging.StreamHandler',
            'formatter': 'json'
        }
    },
    'loggers': {
        '': {
            'handlers': ['json'],
            'level': LOG_LEVEl
        }
    }
})

configure(
    context_class=threadlocal.wrap_dict(dict),
    logger_factory=stdlib.LoggerFactory(),
    wrapper_class=stdlib.BoundLogger,
    processors=[
        stdlib.filter_by_level,
        stdlib.add_logger_name,
        stdlib.add_log_level,
        stdlib.PositionalArgumentsFormatter(),
        processors.TimeStamper(fmt="iso"),
        processors.StackInfoRenderer(),
        processors.format_exc_info,
        processors.UnicodeDecoder(),
        stdlib.render_to_log_kwargs]
)

LOG_CHANNEL_APPLOG = "Application"
LOG_CHANNEL_API_ERROR = "API Errors"
LOG_CHANNEL_RBAC = "RBAC"
LOG_CHANNEL_APICORE = "API Core"
LOG_CHANNEL_AUDIT = "Object Audit"
LOG_CHANNEL_CUSTOMER = "Customer"
LOG_CHANNEL_REQUEST = "Request"
LOG_CHANNEL_METRICS = "Metrics"

LOG_FIELD_CHANNEL = "Channel"
LOG_FIELD_APPNAME = "Application"
LOG_FIELD_APPVER = "Version"
LOG_FIELD_COMPONENT = "Component"
LOG_FIELD_RESOURCE = "Resource"
LOG_FIELD_ROLE = "Role"
LOG_FIELD_PERSON = "Person"
LOG_FIELD_METHOD = "Method"
LOG_FIELD_REMOTEADDR = "Client"
LOG_FIELD_SIZE = "Size"
LOG_FIELD_STATUS = "StatusCode"
LOG_FIELD_TRANSACTION = "Transaction"
LOG_FIELD_REQUEST_PAYLOAD = "Request"
LOG_FIELD_RESPONSE_PAYLOAD = "Response"
LOG_FIELD_USERAGENT = "UserAgent"


def timestamper(logger, log_method, event_dict):
    """Add a timestamp to each log entry."""
    event_dict["timestamp"] = time.time()
    return event_dict


global log
log = get_logger()


def LogApplicationInfo(message):

    log.bind(channel=LOG_CHANNEL_APPLOG)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.info(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
             Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogRequest(message):
    log.bind(channel=LOG_CHANNEL_REQUEST)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.info(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
             StatusCode=response_status.get(),
             Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogApplicationWarn(message):

    log.bind(channel=LOG_CHANNEL_APPLOG)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.warning(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
                Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogApplicationDebug(message):

    log.bind(channel=LOG_CHANNEL_APPLOG)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.debug(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogApiError(message):

    log.bind(channel=LOG_CHANNEL_API_ERROR)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.error(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogEndPointAction(message):

    log.bind(channel=LOG_CHANNEL_APPLOG)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.error(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogEndPointWarning(message):

    log.bind(channel=LOG_CHANNEL_APPLOG)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.error(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogEndPointDebug(message):

    log.bind(channel=LOG_CHANNEL_APPLOG)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.error(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogEndPointStartSegment(message):
    log.bind(channel=LOG_CHANNEL_APPLOG)

    log.bind(channel=LOG_CHANNEL_APPLOG)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.debug(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogEndPointEndSegment(message):

    log.bind(channel=LOG_CHANNEL_APPLOG)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.debug(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogCustomerError(message):

    log.bind(channel=LOG_CHANNEL_CUSTOMER)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.error(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogCustomerAction(message):

    log.bind(channel=LOG_CHANNEL_CUSTOMER)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.error(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogResourceAccessSuccess(message):

    log.bind(channel=LOG_CHANNEL_RBAC)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.error(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def LogResourceAccessFailure(message):

    log.bind(channel=LOG_CHANNEL_RBAC)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.error(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def logHttpreq(message):

    log.bind(channel=LOG_CHANNEL_APPLOG)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.error(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def logHttpResponse(message):

    log.bind(channel=LOG_CHANNEL_REQUEST)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.error(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def logContextRBACtrace(message):

    log.bind(channel=LOG_CHANNEL_RBAC)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.error(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())


def logContextRBACfailure(message):

    log.bind(channel=LOG_CHANNEL_RBAC)
    log.bind(Application=APP_NAME)
    log.bind(Version=APP_VER)

    func = inspect.currentframe().f_back.f_back.f_code
    log.bind(Function=func.co_name)
    log.bind(FileName=func.co_filename)
    log.bind(FirstLine=func.co_firstlineno)

    log.error(message, Transaction=tid.get(), People=current_user.get(), Role=current_user_role.get(),
              Method=current_request_method.get(), span_id=span_id.get(), trace_id=trace_id.get())

# TuneGO API Core Logging for Python Add a timestamp to each log entry.