• Question about logging.config.dictConfig

    From Ivan "Rambius" Ivanov@21:1/5 to All on Tue Feb 7 17:58:26 2023
    Hello,

    I am trying to configure my loggers using dictConfig, but they do not
    print anything. Here are more details.

    I have several python scripts that use a similar logging setup. I put
    the common configuration in a separate module myloggingconf.py:

    # myloggingconf.py
    import logging

    def configure_logging():
    default = logging.Formatter('%(asctime)s %(levelname)-7s %(name)s %(funcName)s %(message)s')

    consoleHandler = logging.StreamHandler()
    consoleHandler.setFormatter(default)

    rootLogger = logging.getLogger()
    rootLogger.addHandler(consoleHandler)
    rootLogger.setLevel(logging.INFO)

    Then I use it in the other scripts as:

    # uselog.py
    import logging
    import os

    from myloggingconf import configure_logging

    logger = logging.getLogger(os.path.basename(__file__))

    def dosmth():
    logger.debug("debug")
    logger.info("info")
    logger.warning("warning")
    logger.error("error")

    def main():
    configure_logging()
    dosmth()

    if __name__ == '__main__':
    main()

    This works correctly and the log messages about INFO print out. Now I
    want to try dictConfig. I changed myloggingconf.configure_logging to:

    # myloggingconf.py
    import logging.config

    def configure_logging():
    config = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
    'default': {
    'fmt': '%(asctime)s %(levelname)-7s %(name)s
    %(funcName)s %(message)s'
    }
    },
    'handlers': {
    'stdout': {
    'level': 'DEBUG',
    'class': 'logging.StreamHandler',
    'formatter': 'default',
    }
    },
    'loggers': {
    '': {
    'handlers': ['stdout'],
    'level': 'DEBUG'
    }
    }
    }
    logging.config.dictConfig(config)

    When I run uselog.py it prints nothing. I am wondering what is wrong
    with the second configuration. I will appreciate any help.

    Regards
    rambius

    --
    Tangra Mega Rock: http://www.radiotangra.com

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Ivan "Rambius" Ivanov on Tue Feb 7 23:29:26 2023
    "Ivan \"Rambius\" Ivanov" <rambiusparkisanius@gmail.com> writes:
    # uselog.py

    I haven't really learned how to use logging yet, but made
    this observation just by trial and error:

    The following modified "uselog.py" prints nothing here:

    # uselog.py
    import logging
    import os

    from myloggingconf import configure_logging

    def dosmth():
    logger.debug("debug")
    logger.info("info")
    logger.warning("warning")
    logger.error("error")

    def main():
    global logger
    logger = logging.getLogger(os.path.basename(__file__))
    configure_logging()
    dosmth()

    if __name__ == '__main__':
    main()

    , but when I swap two lines, it prints something:

    # uselog.py
    import logging
    import os

    from myloggingconf import configure_logging

    def dosmth():
    logger.debug("debug")
    logger.info("info")
    logger.warning("warning")
    logger.error("error")

    def main():
    global logger
    configure_logging()
    logger = logging.getLogger(os.path.basename(__file__))
    dosmth()

    if __name__ == '__main__':
    main()

    .

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Peter J. Holzer@21:1/5 to Ivan "Rambius" Ivanov on Wed Feb 8 01:31:56 2023
    On 2023-02-07 17:58:26 -0500, Ivan "Rambius" Ivanov wrote:
    I am trying to configure my loggers using dictConfig, but they do not
    print anything. Here are more details.
    [...]
    from myloggingconf import configure_logging

    logger = logging.getLogger(os.path.basename(__file__))
    [...]

    def main():
    configure_logging()
    dosmth()
    [...]
    def configure_logging():
    config = {
    'version': 1,
    'disable_existing_loggers': True,

    'disable_existing_loggers': False,

    I think I just found out why I've been cargo-culting this line since my
    early Django days ;-)-

    If you call getLogger very early (as you do), you don't want it disabled
    by a later call to dictConfig().


    'formatters': {
    'default': {
    'fmt': '%(asctime)s %(levelname)-7s %(name)s %(funcName)s %(message)s'
    'format'
    }
    },
    [...]
    }
    logging.config.dictConfig(config)

    When I run uselog.py it prints nothing. I am wondering what is wrong
    with the second configuration.

    See above.

    hp

    --
    _ | Peter J. Holzer | Story must make more sense than reality.
    |_|_) | |
    | | | hjp@hjp.at | -- Charles Stross, "Creative writing
    __/ | http://www.hjp.at/ | challenge!"

    -----BEGIN PGP SIGNATURE-----

    iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmPi7XcACgkQ8g5IURL+ KF1EcA//aHjd+uVs4Pb9RJkGWKBi9Shk2WvsaXVDdATjHZYRP9igx8yPuinBg1iN 0p180tDKCPD2HAUkZemRtERBHSxNJmkZKyTmcQHr7fqMcr7sBKPiGk9UIINnLyA0 L0p92w192QDYIvk8BH0p8fmr4Yn3DebnssQSWFeFQXyI5Dk5bHaOaKWMNNHVt9it ZJ2N1is6jGL5HZwlZ/7rgcDgpbCtpzGhLFSOWujoS0bfm995RqhlYMyFc++rXbrT 4NsILNTBrjyYAw94aoJAFPE7LyX40ZC/3wkNFKZ7KSvfL8lAhPbWOi5oYudQqL/F FCPsKI/GtYP9Gl20tSrZmkE7aVSQSPN57o+2vfLtuzIH/Nib+Mv7KS3twd99ZLE9 u2Y/E1OI0zXUQRcEmgXwaHSsma563cvxMTN3jjLlgGC7BPkOIeFoLikWFaryjZ/C fHjyw9xeNmogbaxFhxfAsyQtg3+pH/0ysb58BNBTohM8ooqiI01zZRHSiCZjpFmp aD7unCI44U93We85R2tM2V2JW9M4V/xHnlYuPZR0e+ymWJC2uew34Sx9iGjuSkIF lEhVbhn9vQM91poENXwZqccesqXlgNt/aOK/E26IaBcjCOVduwn/UayckMLa/C6L 8qAS3KuDNamxFK7DBKpcSiYZ0XB2J2RC5pPgYwR
  • From Ivan "Rambius" Ivanov@21:1/5 to hjp-python@hjp.at on Wed Feb 8 10:27:22 2023
    On Tue, Feb 7, 2023 at 7:35 PM Peter J. Holzer <hjp-python@hjp.at> wrote:

    On 2023-02-07 17:58:26 -0500, Ivan "Rambius" Ivanov wrote:
    I am trying to configure my loggers using dictConfig, but they do not
    print anything. Here are more details.
    [...]
    from myloggingconf import configure_logging

    logger = logging.getLogger(os.path.basename(__file__))
    [...]

    def main():
    configure_logging()
    dosmth()
    [...]
    def configure_logging():
    config = {
    'version': 1,
    'disable_existing_loggers': True,

    'disable_existing_loggers': False,

    Thank you! That helped a lot!

    Regards
    rambius


    I think I just found out why I've been cargo-culting this line since my
    early Django days ;-)-

    If you call getLogger very early (as you do), you don't want it disabled
    by a later call to dictConfig().


    'formatters': {
    'default': {
    'fmt': '%(asctime)s %(levelname)-7s %(name)s %(funcName)s %(message)s'
    'format'
    }
    },
    [...]
    }
    logging.config.dictConfig(config)

    When I run uselog.py it prints nothing. I am wondering what is wrong
    with the second configuration.

    See above.

    hp

    --
    _ | Peter J. Holzer | Story must make more sense than reality.
    |_|_) | |
    | | | hjp@hjp.at | -- Charles Stross, "Creative writing
    __/ | http://www.hjp.at/ | challenge!"
    --
    https://mail.python.org/mailman/listinfo/python-list



    --
    Tangra Mega Rock: http://www.radiotangra.com

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