Skip to main content

Super-opinionated structured logger for AWS lambda

Project description

Intro, in which opinions are had

Cazoo-Logger is a deeply opinionated structured logger for Python on AWS Lambda.

Cazoo-Logger owes a debt to aws_lambda_logger which is both less opinionated, and probaby better suited to your particular use-case than this library. Go check it out.

Basic Usage

The Cazoo Logger module exposes a single function config that sets up the logger for use.

>>> import cazoo_logger
>>> cazoo_logger.config()

By default this configures the root logger at INFO level, writing to a special JSON formatter.

To obtain a logger instance, call the appropriate fromContext function with your incoming event and context:

>>> def handler(event, context):
...     logger = cazoo_logger.fromContext(event, context)
...     logger.info('sup?')
...

This will result in well-formatted json messages with a documented schema.

{
    "msg": "sup?",
    "context": {
        "request_id": "abc123",
        "function": {"name": "do-things", "version": "0.1.2.3"},
        "sns": {
            "id": "66591d01-0241-5751-bb17-586e5a6dcf91",
            "topic": "arn:aws:sns:us-east-1:12345678912:bucket-o-stuff",
            "type": "Notification",
            "subject": "Amazon S3 Notification",
        },
    }
}

If you don’t have an AWS context and event, you can construct a logger with no context

>>> logger = cazoo_logger.empty()

Logging Errors

Logging caught errors is as simple as setting the exc_info kwarg to True on the log call.

>>> try:
...     raise ValueError("What in the heck do you call that?")
... except:
...     logger.warn("I dunno man, looks pretty sketchy to me", exc_info=True)
...
    {"msg": "I dunno man, looks pretty sketchy to me", "data": {"error": {"name": "ValueError", "message": "What in the heck do you call that?", "stack": "Traceback (most recent call last):\n  File \"<stdin>\", line 2, in <module>\nValueError: What in the heck do you call that?"}}}

Logging additional data

You might want to include additional structured data in your logs. Any values you pass to the extra kwarg will be json serialised into the data section of your log line.

>>> logger.info("I did a query", extra={'sql': {'query': 'select * from table where field = ?', 'parameters': [123] }})
{"msg": "I did a query", "data": {"sql": {"query": "select * from table where field = ?", "parameters": [123]}}}

You can also use the with_data method. This method returns a new logger instance with the data section pre-populated.

>>> new_logger = logger.with_data(sql={'query': 'select * from foo where bar = ?', 'parameters':[234]})
>>> new_logger.debug('doin a query')
{"msg": "doin a query", "data": {"sql": {"query": "select * from foo where bar = ?", "parameters": [234]}}}
>>> new_logger.error('oh noes! the query did not work!')
{"msg": "oh noes! the query did not work!", "data": {"sql": {"query": "select * from foo where bar = ?", "parameters": [234]}}}

Logging additional context

Similarly you might want to add more data to the context section of your log event.

>>> new_logger = logger.with_context(request_id='abc-123')
>>> new_logger.info('handling request')
{"msg": "handling request", "context": {"request_id": "abc-123"}}

Additional Log Levels

The add_logging_level function allows you to add custom log levels to the logger. So for example a level of “TRACE” could be added at 15 to provide a level of logging is between DEBUG and INFO.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cazoo_logger-0.9.0.tar.gz (23.0 kB view details)

Uploaded Source

Built Distribution

cazoo_logger-0.9.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file cazoo_logger-0.9.0.tar.gz.

File metadata

  • Download URL: cazoo_logger-0.9.0.tar.gz
  • Upload date:
  • Size: 23.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.8

File hashes

Hashes for cazoo_logger-0.9.0.tar.gz
Algorithm Hash digest
SHA256 ba19f50f1f59d86556e22c4a57f75a10b793019bd4f8666ed9330d8b3f7d27b2
MD5 cfdfe64a1f91acd91984dd897a59cbf2
BLAKE2b-256 f4e57cd16af644943f7e7a5f3f63132d3db781665d5953d2dec730f8c1441bff

See more details on using hashes here.

File details

Details for the file cazoo_logger-0.9.0-py3-none-any.whl.

File metadata

  • Download URL: cazoo_logger-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.8

File hashes

Hashes for cazoo_logger-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9113f2399186d0690181d775237a2fdbd394a2f18a52446d246a11b2ee3e9cf4
MD5 a654077a343c2b5df1db86558b73e821
BLAKE2b-256 5b609ddbdde568b8bce93c71e552e297fc645fcb5a00abc48db6c435ebecd3bd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page