Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot pass lambda context(JSON) to custom_fomat #166

Closed
chikinchoi opened this issue Sep 2, 2020 · 1 comment
Closed

Cannot pass lambda context(JSON) to custom_fomat #166

chikinchoi opened this issue Sep 2, 2020 · 1 comment

Comments

@chikinchoi
Copy link

chikinchoi commented Sep 2, 2020

Hi,

I am developing the fluent-logger-python in AWS Lambda and I would like to pass the lambda context to fluent by using the custom_fomat. However, it seems the fluent-logger-python doesn't allow me to do it.
For the workaround, I pass the function_name only to the custom_format as a string first. But I would like to advise how to pass the entire context JSON to the custom_format.

Below is my lambda code:

import logging
from fluent import handler
def lambda_handler(event, context):
    function_name = context.function_name
    print(function_name)
    custom_format = {
        'host': '%(hostname)s',
        'where': '%(module)s.%(funcName)s',
        'type': '%(levelname)s',
        'stack_trace': '%(exc_text)s',
        'context': context,
        'function_name': function_name
    }
    logging.basicConfig(level=logging.INFO)
    log = logging.getLogger('fluent.test')
    log.setLevel(logging.DEBUG)
    h = handler.FluentHandler('lambdaPython.0434', host='********************', port=24224)
    formatter = handler.FluentRecordFormatter(custom_format)
    h.setFormatter(formatter)
    log.addHandler(h)
    log.info("This log entry will be logged2.")

Error Response:

Response:
{
  "errorMessage": "'LambdaContext' object has no attribute 'find'",
  "errorType": "AttributeError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 28, in lambda_handler\n    log.info(\"This log entry will be logged2.\")\n",
    "  File \"/var/lang/lib/python3.8/logging/__init__.py\", line 1434, in info\n    self._log(INFO, msg, args, **kwargs)\n",
    "  File \"/var/lang/lib/python3.8/logging/__init__.py\", line 1577, in _log\n    self.handle(record)\n",
    "  File \"/var/lang/lib/python3.8/logging/__init__.py\", line 1587, in handle\n    self.callHandlers(record)\n",
    "  File \"/var/lang/lib/python3.8/logging/__init__.py\", line 1649, in callHandlers\n    hdlr.handle(record)\n",
    "  File \"/var/lang/lib/python3.8/logging/__init__.py\", line 950, in handle\n    self.emit(record)\n",
    "  File \"/opt/python/lib/python3.8/site-packages/fluent/handler.py\", line 237, in emit\n    data = self.format(record)\n",
    "  File \"/var/lang/lib/python3.8/logging/__init__.py\", line 925, in format\n    return fmt.format(record)\n",
    "  File \"/opt/python/lib/python3.8/site-packages/fluent/handler.py\", line 97, in format\n    super(FluentRecordFormatter, self).format(record)\n",
    "  File \"/var/lang/lib/python3.8/logging/__init__.py\", line 665, in format\n    if self.usesTime():\n",
    "  File \"/opt/python/lib/python3.8/site-packages/fluent/handler.py\", line 169, in _format_by_dict_uses_time\n    return any([value.find(search) >= 0 for value in self._fmt_dict.values()])\n",
    "  File \"/opt/python/lib/python3.8/site-packages/fluent/handler.py\", line 169, in <listcomp>\n    return any([value.find(search) >= 0 for value in self._fmt_dict.values()])\n"
  ]
}

Please advise. Thank you very much!

@chikinchoi chikinchoi changed the title Cannot pass lambda context(JSON) to custom_fomatter Cannot pass lambda context(JSON) to custom_fomat Sep 2, 2020
@arcivanov
Copy link
Member

This is not a bug.

You can't pass arbitrary objects to format. If you're logging JSON you're logging JSON as logger.log({'a': 1, 'b': 2}) but the object also needs to be formattable to begin with, which I doubt Lambda context is. Same with function name. https://docs.python.org/3/library/logging.html#formatter-objects

You are misusing a Formatter object where LoggerAdapter or Filter should be used: https://docs.python.org/3/howto/logging-cookbook.html#adding-contextual-information-to-your-logging-output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants