Skip to main content

AWS Cloudwatch Lightweight Handler

This small log handler is designed to send events to AWS Cloudwatch. It is useful when operating infrastructure outside of the AWS Serverless Infrastructure (e.g. Lambdas or ECS). You can still integrate it in your Serverless Infrastructure, but you might find it easier to just let AWS Handle the logs in this cases.

We originally developed this to be used on dedicated servers (on and off EC2) and chose to create something new because we wanted:

  • Provide your own AWS programmatic access credentials
  • Lightweight (only the handler is included and you need only one file)

If you already have a codebase that is using python's logger, you only need minor modifications to send your logs to AWS. In fact, you only need to change code at the logger creation. If you havent done any logging before, I recommend you look at a basic tutorial on python's logging module. There are plenty of resources out there.

Installing it

We are on PyPi, so you can install via pip

pip install cloudwatch

Or if you prefer to customise and (hopefully) feedback on improvements and bugs

git clone https://github.com/ernestomonroy/cloudwatch

Usage

This module is designed to fit right into your existing logging code, so you only need to replace (or add) a handler and the same logger will send events to cloudwatch.

import logging
#Create the logger
logger = logging.getLogger('my_logger')
#Create the formatter
formatter = logging.Formatter('%(asctime)s : %(levelname)s - %(message)s')

#Import cloudwath and create the new handler
from cloudwatch import cloudwatch
handler = cloudwatch.CloudwatchHandler(log_group = 'my_log_group')

#Pass the formater to the handler
handler.setFormatter(formatter)
#Set the level
logger.setLevel(logging.WARNING)
#Add the handler to the logger
logger.addHandler(handler)

#USE IT!
logger.warning("Watch out! Something happened!")

Specifying credentials and region

If you dont add credentials when creating the handler, it uses the default AWS credentials that you set-up on the CLI, or that you passed on the invokation (if using on EC2, Lambda, ECS), this is in line with the boto3 configuration Expained here. However, you can also specify the credentials like this:

handler = cloudwatch.CloudwatchHandler(
 log_group = 'my_log_group',
 access_id = 'AWS_ACCESS_KEY_ID', 
 access_key = 'AWS_SECRET_ACCESS_KEY'
)

Likewise, you can specify a region where the logger will be with the parameter: region = 'us-east-1'

Specifying log stream

If you dont add a log stream, the logger will create one for you with the timestamp and a random number. This is useful when you have multiple processes logging to the same log group without colliding. If you want, you can specify the log stream like this:

handler = cloudwatch.CloudwatchHandler(
 log_group = 'my_log_group',
 log_stream = 'my_log_stream'
)

Legacy

We much prefer keyword arguments, and encourage you to use them. However, if you really want to avoid some typing, the order of the positional arguments work as follows:

handler = cloudwatch.CloudwatchHandler(
 'AWS_ACCESS_KEY_ID',
 'AWS_SECRET_ACCESS_KEY',
 'REGION',
 'LOG_GROUP',
 'LOG_STREAM'
)

Parameters

Positional order Keyword argument Required Default Description
0 access_id No Taken from the AWS Configuration File or Role The AWS Access Key ID that you want to use to interact with your AWS Account. Usually a 20 character alphanumeric.
1 access_key No Taken from the AWS Configuration File or Role The corresponding AWS Secret to the above parameter
2 region No Taken from the AWS Configuration File or Role The AWS Region name (e.g. us-east-1)
3 log_group Yes The name of the log group. If it already exists, it writes to it, otherwise it creates it.
4 log_stream No Datetime in the format %Y%m%d%H%M%S%f and 3 random digits The name of the log stream. If it already exists, it writes to it, otherwise it creates it.

Metadata

Release files for cloudwatch 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cloudwatch 1.1.0
File Size Uploaded Provenance Actions
cloudwatch-1.1.0.tar.gz 5.2 kB Without provenance Details for cloudwatch-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cloudwatch 1.1.0
File Interpreter ABI Platform Actions
cloudwatch-1.1.0-py3-none-any.whl Python 3 none any Details for cloudwatch-1.1.0-py3-none-any.whl

Total release size: 10.8 kB

Release files / cloudwatch-1.1.0.tar.gz

Download URL cloudwatch-1.1.0.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
1bbe2fc668fbc78226b321a8f2ae7c9f63027da9618a55569c9e0c8f17df8a58
BLAKE2b-256 checksum
How to use checksums
38d8c6aa81fba2c82de5b2d7ea74141d1ec4b7dc5f98738d1bf2e126696b38d0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release files / cloudwatch-1.1.0-py3-none-any.whl

Download URL cloudwatch-1.1.0-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6b175d90025cfb953912c3582afb3be74d8c9291598dac953b41cfe78e61834f
BLAKE2b-256 checksum
How to use checksums
e6575f4a617a743bcc6bda67002d16a1256d952bcc3d2c235f17d73f86eda689
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Security for cloudwatch 1.1.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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page