Improve logging of dictionaries
This commit is contained in:
parent
d38abe28ba
commit
b4e8258de1
2 changed files with 46 additions and 7 deletions
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
import time
|
import time
|
||||||
from logging import getLogger
|
from logging import getLogger
|
||||||
|
from logging import root as logging_root
|
||||||
from logging.config import dictConfig as loggingDictConfig
|
from logging.config import dictConfig as loggingDictConfig
|
||||||
|
|
||||||
from bluepy.btle import BTLEDisconnectError # type: ignore
|
from bluepy.btle import BTLEDisconnectError # type: ignore
|
||||||
|
@ -10,9 +11,9 @@ from serial import SerialException # type: ignore
|
||||||
|
|
||||||
from .config import get_config, get_consumers, get_interface
|
from .config import get_config, get_consumers, get_interface
|
||||||
from .protocol import ChargeController
|
from .protocol import ChargeController
|
||||||
from .util import Periodical
|
from .util import LazyJSON, LoggingDictFilter, Periodical
|
||||||
|
|
||||||
logger = getLogger(__name__)
|
logger = getLogger("SolarMPPT")
|
||||||
|
|
||||||
|
|
||||||
class CommunicationError(BTLEDisconnectError, SerialException, IOError):
|
class CommunicationError(BTLEDisconnectError, SerialException, IOError):
|
||||||
|
@ -23,6 +24,10 @@ def main():
|
||||||
conf = get_config()
|
conf = get_config()
|
||||||
|
|
||||||
loggingDictConfig(conf.get("logging", {}))
|
loggingDictConfig(conf.get("logging", {}))
|
||||||
|
logging_dict_filter = LoggingDictFilter()
|
||||||
|
logging_dict_filter.data["service"] = "SolarMPPT"
|
||||||
|
logging_root.addFilter(logging_dict_filter)
|
||||||
|
|
||||||
consumers = get_consumers(conf)
|
consumers = get_consumers(conf)
|
||||||
|
|
||||||
per_voltages = Periodical(interval=15)
|
per_voltages = Periodical(interval=15)
|
||||||
|
@ -33,9 +38,13 @@ def main():
|
||||||
try:
|
try:
|
||||||
logger.info("Connecting...")
|
logger.info("Connecting...")
|
||||||
with get_interface() as dev:
|
with get_interface() as dev:
|
||||||
|
cc = ChargeController(dev)
|
||||||
|
logging_dict_filter.data["srne_model"] = cc.model
|
||||||
|
logging_dict_filter.data["srne_version"] = cc.version
|
||||||
|
logging_dict_filter.data["srne_serial"] = cc.serial
|
||||||
|
|
||||||
logger.info("Connected.")
|
logger.info("Connected.")
|
||||||
|
|
||||||
cc = ChargeController(dev)
|
|
||||||
logger.info(f"Controller model: {cc.model}")
|
logger.info(f"Controller model: {cc.model}")
|
||||||
logger.info(f"Controller version: {cc.version}")
|
logger.info(f"Controller version: {cc.version}")
|
||||||
logger.info(f"Controller serial: {cc.serial}")
|
logger.info(f"Controller serial: {cc.serial}")
|
||||||
|
@ -62,7 +71,7 @@ def main():
|
||||||
for i in range(min(days, 4)):
|
for i in range(min(days, 4)):
|
||||||
hist = cc.get_historical(i)
|
hist = cc.get_historical(i)
|
||||||
res = hist.as_dict()
|
res = hist.as_dict()
|
||||||
logger.debug({i: res})
|
logger.debug(LazyJSON({i: res}))
|
||||||
for consumer in consumers:
|
for consumer in consumers:
|
||||||
consumer.write({str(i): res})
|
consumer.write({str(i): res})
|
||||||
|
|
||||||
|
@ -71,14 +80,14 @@ def main():
|
||||||
|
|
||||||
if per_voltages(now):
|
if per_voltages(now):
|
||||||
data = cc.state.as_dict()
|
data = cc.state.as_dict()
|
||||||
logger.debug(data)
|
logger.debug(LazyJSON(data))
|
||||||
for consumer in consumers:
|
for consumer in consumers:
|
||||||
consumer.write(data)
|
consumer.write(data)
|
||||||
|
|
||||||
if per_current_hist(now):
|
if per_current_hist(now):
|
||||||
data = cc.today.as_dict()
|
data = cc.today.as_dict()
|
||||||
data.update(cc.extra.as_dict())
|
data.update(cc.extra.as_dict())
|
||||||
logger.debug(data)
|
logger.debug(LazyJSON(data))
|
||||||
for consumer in consumers:
|
for consumer in consumers:
|
||||||
consumer.write(data)
|
consumer.write(data)
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
import json
|
||||||
import time
|
import time
|
||||||
|
from logging import Filter as LoggingFilter
|
||||||
from logging import getLogger
|
from logging import getLogger
|
||||||
from typing import Optional
|
from typing import Dict, Optional
|
||||||
|
|
||||||
|
__all__ = ["humanize_number", "Periodical", "LazyJSON", "LoggingDictFilter"]
|
||||||
|
|
||||||
# Only factor of 1000
|
# Only factor of 1000
|
||||||
SI_PREFIXES_LARGE = "kMGTPEZY"
|
SI_PREFIXES_LARGE = "kMGTPEZY"
|
||||||
|
@ -35,6 +39,32 @@ def humanize_number(data, unit: str = ""):
|
||||||
return f"{data:.3g} {prefix}{unit}"
|
return f"{data:.3g} {prefix}{unit}"
|
||||||
|
|
||||||
|
|
||||||
|
class LazyJSON:
|
||||||
|
def __init__(self, data):
|
||||||
|
self.data = data
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
return json.dumps(self.data)
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return repr(self.data)
|
||||||
|
|
||||||
|
|
||||||
|
class LoggingDictFilter(LoggingFilter):
|
||||||
|
data: Dict[str, str]
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.data = {}
|
||||||
|
|
||||||
|
def filter(self, record):
|
||||||
|
print(self.data)
|
||||||
|
for key, value in self.data.items():
|
||||||
|
print(key, value)
|
||||||
|
assert not hasattr(record, key)
|
||||||
|
setattr(record, key, value)
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
class Periodical:
|
class Periodical:
|
||||||
prev: float
|
prev: float
|
||||||
interval: float
|
interval: float
|
||||||
|
|
Loading…
Reference in a new issue