Skip to content

Python logging altered on import #314

Open

Description

@surgura

Importing dm_control modifies the standard python logging module. As far as I can tell this is done using Google's abseil. Abseil itself already moved away from this behaviour: abseil/abseil-py#99. Can we do this too for dm_control?

Activity

  1. nimrod-gileadi commented on Jul 1, 2022

    @nimrod-gileadi
    Contributor

    What version of absl-py do you have installed?

    dm_control requires 0.7.0 or later, but this fix was included in 0.8.0. Do you still see the issue if you manually pip install the latest absl-py?

  2. surgura commented on Jul 1, 2022

    @surgura
    Author

    I am running absl 1.1.0.

    Currently I am importing mujoco as follows to prevent this issue:

        import logging
    
        old_len = len(logging.root.handlers)
    
        from dm_control import mjcf
    
        new_len = len(logging.root.handlers)
    
        assert (
            old_len + 1 == new_len
        ), "dm_control not adding logging handler as expected."
    
        logging.root.removeHandler(logging.root.handlers[-1])
    

    Doing this or not importing mjcf at all solves the issue, so it must be somewhere in there. Thank you for your response.

  3. nimrod-gileadi commented on Jul 1, 2022

    @nimrod-gileadi
    Contributor

    I'm seeing the following behaviour from the standard logging library

    >>> import logging
    >>> len(logging.root.handlers)
    0
    >>> logging.info("banana")
    >>> len(logging.root.handlers)
    1
    

    so I don't think you're looking at the right thing. Did you see any other evidence of absl logging handlers being installed?

    In the example you posted, the new logging handler is a <StreamHandler <stderr> (NOTSET)>, which is the same thing I get if I just call logging.info myself.

  4. surgura commented on Jul 1, 2022

    @surgura
    Author

    Sorry I should have provided a minimal example. See below:

    import logging
    from dm_control import mjcf
    
    logging.basicConfig(
        level=logging.INFO,
        format="[%(asctime)s] [%(levelname)s] [%(module)s] %(message)s",
    )
    
    logging.info(f"Starting optimization")
    

    Running this gives no output. If I remove the import mjcf I do get output.

  5. JeanElsner commented on Nov 21, 2022

    @JeanElsner
    Contributor

    I recently noticed this behavior as well. The standard handler <StreamHandler <stderr> (NOTSET)> is added by the first log call (either absl or standard logging). The dm_control code base just happens to log a few messages during start-up. You can see those log messages by setting the absl log level flag or simply like this:

    import logging
    logging.basicConfig(level=logging.DEBUG)
    import dm_control.mjcf
    

    will print something like

    INFO:absl:MUJOCO_GL is not set, so an OpenGL backend will be chosen automatically.
    INFO:absl:Successfully imported OpenGL backend: glfw
    INFO:absl:MuJoCo library version is: 213
    

    You can still use basicConfig by setting force=True, which will remove any root handlers, i.e.
    logging.basicConfig(level=logging.INFO, format='[%(name)s] %(message)s', force=True)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions