osxphotos icon indicating copy to clipboard operation
osxphotos copied to clipboard

DEBUG logging disabled globally

Open pekingduck opened this issue 1 year ago • 5 comments

osxphotos version: 0.59.3 OS: Venture 13.3.1 on M1 Mac

I am currently using osxphotos as a library in my own code. When I try to log a DEBUG message, I found that nothing is written to the log file I specify (test.log). Below is a minimal example that illustrates the issue:

import logging
import osxphotos

lg = logging.getLogger("xxx")

lg.setLevel(logging.DEBUG)
h = logging.FileHandler("test.log")
fmt = logging.Formatter('[%(asctime)s]:%(name)s:%(levelname)s %(message)s')
h.setFormatter(fmt)
lg.addHandler(h)

print(lg.isEnabledFor(logging.DEBUG)) # Returns False
lg.debug("debug message")

Everything's OK if I comment out import osxphotos.

After digging around a bit, I found DEBUG logging is disabled globally in osxphotos/__init__.py:

logging.basicConfig(
    level=logging.DEBUG,
    format="%(asctime)s - %(name)s - %(levelname)s - %(filename)s - %(lineno)d - %(message)s",
)
logger: logging.Logger = logging.getLogger("osxphotos")
if not is_debug():
    logging.disable(logging.DEBUG)

logging.basicConfig() and logging.disable() configure the root logger instead of the osxphotos logger. Perhaps it's better to change the settings of osxphotos logger only? For example:

import sys
import logging

logger: logging.Logger = logging.getLogger("osxphotos")
logger.setLevel(logging.DEBUG)
h = logging.StreamHandler(sys.stderr)
fmt = logging.Formatter(...)
h.setFormatter(fmt)
logger.addHandler(h)
...

pekingduck avatar Apr 13 '23 03:04 pekingduck

Good point -- agree that osxphotos should configure only the osxphotos logger.

RhetTbull avatar Apr 13 '23 03:04 RhetTbull

@all-contributors please add @pekingduck for bug

RhetTbull avatar Apr 13 '23 03:04 RhetTbull

@RhetTbull

I've put up a pull request to add @pekingduck! :tada:

allcontributors[bot] avatar Apr 13 '23 03:04 allcontributors[bot]

moving from osxphotos/init.py to osxphotos/main.py the code part that should not be executed when used as a library might be the way to go. That would leave the logger declaration in init and move the config (basicConfig and if not is_debug) to main

fkhn avatar Aug 17 '24 21:08 fkhn

Thanks for the reminder on this issue -- will try to get it fixed in the next release. I agree that osxphotos shouldn't do this globally -- that part of the code was written when I was first learning python and didn't fully understand the logging package.

RhetTbull avatar Aug 18 '24 00:08 RhetTbull