• Using loguru in a library

    From Roy Hann@21:1/5 to All on Sun Apr 30 15:40:21 2023
    Is there anyone using loguru (loguru 0.5.3 in my case) successfully in a library?


    In my __init__.py in mylib I do

    logger.disable('mylib')

    which definitely works. I don't get any more logging.

    I "pip install ." the library, then in mytest.py I do

    import mylib
    logger.enable('mylib')

    expecting that it would report any log messages above level DEBUG, just
    as it does when I don't disable logging. Unfortunately it doesn't
    have any effect; it doesn't report any logging from mylib.

    I have verified that __name__ is visible in the library and is 'mylib'.

    I would also have expected that logger.enable(None) would turn all the
    logging on everywhere but it seems not.

    I have probably misunderstood how logger.enable() is supposed to be
    used. Can anyone share a brief example?

    Roy

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From MRAB@21:1/5 to Roy Hann on Mon May 1 17:59:51 2023
    On 2023-04-30 16:40, Roy Hann wrote:
    Is there anyone using loguru (loguru 0.5.3 in my case) successfully in a library?


    In my __init__.py in mylib I do

    logger.disable('mylib')

    which definitely works. I don't get any more logging.

    I "pip install ." the library, then in mytest.py I do

    import mylib
    logger.enable('mylib')

    expecting that it would report any log messages above level DEBUG, just
    as it does when I don't disable logging. Unfortunately it doesn't
    have any effect; it doesn't report any logging from mylib.

    I have verified that __name__ is visible in the library and is 'mylib'.

    I would also have expected that logger.enable(None) would turn all the logging on everywhere but it seems not.

    I have probably misunderstood how logger.enable() is supposed to be
    used. Can anyone share a brief example?

    According to the docs, the argument is the name of the module.

    In your example, the name of the module (i.e. file) is "__init__";
    "mylib" is the name of the package.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Roy Hann@21:1/5 to MRAB on Tue May 2 08:17:30 2023
    MRAB wrote:

    On 2023-04-30 16:40, Roy Hann wrote:
    Is there anyone using loguru (loguru 0.5.3 in my case) successfully in a
    library?


    In my __init__.py in mylib I do

    logger.disable('mylib')

    which definitely works. I don't get any more logging.

    I "pip install ." the library, then in mytest.py I do

    import mylib
    logger.enable('mylib')

    expecting that it would report any log messages above level DEBUG, just
    as it does when I don't disable logging. Unfortunately it doesn't
    have any effect; it doesn't report any logging from mylib.

    I have verified that __name__ is visible in the library and is 'mylib'.

    I would also have expected that logger.enable(None) would turn all the
    logging on everywhere but it seems not.

    I have probably misunderstood how logger.enable() is supposed to be
    used. Can anyone share a brief example?

    According to the docs, the argument is the name of the module.

    In your example, the name of the module (i.e. file) is "__init__";
    "mylib" is the name of the package.

    Thanks for taking a look at this. I will continue to play around with
    it using your suggestion, but according to the snippets and recipes
    for loguru at https://loguru.readthedocs.io/en/stable/resources/recipes.html#configuring-loguru-to-be-used-by-a-library-or-an-application
    I need to refer to the package name in disable() in __init__.py, not the module/file name, and so I expect to use it in enable() in my main.
    Also, as mentioned, I have already verified that __name__ in my
    package is referring to the package name not the module.

    Since every package will have a __init__.py even if it's empty, using
    the module name to control logging by package would be difficult.

    Or so I say. My code isn't working so I am wrong about some or all
    of this. I'll keep tinkering and looking out for advice/suggestions
    here.

    Roy

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dieter Maurer@21:1/5 to Roy Hann on Tue May 2 18:26:16 2023
    Roy Hann wrote at 2023-4-30 15:40 -0000:
    Is there anyone using loguru (loguru 0.5.3 in my case) successfully in a >library?
    ...
    import mylib
    logger.enable('mylib')

    expecting that it would report any log messages above level DEBUG, just
    as it does when I don't disable logging.

    Have you configured the logging system?

    Note that `logging.config.fileConfig` may do strange things
    regarding disabling (due to its default parameter `disable_existing_loggers=True`).
    I had several cases of missing log entries because `fileConfig`
    had disabled already existing loggers.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Roy Hann@21:1/5 to Dieter Maurer on Tue May 2 22:10:11 2023
    Dieter Maurer wrote:

    Roy Hann wrote at 2023-4-30 15:40 -0000:
    Is there anyone using loguru (loguru 0.5.3 in my case) successfully in a >>library?
    ...
    import mylib
    logger.enable('mylib')

    expecting that it would report any log messages above level DEBUG, just
    as it does when I don't disable logging.

    Have you configured the logging system?

    Note that `logging.config.fileConfig` may do strange things
    regarding disabling (due to its default parameter `disable_existing_loggers=True`).
    I had several cases of missing log entries because `fileConfig`
    had disabled already existing loggers.

    Thank you for the response Dieter.

    I am using the loguru package, not the logging package.

    I think my next step is going to have be creating a minimal test
    case to post on stackoverflow. :-P

    Roy

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)