Lightweight AWS Cloudwatch Handler for Python Logging
Lightweight Handler for Python's standard logging to send logs to AWS Cloudwatch. There are already other libraries that do a similar job, however, this one has a couple of features that I couldnt find elsewhere:
- Provide your own AWS programmatic access credentials
- Lightweight (only the handler is included)
If you already have a codebase that is using python's logger, you 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.
If you find this useful dont forget to contribute by pressing the ★ Star button :smile:!
If you want to get up and running quickly across different projects, simply install with pip:
pip install cloudwatch
You also have the option of cloning or forking from Github if you would like to modify (and hopefully feedback!). allows you to tweak the code if you want.
git clone https://github.com/ernestomonroy/cloudwatch
Also, you will need the boto3 library from AWS. Its also available from pypi
pip install boto3
The typical python logger looks something like this:
import logging #Create the logger logger = logging.getLogger('my_logger') #Create the formatter formatter = logging.Formatter('%(asctime)s : %(levelname)s - %(message)s') #Create the handler handler = logging.FileHandler('path/to/log', mode='a+') #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!")
The important line here is where we create the handler. In the case above, we use a
FileHandler, which as its name suggests places your logs into a file in the specified location. However, instead of logging to files, we will now be logging to AWS, so we need a new type of handler.
After you clone the repo to your directory, your new code will look like this:
import logging from cloudwatch import cloudwatch #Create the logger logger = logging.getLogger('my_logger') #Create the formatter formatter = logging.Formatter('%(asctime)s : %(levelname)s - %(message)s') # ---- Create the Cloudwatch Handler ---- handler = cloudwatch.CloudwatchHandler('AWS_KEY_ID','AWS_SECRET_KEY','AWS_REGION','AWS_LOG_GROUP','AWS_LOG_STREAM') #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!")
The only difference is we added the cloudwatch import, and changed the handler creation line. Thats it!
- Add support to declare the retention period
- Add support to get credentials straight from OS Environment Variables
Of course if you have issues please report them or send me a message with suggestions!
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size cloudwatch-0.0.5-py3-none-any.whl (4.8 kB)||File type Wheel||Python version py3||Upload date||Hashes View|
|Filename, size cloudwatch-0.0.5.tar.gz (3.8 kB)||File type Source||Python version None||Upload date||Hashes View|
Hashes for cloudwatch-0.0.5-py3-none-any.whl