chrisb2 / pi_ina219

This Python library supports the INA219 voltage, current and power monitor from Texas Instruments with a Raspberry Pi using the I2C bus. The intent of the library is to make it easy to use the quite complex functionality of this sensor.
MIT License
114 stars 34 forks source link

Force library to use specified logging level #35

Closed rrnicolay closed 1 year ago

rrnicolay commented 1 year ago

I have a logging configuration file which is:

[loggers]
keys=root

[handlers]
keys=consoleHandler

[logger_root]
level=NOTSET
handlers=consoleHandler

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)
...

My code:

import logging
from ina219 import INA219, DeviceRangeError
logging.getLogger('ina219').setLevel(logging.ERROR)

class CurrentReader():
    SHUNT_OHMS = 0.1
    MAX_EXPECTED_AMPS = 3.0

    def init(self):
        self.ina = INA219(self.SHUNT_OHMS, self.MAX_EXPECTED_AMPS, busnum=1, log_level=logging.ERROR)

No matter what I do, the module is always in debug level. How can I force Ina219 to use the specified logging level instead of the one in the logging file?

rrnicolay commented 1 year ago

Ok, found the problem. Everything is fine with ina219 lib. The problem is that the I2C library gets the root logger, which is in DEBUG level (in my case). Solved it with: logging.getLogger('Adafruit_I2C.Device.Bus.1.Address.0X40').setLevel(logging.WARNING)