Overview
This package provides structured JSON logging faculties designed to seamlessly interoperate with log aggregators like AWS Cloudwatch, ELK, and others. These faculties are provided via extension of the python's existing logging library and can be used as a drop in replacement.
Usage:
If you want to generate simple structured logs, use this package just like the Python logging library.
If you want inject contextual information into your structured logs, add a LogAppender. This is great option if you want to run your application in somewhere like AWS Lambda, where you would want your logs to contain aws_request_id and other function specific metadata. See Use Case: AWS Lambda for more.
If you want to ensure that unwanted information is removed from your logs, use a LogFilter. Filters are
a great way to trim the size of your log lines to cut down on logging costs. Filters can also be useful in specific log
stacks - namely the ELK stack - that need to inject specific keys like the type key to index logs correctly.
Filters and Appenders can be mixed and matched through your logger's addFilter and removeFilter methods. Appenders and Filters are both dynamically configurable via the logger's update_appender and update_filter methods.
Basic use: JSON logging
import logging
from cloudwatch_logging import CloudwatchLogging
# create a structured JSON logger that works well with Cloudwatch and others; optionally with runtime specific appenders and filters to remove unwanted fields
logger = CloudwatchLogging.create_logger(__name__, appender=None, filter=None)
logger.setLevel(logging.INFO)
logger.warning("Structured logging with custom fields", extra={"custom_field": "custom_value"})
Use case; AWS Lambda
import logging
from cloudwatch_logging import CloudwatchLogging
# Setup logging
logger = CloudwatchLogging.create_logger("your_lambda_function")
logger.propagate = False # disable Lambda runtime default logger from double logging lines sent to this logger
logger.setLevel(logging.INFO)
def your_lambda_handler(event, context):
lamdbda_appender = CloudwatchLogging.LogAppender(context)
logger.addFilter(lamdbda_appender)
logger.info("This line will be logged with info from the Lambda context object!", extra={"tapped_in": True})
logger.removeFilter(lamdbda_appender) # ensure you do this so you don't add duplicate appenders on the same logger
Use case; Cloudwatch Cost Optimization
import logging
from cloudwatch_logging import CloudwatchLogging, Filters
logger = CloudwatchLogging.create_logger("your_lambda_function")
logger.setLevel(logging.INFO)
# add a LogTrimmer on the logger and save some money on CW storage costs
logger.addFilter(CloudwatchLogging.LogFilter(Filters.COST_EFFECTIVE))
logger.warning("Structured logging on the cheap!", extra={"field": "value"})
Development
Setup
This is a pure Python library; it can easily be installed using pip install cloudwatch_logging. Use the -e arg if you
plan to develop locally.
Test
Run python3 test_cloudwatch_logging.py. Inspect the output to see what is logged. Someone should write proper tests but I wrote this on vacation and couldn't be bothered.
To-Do
- Check error and exception trace handling - how can we make these better?
- Add examples for other AWS runtimes (Fargate?)
Metadata
Release files for cloudwatch-logging 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cloudwatch-logging-1.0.0.tar.gz | 4.4 kB | Without provenance | Details for cloudwatch-logging-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cloudwatch_logging-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for cloudwatch_logging-1.0.0-py3-none-any.whl |
Total release size: 14.2 kB
Release files / cloudwatch-logging-1.0.0.tar.gz
| Download URL | cloudwatch-logging-1.0.0.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
10cc937baf258959cbfe1f4995e356b7eeab3a567330ff0dbffbe26890305d8d
|
|
BLAKE2b-256 checksum How to use checksums |
7c6af95a5d1c83793dfff331b7cf7fac34aebb80e6b06495eb2e238727f06d8b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.15.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.8
|
Release files / cloudwatch_logging-1.0.0-py3-none-any.whl
| Download URL | cloudwatch_logging-1.0.0-py3-none-any.whl |
|---|---|
| Size | 9.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
31403fd97a9aedeee64e2a488a2e2045c3ca531f06dd540af43c3cf2ad4dea3f
|
|
BLAKE2b-256 checksum How to use checksums |
d9da69ab907791f1143a50902c12ad0ec7c30c0521d1ba27b1466446777b3746
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.15.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.8
|
Security for cloudwatch-logging 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.