Skip to content

Commit

Permalink
docs: add a logging handler example
Browse files Browse the repository at this point in the history
  • Loading branch information
strom-und-spiele committed Feb 16, 2022
1 parent 8ccdce3 commit cc8ce89
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions examples/logging_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
Show the usage of influx with python native logging.
This is useful if you
* want to log to influx and a local file.
* want to set up influx logging in a project without specifying it in submodules
"""
import datetime
import logging
import time

from influxdb_client import InfluxLoggingHandler, WritePrecision, Point

DATA_LOGGER_NAME = '…'


def setup_logger():
"""
Set up data logger with the influx logging handler.
This can happen in your core module.
"""
influx_logging_handler = InfluxLoggingHandler(url="…", token="…", org="…", bucket="…",
client_args={'arg1': '…'},
write_api_args={'arg': '…'})
influx_logging_handler.setLevel(logging.DEBUG)

data_logger = logging.getLogger(DATA_LOGGER_NAME)
data_logger.setLevel(logging.DEBUG)
data_logger.addHandler(influx_logging_handler)
# feel free to add other handlers here.
# if you find yourself writing filters e.g. to only log points to influx, think about adding a PR :)


def use_logger():
"""Use the logger. This can happen in any submodule."""
# `data_logger` will have the influx_logging_handler attached if setup_logger was called somewhere.
data_logger = logging.getLogger(DATA_LOGGER_NAME)
# write a line yourself
data_logger.debug(f"my-measurement,host=host1 temperature=25.3 {int(time.time() * 1e9)}")
# or make use of the influxdb helpers like Point
data_logger.debug(
Point('my-measurement')
.tag('host', 'host1')
.field('temperature', 25.3)
.time(datetime.datetime.utcnow(), WritePrecision.MS)
)

0 comments on commit cc8ce89

Please sign in to comment.