def test_empty_processors(self): """ An empty list is a valid value for processors so it must be preserved. """ # We need to do a bind such that we get an actual logger and not just # a lazy proxy. l = wrap_logger(object(), processors=[]).new() assert [] == l._processors
def _set_structured_logger(context, logger_name=None, service_tag=None): """ use this function to get a structured logger and can use this to pass into the LogLambda decorator context - aws lambda context service - name of the service that this lambda belongs to logger_name - logger's name log_level - one of the levels in logging module """ if LogLambdaBase._structured_logger: return LogLambdaBase._structured_logger stage_tag = get_stage(context) if logger_name: logger = logging.getLogger(str(logger_name)) else: logger = logging.getLogger() # Python logger in AWS Lambda has a preset format. # To change the format of # the logging statement, remove the logging handler # and add a new handler with the required format for handler in logger.handlers: logger.removeHandler(handler) LogLambdaBase._log_handler = LogLambdaBase._get_handler() LogLambdaBase._log_handler.setFormatter(logging.Formatter(FORMAT)) logger.addHandler(LogLambdaBase._log_handler) logger.setLevel(LogLambdaBase._log_level) logger.propagate = False wlogger = wrap_logger( logger, processors=[ LogLambdaBase._filter_pii_info, add_logger_name, add_log_level, TimeStamper(fmt="iso"), StackInfoRenderer(), format_exc_info, JSONRenderer(separators=(',', ':'), sort_keys=True)]) inferred_lambda_tag = context.function_name if stage_tag is not None: inferred_lambda_tag = inferred_lambda_tag.replace('{0}_'.format(stage_tag), '', 1) LogLambdaBase._structured_logger = wlogger.bind( aws_lambda_name=context.function_name, aws_lambda_request_id=context.aws_request_id, internal_service_tag=service_tag, inferred_stage_tag=stage_tag, inferred_lambda_tag=inferred_lambda_tag ) return LogLambdaBase._structured_logger
def test_wrap_passes_args(self): """ wrap_logger propagates all arguments to the wrapped bound logger. """ logger = object() p = wrap_logger(logger, processors=[1, 2, 3], context_class=dict) assert logger is p._logger assert [1, 2, 3] == p._processors assert dict is p._context_class
def test_wrap_returns_proxy(self): assert isinstance(wrap_logger(None), BoundLoggerLazyProxy)
def test_wrap_passes_args(self): logger = object() p = wrap_logger(logger, processors=[1, 2, 3], context_class=dict) assert logger is p._logger assert [1, 2, 3] == p._processors assert dict is p._context_class
def log(): """ Returns a ReturnLogger with a freshly wrapped OrderedDict. """ return wrap_logger(logger(), context_class=wrap_dict(OrderedDict))
def log(logger): """ Returns a ReturnLogger with a freshly wrapped dict. """ return wrap_logger(logger, context_class=wrap_dict(dict))
def test_wrap_returns_proxy(self): """ wrap_logger always returns a lazy proxy. """ assert isinstance(wrap_logger(None), BoundLoggerLazyProxy)
def log(): return wrap_logger(logger(), context_class=wrap_dict(OrderedDict))