Skip to content

Commit

Permalink
chore: add pydocstyle docs
Browse files Browse the repository at this point in the history
  • Loading branch information
strom-und-spiele committed Feb 16, 2022
1 parent 9c93dfc commit db36282
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions influxdb_client/client/loggingHandler.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
"""
Use the influxdb_client together with python native logging
"""
"""Use the influxdb_client with python native logging."""
import logging

from influxdb_client import InfluxDBClient


class InfluxLoggingHandler(logging.Handler):
"""
InfluxLoggingHandler instances dispatch logging events to influx.
There is no need to set a Formater.
The raw input will be passed on to the influx write api.
"""

DEFAULT_LOG_RECORD_KEYS = logging.makeLogRecord({}).__dict__.keys()

def __init__(self, *, url, token, org, bucket, client_args=None, write_api_args=None):
"""
Initialize defaults.
The arguments `client_args` and `write_api_args` can be dicts of kwargs.
They are passed on to the InfluxDBClient and write_api calls respectively.
"""
super().__init__()

self.bucket = bucket
Expand All @@ -21,15 +32,17 @@ def __init__(self, *, url, token, org, bucket, client_args=None, write_api_args=
self.write_api = self.client.write_api(**write_api_args)

def __del__(self):
"""Make sure all resources are closed."""
self.close()

def close(self) -> None:
"""Close the write_api, client and logger."""
self.write_api.close()
self.client.close()
super().close()

def emit(self, record: logging.LogRecord) -> None:
""" Emit a record via the influxDB WriteApi """
"""Emit a record via the influxDB WriteApi."""
try:
extra = self._get_extra_values(record)
return self.write_api.write(record=record.msg, **extra)
Expand All @@ -39,7 +52,11 @@ def emit(self, record: logging.LogRecord) -> None:
self.handleError(record)

def _get_extra_values(self, record: logging.LogRecord) -> dict:
"""extracts all items from the record that were injected by logging.debug(msg, extra={key: value, ...})"""
"""
Extract all items from the record that were injected via extra.
Example: `logging.debug(msg, extra={key: value, ...})`.
"""
extra = {key: value
for key, value in record.__dict__.items() if key not in self.DEFAULT_LOG_RECORD_KEYS}
if 'bucket' not in extra.keys():
Expand Down

0 comments on commit db36282

Please sign in to comment.