bitmovin / bitmovin-python

DEPRECATED: Python client for the Bitmovin API, see https://github.com/bitmovin/bitmovin-api-sdk-python
The Unlicense
38 stars 20 forks source link

Multiple loggers if BitmovinObject is created multiple times #71

Closed 153957 closed 6 years ago

153957 commented 6 years ago

Currently a new console logging handler is attached to the BitmovinObject each time it is initialized: https://github.com/bitmovin/bitmovin-python/blob/master/bitmovin/bitmovin_object.py#L12 This causes multiple log outputs for the same log call if the API is initialized multiple times.

import bitmovin

# Initialize the API once, call a method, results in one log line.
api = bitmovin.Bitmovin(api_key='SECRET_CODE')
api.inputs.HTTPS.list()
# […datetime…] bitmovin.rest.http_client.BitmovinHttpClient INFO     REQUEST: GET encoding/inputs/https?offset=0&limit=100

# Initialize the API additional times, call a method, results in multiple log lines.
bitmovin.Bitmovin(api_key='SECRET_CODE')
bitmovin.Bitmovin(api_key='SECRET_CODE')
api.inputs.HTTPS.list()
# […datetime…] bitmovin.rest.http_client.BitmovinHttpClient INFO     REQUEST: GET encoding/inputs/https?offset=0&limit=100
# […datetime…] bitmovin.rest.http_client.BitmovinHttpClient INFO     REQUEST: GET encoding/inputs/https?offset=0&limit=100
# […datetime…] bitmovin.rest.http_client.BitmovinHttpClient INFO     REQUEST: GET encoding/inputs/https?offset=0&limit=100

Perhaps simply check if one or more handlers already exist, for example:

class BitmovinObject(object):
    def __init__(self, *args, **kwargs):
        self.logger = logging.getLogger(self.__class__.__module__ + "." + self.__class__.__name__)
        if not self.logger.hasHandlers():
            self.logger.setLevel(logging.DEBUG)
            console_handler = logging.StreamHandler(sys.stdout)
            formatter = logging.Formatter('%(asctime)-15s %(name)-5s %(levelname)-8s %(message)s')
            console_handler.setFormatter(formatter)
            self.logger.addHandler(console_handler)
dominic-miglar commented 6 years ago

Fixed in https://github.com/bitmovin/bitmovin-python/releases/tag/v1.23.0