powertools-lambda-python icon indicating copy to clipboard operation
powertools-lambda-python copied to clipboard

Support custom log attributes via extra logger parameter

Open eoinsha opened this issue 5 years ago • 11 comments

Hi!

I am using the aws-lambda-powertools-python logger for a number of projects but also have existing deployments using structured JSON logging implemented with a custom logger. Our custom logger supports the Python logging extra parameter to supply custom attributes. It would be great if the aws-lambda-powertools-python formatter supported custom attributes so we can take advantage of structured logging for additional attributes.

Example

from aws_lambda_powertools.logging import Logger
logger = Logger()
logger.info('Put aggregation result', extra={'key': 'results/sum.csv', 'size': 130425})

Current output:

{
  "level": "INFO",
  "location": "<module>:5",
  "message": "Put aggregation result",
  "timestamp": "2020-12-23 11:00:47,695",
  "service": "service_undefined",
  "sampling_rate": 0
}

Desired output:

{
  "level": "INFO",
  "location": "<module>:5",
  "message": "Put aggregation result",
  "timestamp": "2020-12-23 11:00:47,695",
  "service": "service_undefined",
  "sampling_rate": 0,
  "key": "results/sum.csv",
  "size": 130425
}

This enables us to do effective log queries, filtering and stats using CloudWatch Logs Insights.

For reference:

  • python-json-logger support custom attributes using extra - https://github.com/madzak/python-json-logger
  • Usage of the extra keyword argument to add custom attributes to the LogRecord is covered here: https://docs.python.org/3/library/logging.html

Is this something that you would consider supporting?

eoinsha avatar Dec 23 '20 11:12 eoinsha

Hey @eoinsha - Great to hear from you, and thanks for opening this feature request.

We're in PTO so I'll revisit in January - for now, I have one quick question for you:

Logger allows an arbitrary object instead of string today:

logger.info({ 'key': 'results/sum.csv',
'size': 130425 })

This will be encapsulated within the message key instead of being top-level keys:

{ ... "message": { 'key': 'results/sum.csv',
'size': 130425} }

Does this help or you prefer having a string to message plus extra keys as top level keys?

If that helps, we should update the docs to make this more explicit. If not, I also think is fine having an extra key for this purpose for cases where you want to preserve the message

heitorlessa avatar Dec 23 '20 11:12 heitorlessa

Thanks @heitorlessa! I do have a preference for the extra method as it's consistent with the references, what we do already, makes it easier to switch over. But what you show here achieves the same thing, so it looks like so I can use that on other projects. Thanks again and have a great break! :)

eoinsha avatar Dec 23 '20 12:12 eoinsha

+1 for this feature. Would make it easier to convice people at my org to use it :)

ran-isenberg avatar Dec 29 '20 12:12 ran-isenberg

it's be great if there would be no need for extra= but just send a kwargs

ran-isenberg avatar Dec 29 '20 12:12 ran-isenberg

Hey @eoinsha - PR created! Could you double check if that helps you moving forward? #257

@risenberg-cyberark we wouldn't be able to do it without extra as it'd break the logging protocol and make it slightly difficult for those moving in or outside Logger.

cc Other folks who opened issues in the past, as this feature might help them out too @papi-tokei @ivamluz @patrickwerz @alvaropc

heitorlessa avatar Jan 12 '21 13:01 heitorlessa

This looks perfect, thanks @heitorlessa !

eoinsha avatar Jan 12 '21 16:01 eoinsha

Thanks, @heitorlessa ! That covers that scenario I raised in the other PR I opened the other day.

ivamluz avatar Jan 12 '21 16:01 ivamluz

Merging it now, and it should be released by Friday along with other minor improvements ;)

will circle back when 1.10.0 is available on PyPi this week

heitorlessa avatar Jan 12 '21 16:01 heitorlessa

@heitorlessa looking forward to the release. Thanks for your effort.

patrickwerz avatar Jan 12 '21 21:01 patrickwerz

This is perfect! Thank you so much!

Álvaro Ponce Cabrera.

El mar, 12 ene 2021 a las 14:31, Heitor Lessa ([email protected]) escribió:

Hey @eoinsha https://github.com/eoinsha - PR created! Could you double check if that helps you moving forward? #257 https://github.com/awslabs/aws-lambda-powertools-python/pull/257

@risenberg-cyberark https://github.com/risenberg-cyberark we wouldn't be able to do it without extra as it'd break the logging protocol https://github.com/python/cpython/blob/11d13e83abedabba12b28773317f1a365113e7af/Lib/logging/__init__.py#L1585 and make it slightly difficult for those moving in or outside Logger.

cc Other folks who opened issues in the past, as this feature might help them out too @papi-tokei https://github.com/papi-tokei @ivamluz https://github.com/ivamluz @patrickwerz https://github.com/patrickwerz @alvaropc https://github.com/alvaropc

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/awslabs/aws-lambda-powertools-python/issues/253#issuecomment-758656236, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGANP567JD6ZUDJXDQHHXCDSZRFLVANCNFSM4VG3IFBQ .

alvaropc avatar Jan 13 '21 09:01 alvaropc

hey everyone - This is now available on 1.10 on PyPi and on SAR :)

Closing this now, and thank you for @eoinsha for raising it!

heitorlessa avatar Jan 18 '21 15:01 heitorlessa