Skip to content

Relax constraint on logger class in register_loggerΒ #4783

Closed

Description

@RustingSword

Summary

Allow any logger instance to be registered via register_logger, as long as it has info and warning methods defined.

Motivation

Currently only instance of logging.Logger or its subclass can be registered. I'd like to use logger from loguru, which is more convenient, however it will trigger TypeError now. Since only info and warning are used to log messages, any logger class providing these two methods would suffice.

Description

Instead of checking the class of logger, just check the existence of info and warning method.

Reference implementation:

def register_logger(logger) -> None:
    def _has_method(method):
        return callable(getattr(logger, method, None))

    if not _has_method("info") or not _has_method("warning"):
        raise TypeError("Logger must provide 'info' and 'warning' method")
    global _LOGGER
    _LOGGER = logger

References

N/A

Activity

  1. jameslamb commented on Dec 4, 2021

    @jameslamb
    Member

    Thanks very much for the excellent feature request, and sorry for the delayed response. I totally support this relaxed constraint.

    @StrikerRUS @jmoralez what do you think?

    @RustingSword are you interested in contributing this? If you are and if other Python maintainers I've tagged support the idea, I'd be happy to answer any questions you have about the contribution process.

  2. RustingSword commented on Dec 4, 2021

    @RustingSword
    ContributorAuthor

    Sure, happy to contribute :)

  3. StrikerRUS commented on Dec 4, 2021

    @StrikerRUS
    Collaborator

    I support the idea of allowing to register custom advanced logger classes.

    Also, I think that for the forward compatibility we should ask custom class to provide error() method, even though we are not using it right now.

  4. github-actions commented on Aug 15, 2023

    @github-actions
    Contributor

    This issue has been automatically locked since there has not been any recent activity since it was closed.
    To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues
    including a reference to this.

  5. locked as resolved and limited conversation to collaborators on Aug 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions