Created
December 30, 2020 22:13
-
-
Save Hadlock/20763e9d4828f676b2f34d6af7a01ae3 to your computer and use it in GitHub Desktop.
Splunk-compatible structured logging python 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import datetime | |
| import time | |
| import logging | |
| import sys | |
| import structlog | |
| logging.basicConfig(stream=sys.stdout, format="%(message)s", level=logging.INFO) | |
| # batch things | |
| batch_id=int(time.time()) | |
| production_bool=False | |
| batch_job=True | |
| def add_unix_timestamp(_, __, event_dict): | |
| event_dict["timestamp"] = int(time.time()) | |
| return event_dict | |
| def add_timestamp(_, __, event_dict): | |
| event_dict["datestamp"] = str(datetime.datetime.utcnow()) | |
| return event_dict | |
| def censor_password(_, __, event_dict): | |
| pw = event_dict.get("password") | |
| if pw: | |
| event_dict["password"] = "*CENSORED*" | |
| return event_dict | |
| def add_batch_id(_, __, event_dict): | |
| event_dict["batch_id"] = batch_id | |
| return event_dict | |
| def add_production_status(_, __, event_dict): | |
| event_dict["production"] = production_bool | |
| return event_dict | |
| def add_batch_job_identifier(_, __, event_dict): | |
| event_dict["batch_job"] = batch_job | |
| return event_dict | |
| log = structlog.wrap_logger( | |
| logging.getLogger(__name__), | |
| processors=[ | |
| add_batch_id, | |
| add_unix_timestamp, | |
| add_timestamp, | |
| censor_password, | |
| add_production_status, | |
| add_batch_job_identifier, | |
| structlog.processors.JSONRenderer(indent=1, sort_keys=True), | |
| ], | |
| ) | |
| log.warning("something") | |
| time.sleep(2) | |
| log.warning("something_more", meta="whoa") | |
| time.sleep(2) | |
| log.warning("platypus", meta="whoa", widget="True") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment