Search code examples
pythonclassyoutube-dl

The youtubedl logger class


I am very confused about how youtube-dl's logger class works in python and how it can be used to aid in debugging, raising warnings and error messages. Here's my following attempt at creating a logger class:

 class BuiltInLogger:

    @staticmethod
    def debug(msg):
        print(msg)

    @staticmethod
    def warning(msg):
        print(msg)

    @staticmethod
    def error(msg):
        print(msg)

I did it this way because I assume that youtube-dl creates an instance of the class whenever needed and prints out a specific message according to what error may have occurred. Please let me know if this is a good implementation of the logger class or what I can do to improve it, thanks!


Solution

  • I would suggest as rewriting class as follow:

    class BuiltInLogger:
    
        # or maybe:
        #   debug = warning = error = print
        debug = print
        warning = print
        error = print
    

    Usage:

    >>> BuiltInLogger.debug('hey', 'whats up')
    hey whats up
    

    Example with logging module

    import logging
    
    
    # set minimum level you want be printed
    logging.basicConfig(level='DEBUG')
    
    BuiltInLogger = logging.getLogger('some-module')
    # (optional) set minimum level for logger only
    BuiltInLogger.setLevel('DEBUG')
    
    BuiltInLogger.debug('hey %s', 'whats up')
    # DEBUG:some-module:hey whats up