def get_logger_for_context(context): """Create logger for context. :param context: :return: the logger object :rtype: logging.Logger """ if is_instance_of(context, "AutoLoadCommandContext"): log_group = INVENTORY resource_name = context.resource.name elif is_instance_of(context, "ResourceCommandContext"): log_group = (context.reservation.reservation_id if context.reservation else INVENTORY) resource_name = context.resource.name elif is_instance_of(context, "ResourceRemoteCommandContext"): log_group = (context.remote_reservation.reservation_id if context.remote_reservation else INVENTORY) resource_name = context.remote_endpoints[0].name elif is_instance_of(context, "UnreservedResourceCommandContext"): log_group = DELETE_ARTIFACTS resource_name = context.resource.name else: raise Exception( "get_logger_for_context", "Unsupported command context provided {0}".format(context), ) exec_info = LoggingSessionContext.get_execution_info(context) qs_logger = get_qs_logger(log_group=log_group, log_category="QS", log_file_prefix=resource_name) log_execution_info(qs_logger, exec_info) return qs_logger
def __init__(self): self.automation_api = api_helpers.get_api_session() self.workflow = Workflow(self) self.suppress_exceptions = True self._exception = None self.connectivityContextDetails = helpers.get_connectivity_context_details( ) self.reservationContextDetails = helpers.get_reservation_context_details( ) self.reservationLifecycleDetails = helpers.get_lifecycle_context_details( ) self.global_inputs = helpers.get_global_inputs() self.additional_info_inputs = helpers.get_resource_additional_info_inputs( ) self.requirement_inputs = helpers.get_resource_requirement_inputs() self.id = self.reservationContextDetails.id reservation_description = self.automation_api.GetReservationDetails( reservationId=self.id, disableCache=True).ReservationDescription self.name = reservation_description.Name self.components = Components(reservation_description.Resources, reservation_description.Services, reservation_description.Apps) self.logger = get_qs_logger( log_file_prefix='CloudShell Sandbox Orchestration', log_group=self.id, log_category='Orchestration') self.apps_configuration = AppsConfiguration(sandbox=self)
def __init__(self): self.automation_api = api_helpers.get_api_session() self.workflow = Workflow(self) self.suppress_exceptions = True self._exception = None self.connectivityContextDetails = helpers.get_connectivity_context_details() self.reservationContextDetails = helpers.get_reservation_context_details() self.reservationLifecycleDetails = helpers.get_lifecycle_context_details() self.global_inputs = helpers.get_global_inputs() self.additional_info_inputs = helpers.get_resource_additional_info_inputs() self.requirement_inputs = helpers.get_resource_requirement_inputs() self.id = self.reservationContextDetails.id reservation_description = self.automation_api.GetReservationDetails(reservationId=self.id, disableCache=True).ReservationDescription self.name = reservation_description.Name self.components = Components(reservation_description.Resources, reservation_description.Services, reservation_description.Apps) self.logger = get_qs_logger(log_file_prefix='CloudShell Sandbox Orchestration', log_group=self.id, log_category='Orchestration') self.apps_configuration = AppsConfiguration(sandbox=self)
def test_get_qs_logger_full_settings(self): """Test suite for get_qs_logger method.""" qs_logger.get_settings = full_settings self.assertTrue( isinstance( qs_logger.get_qs_logger(log_group="test1").handlers[0], MultiProcessingLog, ))
def test_get_qs_logger_stream_handler(self): """Test suite for get_qs_logger method.""" if "LOG_PATH" in os.environ: del os.environ["LOG_PATH"] qs_logger.get_settings = cut_settings self.assertTrue( isinstance( qs_logger.get_qs_logger(log_group="test2").handlers[0], logging.StreamHandler, ))
def _initialize_logger(config_operations): """ :type config_operations: cloudshell.migration.operations.config_operations.ConfigOperations """ os.environ['LOG_PATH'] = config_operations.read_key_or_default( config_operations.KEY.LOG_PATH) logger = get_qs_logger(str(PACKAGE_NAME), 'migration_tool', 'migration_tool') logger.setLevel( config_operations.read_key_or_default(config_operations.KEY.LOG_LEVEL)) click.echo('Log file: {}'.format(logger.handlers[0].baseFilename)) return logger
def test_get_qs_logger_container_filling(self): """Test suite for get_qs_logger method.""" qs_logger.get_settings = full_settings qs_logger.get_qs_logger() qs_logger.get_qs_logger(log_group="test1") if "LOG_PATH" in os.environ: del os.environ["LOG_PATH"] qs_logger.get_settings = cut_settings qs_logger.get_qs_logger(log_group="test2") self.assertEqual( sorted(qs_logger._LOGGER_CONTAINER.keys()), sorted(["Ungrouped", "test1", "test2"]), )
def test_get_qs_logger_full_settings_default_params(self): """Test suite for get_qs_logger method.""" qs_logger.get_settings = full_settings self.assertTrue( isinstance(qs_logger.get_qs_logger().handlers[0], MultiProcessingLog))
def initialize(self, context: InitCommandContext) -> None: """Initialize Ixia chassis shell (from API).""" self.logger = get_qs_logger(log_group="traffic_shells", log_file_prefix=context.resource.name) self.logger.setLevel(logging.DEBUG)
Test test_helpers. """ # pylint: disable=redefined-outer-name import logging from typing import Iterable import pytest from shellfoundry_traffic.test_helpers import TestHelpers, create_session_from_config from cloudshell.api.cloudshell_api import CloudShellAPISession from cloudshell.logging.qs_logger import get_qs_logger from cloudshell.sandbox_rest.sandbox_api import SandboxRestApiSession from cloudshell.traffic.helpers import ReservationOutputHandler, get_reservation_id from cloudshell.workflow.orchestration.sandbox import Sandbox logger = get_qs_logger() logger.setLevel(logging.DEBUG) REST_SERVER = "localhost" @pytest.fixture def session() -> CloudShellAPISession: """Yield CloudShell session.""" return create_session_from_config() @pytest.fixture def test_helpers(session: CloudShellAPISession) -> Iterable[TestHelpers]: """Yield configured TestHelpers object with reservation."""
def _get_logger(context): logger = get_qs_logger(context.reservation.reservation_id) logger.setLevel(0) return logger