Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Logging should be able to be disabled completely #787

Open
lrots opened this issue Nov 24, 2021 · 2 comments
Open

Logging should be able to be disabled completely #787

lrots opened this issue Nov 24, 2021 · 2 comments

Comments

@lrots
Copy link

lrots commented Nov 24, 2021

By using the pyvcloud.vcd.client.Client class a default log file is created as vcd_pysdk.log.

Importing VApp from pyvcloud.vcd.vapp also creates a log file called vcd_sdk.log.

This occurs due to the RotatingFileHandler which are created and registered to the logger object.

Desired behavior:

  • Logging should be turned off by default
  • If the provided file_name is not given, or given as None, no RotatingFileHandler should be initialized and registered.
  • Only initialize base logger so the client can register own handlers.

Currently this is not possible due to:

client.py:
class Client(object):
    .
    .
    .
    def _get_default_logger(self, file_name="vcd_pysdk.log",
                            log_level=logging.DEBUG,
                            max_bytes=30000000, backup_count=30):
        .
        .
        if file_name is None:
            file_name = "vcd_pysdk.log"

and

vapp.py:
from pyvcloud.vcd.client import get_logger
LOGGER = get_logger()

which points to client.py:

def get_logger(file_name="vcd_sdk.log",
               log_level=logging.DEBUG,
               max_bytes=10000000,
               backup_count=10):
    .
    .
    LOGGER = logging.getLogger(file_name)
    logHandler = handlers.RotatingFileHandler(
        filename=file_name, maxBytes=max_bytes, backupCount=backup_count)
    logHandler.setLevel(log_level)
    LOGGER.addHandler(logHandler)
    return LOGGER

As workaround I use in my code:

#Override Client class to prevent file logger from being created
from pyvcloud.vcd.client import Client as _Client
class Client(_Client):
    def _get_default_logger(self, file_name=None, log_level=logging.DEBUG):
        self._logger = logging.getLogger(file_name)
        self._logger.setLevel(log_level)

#Override get_logger function in pyvcloud.vdc.client to prevent file logger from being created
def _get_logger(file_name=None, log_level=logging.DEBUG):
    LOGGER = logging.getLogger(file_name)
    LOGGER.setLevel(log_level)
    return LOGGER

import pyvcloud.vcd.client
pyvcloud.vcd.client.get_logger = _get_logger

Import VApp from pyvcloud.vcd.vapp after the workaround code to prevent the log file vcd_sdk.log from being created.

@volehuy1998
Copy link

Same here. It is inconsistent, it must be fixed.

@zi1611061206
Copy link

I also encountered a similar issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants