meraki / dashboard-api-python

Official Dashboard API library (SDK) for Python
MIT License
293 stars 154 forks source link

Logging level #217

Closed HyazOulephant closed 1 year ago

HyazOulephant commented 1 year ago

In meraki.DashboardAPI(...) function, it seems that some parts of the code are unreachable. I'm attempting to limit logging to console.

d = meraki.DashboardAPI(api_key=meraki_api_key,
                                print_console=True,
                                output_log=False,
                                caller="caller",
                                maximum_retries = 50,
                                nginx_429_retry_wait_time = 2
                                )

In the function's code, it looks like last if loops are not reachable because a logging system is configured by default.

...
        # Configure logging
        if not suppress_logging:
            self._logger = logging.getLogger(__name__) # This defines a logging system

            if not inherit_logging_config:
                self._logger.setLevel(logging.DEBUG) # This configure the logging system

                formatter = logging.Formatter(
                    fmt='%(asctime)s %(name)12s: %(levelname)8s > %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S'
                )
                handler_console = logging.StreamHandler()
                handler_console.setFormatter(formatter)

                if output_log:
                    if log_path and log_path[-1] != '/':
                        log_path += '/'
                    self._log_file = f'{log_path}{log_file_prefix}_log__{datetime.now():%Y-%m-%d_%H-%M-%S}.log'
                    handler_log = logging.FileHandler(
                        filename=self._log_file
                    )
                    handler_log.setFormatter(formatter)

                if output_log and not self._logger.hasHandlers(): # Wont go through because of logging system
                    self._logger.addHandler(handler_log)
                    if print_console:
                        handler_console.setLevel(logging.INFO)
                        self._logger.addHandler(handler_console)
                elif print_console and not self._logger.hasHandlers(): # Wont go through because of logging system
                    self._logger.addHandler(handler_console)
...

Is this a bug ? If not, how can I manually define the logging level to get Warning and upper level logs only ? Thank you

thomaschristory commented 1 year ago

I am running into the exact same issue. This is pretty annoying.

TKIPisalegacycipher commented 1 year ago

Hi @HyazOulephant -- per config.py this is just the default config if you don't inherit your parent application's logging scheme.

See lines 51-53

# You might integrate the library in an application with a predefined logging scheme. If so, you may not need the
# library's default logging handlers, formatters etc.--instead, you can inherit an external logger instance.
INHERIT_LOGGING_CONFIG = False

So if you want to define it differently, set TRUE and then define the logging scheme in your parent application.

Alternatively, if you'd like to enable changing the logging levels more granularly inside the library, we'd welcome a backwards-compatible PR that enabled that.

In any case, this is not a bug. But thanks for asking!