FalkTannhaeuser / python-onvif-zeep

ONVIF Client Implementation in Python 2+3 (using https://github.com/mvantellingen/python-zeep instead of suds as SOAP client)
MIT License
420 stars 137 forks source link

Importing onvif messes logs up #107

Open ludgerh opened 1 year ago

ludgerh commented 1 year ago

I have an application with extensive logging. When I import onvif from onvif import ONVIFCamera, exceptions all logging lines from the rest of the application are doubbled:

***** Software-Version:  0.9.15 *****
11.07.2023 10:00:58 [INFO    ] Started Process health...
11.07.2023 10:00:58 [INFO    ] Started Process trainer #1...
INFO:trainer #1:Started Process trainer #1...
11.07.2023 10:00:58 [INFO    ] Started Process tf_worker #1...
INFO:tf_worker #1:Started Process tf_worker #1...
11.07.2023 10:00:58 [INFO    ] Started Process stream #1...
INFO:stream #1:Started Process stream #1...
11.07.2023 10:00:58 [INFO    ] Started Process camera #1...
INFO:camera #1:Started Process camera #1...

Is there a way of disabling the logging completely and leave it to calling application?

This is my logging call, being used by several processes, each seperately:

from logging import getLogger, DEBUG, FileHandler as LogFileHandler, StreamHandler as LogStreamHandler, Formatter
from tools.l_tools import djconf, logdict
from os import path, makedirs

def log_ini(logger, logname):
  logger.setLevel(DEBUG)
  logpath = djconf.getconfig('logdir', default='data/logs/')
  if not path.exists(logpath):
    makedirs(logpath)
  loglevelstring = djconf.getconfig('loglevel', default='INFO')
  fh = LogFileHandler(logpath+logname+'.log')
  fh.setLevel(logdict[loglevelstring])
  ch = LogStreamHandler()
  ch.setLevel(logdict[loglevelstring])
  formatter = Formatter("{asctime} [{levelname:8}] {message}",
    "%d.%m.%Y %H:%M:%S","{")
  ch.setFormatter(formatter)
  fh.setFormatter(formatter)
  logger.addHandler(ch)
  logger.addHandler(fh)
  logger.info('Started Process '+logname+'...')
Waramoto commented 7 months ago

"Better Late than Never".

I fixed a similar issue today. In my case, onvif was overriding my own logging.basicConfig(), so I moved logging.basicConfig() into a separate config.py file and imported into a file that interacts with onvif, like

from config import logging

instead of

import logging

Hope this helps you.

ludgerh commented 7 months ago

...thank you, Waramoto, for your hint. As you might have guessed, I had to find a solution in the meantime. I modified client.py according to my standard way of doing the logging. I added

def init_log(mylogger):
  global logger
  if mylogger:
    logger = mylogger
  else:
    import logging
    logger = logging.getLogger('onvif')
    logging.basicConfig(level=logging.INFO)
    logging.getLogger('zeep.client').setLevel(logging.CRITICAL)

and call init_log before using ONVIF. Best regards from Aachen, Germany Ludger

tapakeht commented 3 months ago

"Better Late than Never".

I fixed a similar issue today. In my case, onvif was overriding my own logging.basicConfig(), so I moved logging.basicConfig() into a separate config.py file and imported into a file that interacts with onvif, like

from config import logging

instead of

import logging

Hope this helps you.

Thank you very much =) you gave me the idea to simply move the module import into the initialization of the fastapi application. This way, my logger was imported before the module that pulls in onvif.