Python logging.LoggerAdapter() Examples

The following are 30 code examples of logging.LoggerAdapter(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module logging , or try the search function .
Example #1
Source File: query.py    From integrations-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def run(self, logger=None, **kwargs):
        # type: (Union[logging.Logger, logging.LoggerAdapter], **Any) -> Iterator[Metric]
        if logger is None:
            logger = null_logger  # For convenience in unit tests and example scripts.

        logger.debug('document_query %s', self.name)

        for document, tags in self.source(**kwargs):
            logger.debug('%s %r', self.name, document)

            for spec in self.metrics:
                yield self._make_metric_from_spec(document, spec, tags=tags)

            for enumeration in self.enumerations:
                for metric in self._make_metrics_from_enumeration(document, enumeration, tags=tags):
                    yield metric

            for group in self.groups:
                for metric in self._make_metrics_from_group(document, group, tags=tags):
                    yield metric 
Example #2
Source File: crhelper.py    From aws-cloudformation-templates with Apache License 2.0 6 votes vote down vote up
def log_config(event, loglevel=None, botolevel=None):
    if 'ResourceProperties' in event.keys():
        if 'loglevel' in event['ResourceProperties'] and not loglevel:
            loglevel = event['ResourceProperties']['loglevel']
        if 'botolevel' in event['ResourceProperties'] and not botolevel:
            loglevel = event['ResourceProperties']['botolevel']
    if not loglevel:
        loglevel = 'WARNING'
    if not botolevel:
        botolevel = 'ERROR'
    # Set log verbosity levels
    loglevel = getattr(logging, loglevel.upper(), 20)
    botolevel = getattr(logging, botolevel.upper(), 40)
    mainlogger = logging.getLogger()
    mainlogger.setLevel(loglevel)
    logging.getLogger('boto3').setLevel(botolevel)
    logging.getLogger('botocore').setLevel(botolevel)
    # Set log message format
    logfmt = '[%(requestid)s][%(asctime)s][%(levelname)s] %(message)s \n'
    mainlogger.handlers[0].setFormatter(logging.Formatter(logfmt))
    return logging.LoggerAdapter(mainlogger, {'requestid': event['RequestId']}) 
Example #3
Source File: test_logging.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
def setUp(self):
        super(LoggerAdapterTest, self).setUp()
        old_handler_list = logging._handlerList[:]

        self.recording = RecordingHandler()
        self.logger = logging.root
        self.logger.addHandler(self.recording)
        self.addCleanup(self.logger.removeHandler, self.recording)
        self.addCleanup(self.recording.close)

        def cleanup():
            logging._handlerList[:] = old_handler_list

        self.addCleanup(cleanup)
        self.addCleanup(logging.shutdown)
        self.adapter = logging.LoggerAdapter(logger=self.logger, extra=None) 
Example #4
Source File: crhelper.py    From aws-cloudformation-templates with Apache License 2.0 6 votes vote down vote up
def log_config(event, loglevel=None, botolevel=None):
    if 'ResourceProperties' in event.keys():
        if 'loglevel' in event['ResourceProperties'] and not loglevel:
            loglevel = event['ResourceProperties']['loglevel']
        if 'botolevel' in event['ResourceProperties'] and not botolevel:
            loglevel = event['ResourceProperties']['botolevel']
    if not loglevel:
        loglevel = 'warning'
    if not botolevel:
        botolevel = 'error'
    # Set log verbosity levels
    loglevel = getattr(logging, loglevel.upper(), 20)
    botolevel = getattr(logging, botolevel.upper(), 40)
    mainlogger = logging.getLogger()
    mainlogger.setLevel(loglevel)
    logging.getLogger('boto3').setLevel(botolevel)
    logging.getLogger('botocore').setLevel(botolevel)
    # Set log message format
    logfmt = '[%(requestid)s][%(asctime)s][%(levelname)s] %(message)s \n'
    mainlogger.handlers[0].setFormatter(logging.Formatter(logfmt))
    return logging.LoggerAdapter(mainlogger, {'requestid': event['RequestId']}) 
Example #5
Source File: timeline.py    From ambianic-edge with Apache License 2.0 6 votes vote down vote up
def get_event_log(pipeline_context: PipelineContext = None) \
  -> logging.Logger:
    """Get an instance of pipeline event logger.

    :Parameters:
    ----------
    pipe_context : PipelineContext

    :Returns:
    -------
    type
        Implementation of logging.Logger that handles pipeline events

    """
    pipeline_event_log = logging.getLogger(TIMELINE_EVENT_LOGGER_NAME)
    # wrap logger in an adapter that carries pipeline context
    # such as pipeline name and current pipe element.
    pipeline_event_log = logging.LoggerAdapter(
        pipeline_event_log,
        {PIPELINE_CONTEXT_KEY: pipeline_context})
    return pipeline_event_log 
Example #6
Source File: crhelper.py    From aws-cloudformation-templates with Apache License 2.0 6 votes vote down vote up
def log_config(event, loglevel=None, botolevel=None):
    if 'ResourceProperties' in event.keys():
        if 'loglevel' in event['ResourceProperties'] and not loglevel:
            loglevel = event['ResourceProperties']['loglevel']
        if 'botolevel' in event['ResourceProperties'] and not botolevel:
            loglevel = event['ResourceProperties']['botolevel']
    if not loglevel:
        loglevel = 'WARNING'
    if not botolevel:
        botolevel = 'ERROR'
    # Set log verbosity levels
    loglevel = getattr(logging, loglevel.upper(), 20)
    botolevel = getattr(logging, botolevel.upper(), 40)
    mainlogger = logging.getLogger()
    mainlogger.setLevel(loglevel)
    logging.getLogger('boto3').setLevel(botolevel)
    logging.getLogger('botocore').setLevel(botolevel)
    # Set log message format
    logfmt = '[%(requestid)s][%(asctime)s][%(levelname)s] %(message)s \n'
    mainlogger.handlers[0].setFormatter(logging.Formatter(logfmt))
    return logging.LoggerAdapter(mainlogger, {'requestid': event['RequestId']}) 
Example #7
Source File: crhelper.py    From AWS-Transit-Gateway-Demo-MultiAccount with MIT License 6 votes vote down vote up
def log_config(event, loglevel=None, botolevel=None):
    if 'ResourceProperties' in event.keys():
        if 'loglevel' in event['ResourceProperties'] and not loglevel:
            loglevel = event['ResourceProperties']['loglevel']
        if 'botolevel' in event['ResourceProperties'] and not botolevel:
            loglevel = event['ResourceProperties']['botolevel']
    if not loglevel:
        loglevel = 'WARNING'
    if not botolevel:
        botolevel = 'ERROR'
    # Set log verbosity levels
    loglevel = getattr(logging, loglevel.upper(), 20)
    botolevel = getattr(logging, botolevel.upper(), 40)
    mainlogger = logging.getLogger()
    mainlogger.setLevel(loglevel)
    logging.getLogger('boto3').setLevel(botolevel)
    logging.getLogger('botocore').setLevel(botolevel)
    # Set log message format
    logfmt = '[%(requestid)s][%(asctime)s][%(levelname)s] %(message)s \n'
    mainlogger.handlers[0].setFormatter(logging.Formatter(logfmt))
    return logging.LoggerAdapter(mainlogger, {'requestid': event['RequestId']}) 
Example #8
Source File: crhelper.py    From AWS-Transit-Gateway-Demo-MultiAccount with MIT License 6 votes vote down vote up
def log_config(event, loglevel=None, botolevel=None):
    if 'ResourceProperties' in event.keys():
        if 'loglevel' in event['ResourceProperties'] and not loglevel:
            loglevel = event['ResourceProperties']['loglevel']
        if 'botolevel' in event['ResourceProperties'] and not botolevel:
            loglevel = event['ResourceProperties']['botolevel']
    if not loglevel:
        loglevel = 'WARNING'
    if not botolevel:
        botolevel = 'ERROR'
    # Set log verbosity levels
    loglevel = getattr(logging, loglevel.upper(), 20)
    botolevel = getattr(logging, botolevel.upper(), 40)
    mainlogger = logging.getLogger()
    mainlogger.setLevel(loglevel)
    logging.getLogger('boto3').setLevel(botolevel)
    logging.getLogger('botocore').setLevel(botolevel)
    # Set log message format
    logfmt = '[%(requestid)s][%(asctime)s][%(levelname)s] %(message)s \n'
    mainlogger.handlers[0].setFormatter(logging.Formatter(logfmt))
    return logging.LoggerAdapter(mainlogger, {'requestid': event['RequestId']}) 
Example #9
Source File: callbacks.py    From kopf with MIT License 6 votes vote down vote up
def __call__(  # lgtm[py/similar-function]
            self,
            *args: Any,
            type: str,
            event: bodies.RawEvent,
            body: bodies.Body,
            meta: bodies.Meta,
            spec: bodies.Spec,
            status: bodies.Status,
            uid: str,
            name: str,
            namespace: Optional[str],
            patch: patches.Patch,
            logger: Union[logging.Logger, logging.LoggerAdapter],
            **kwargs: Any,
    ) -> _SyncOrAsyncResult: ... 
Example #10
Source File: queue_server.py    From aitom with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self):
        self.todo_queue = queue.PriorityQueue()
        self.done_queues = {}
        self.done_tasks_time = dict()
        self.done_tasks_time_max = (60.0 * 20)
        self.broadcast_todo_queue = {}
        self.start_calc = {}
        self.out_tasks = {}
        self.proj_alive_time = {}
        self.proj_alive_time_max = (60 * 5)
        self.worker_alive_time = {}
        self.worker_alive_time_max = (60 * 30)
        self.pub_logger = logging.getLogger()
        self.pub_logger.setLevel(logging.WARN)
        if False:
            h = logging.handlers.RotatingFileHandler(filename='./server.log', mode='a', maxBytes=1000000, backupCount=10)
        else:
            h = logging.handlers.RotatingFileHandler(filename='./server.log', mode='a')
        f = logging.Formatter('%(asctime)s %(host)-16s %(job_id)-16s %(source_type)-8s %(levelname)-8s %(message)s')
        h.setFormatter(f)
        self.pub_logger.addHandler(h)
        self.logger = logging.LoggerAdapter(logging.getLogger(), {'host': os.environ.get('HOSTNAME', 'unknown'), 'job_id': os.environ.get('PBS_JOBID', 'N/A').split('.')[0], 'source_type': 'queue_server', })
        self.process = psutil.Process(os.getpid())
        _thread.start_new_thread(QueueServer.remove_dead_projects_daemon, (self,)) 
Example #11
Source File: callbacks.py    From kopf with MIT License 6 votes vote down vote up
def __call__(  # lgtm[py/similar-function]
            self,
            *args: Any,
            type: str,
            event: Union[str, bodies.RawEvent],
            body: bodies.Body,
            meta: bodies.Meta,
            spec: bodies.Spec,
            status: bodies.Status,
            uid: str,
            name: str,
            namespace: Optional[str],
            patch: patches.Patch,
            logger: Union[logging.Logger, logging.LoggerAdapter],
            diff: diffs.Diff,
            old: Optional[Union[bodies.BodyEssence, Any]],  # "Any" is for field-handlers.
            new: Optional[Union[bodies.BodyEssence, Any]],  # "Any" is for field-handlers.
            **kwargs: Any,
    ) -> bool: ... 
Example #12
Source File: ClipboardMITM.py    From pyrdp with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, client: ClipboardLayer, server: ClipboardLayer, log: LoggerAdapter, recorder: Recorder,
                 statCounter: StatCounter):
        """
        :param client: clipboard layer for the client side
        :param server: clipboard layer for the server side
        :param log: logger for this component
        :param recorder: recorder for clipboard data
        """
        self.statCounter = statCounter
        self.client = client
        self.server = server
        self.log = log
        self.recorder = recorder
        self.forwardNextDataResponse = True

        self.client.createObserver(
            onPDUReceived = self.onClientPDUReceived,
        )

        self.server.createObserver(
            onPDUReceived = self.onServerPDUReceived,
        ) 
Example #13
Source File: LiveEventHandler.py    From pyrdp with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, viewer: QRemoteDesktop, text: QTextEdit, log: LoggerAdapter, fileSystem: FileSystem, layer: PlayerLayer, tabInstance: LiveTab):
        super().__init__(viewer, text)
        self.log = log
        self.fileSystem = fileSystem
        self.layer = layer
        self.drives: Dict[int, Drive] = {}
        self.downloadDirectories: Dict[str, Directory] = {}
        self.downloadFiles: Dict[str, BinaryIO] = {}
        self.downloadDialogs: Dict[str, FileDownloadDialog] = {}
        self.tabInstance = tabInstance

        # Clicking on an item and "downloading" is a job. Only one job at a time.
        # We need to process each job independently to keep the dialog reliable
        self.jobsQueue = set()
        self.directoryDownloadQueue = set()
        self.fileDownloadQueue = set()
        self.currentDownload = None

        self.handlers[PlayerPDUType.DIRECTORY_LISTING_RESPONSE] = self.handleDirectoryListingResponse
        self.handlers[PlayerPDUType.FILE_DOWNLOAD_RESPONSE] = self.handleDownloadResponse
        self.handlers[PlayerPDUType.FILE_DOWNLOAD_COMPLETE] = self.handleDownloadComplete
        self.handlers[PlayerPDUType.CLIENT_DATA] = self.onClientData
        self.handlers[PlayerPDUType.CONNECTION_CLOSE] = self.onConnectionClose 
Example #14
Source File: callbacks.py    From kopf with MIT License 6 votes vote down vote up
def __call__(  # lgtm[py/similar-function]
            self,
            *args: Any,
            event: str,  # DEPRECATED
            body: bodies.Body,
            meta: bodies.Meta,
            spec: bodies.Spec,
            status: bodies.Status,
            uid: str,
            name: str,
            namespace: Optional[str],
            patch: patches.Patch,
            logger: Union[logging.Logger, logging.LoggerAdapter],
            diff: diffs.Diff,
            old: Optional[Union[bodies.BodyEssence, Any]],  # "Any" is for field-handlers.
            new: Optional[Union[bodies.BodyEssence, Any]],  # "Any" is for field-handlers.
            **kwargs: Any,
    ) -> _SyncOrAsyncResult: ... 
Example #15
Source File: PyPretreatMolutil.py    From PyBioMed with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, log):
        self.log = logging.LoggerAdapter(log, {"validation": type(self).__name__}) 
Example #16
Source File: callbacks.py    From kopf with MIT License 5 votes vote down vote up
def __call__(  # lgtm[py/similar-function]
            self,
            value: Optional[str],  # because it is either labels or annotations, nothing else.
            *args: Any,
            body: bodies.Body,
            meta: bodies.Meta,
            spec: bodies.Spec,
            status: bodies.Status,
            uid: str,
            name: str,
            namespace: Optional[str],
            patch: patches.Patch,
            logger: Union[logging.Logger, logging.LoggerAdapter],
            **kwargs: Any,
    ) -> bool: ... 
Example #17
Source File: handler.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def _init_session(self, session: Session):
        assert session
        log = logging.getLogger(__name__)
        self.session = session
        self.logger = logging.LoggerAdapter(log, {'client_id': self.session.client_id})
        self.keepalive_timeout = self.session.keep_alive
        if self.keepalive_timeout <= 0:
            self.keepalive_timeout = None 
Example #18
Source File: request.py    From core with MIT License 5 votes vote down vote up
def get_request_logger(request_id):
    """Given a request_id, produce a Logger or LoggerAdapter"""
    extra = {"request_id":request_id}
    logger = RequestLoggerAdapter(config.log, extra=extra)
    return logger 
Example #19
Source File: log.py    From avos with Apache License 2.0 5 votes vote down vote up
def logger(self):
        if not self._logger:
            self._logger = getLogger(self.name, self.version)
            if six.PY3:
                # In Python 3, the code fails because the 'manager' attribute
                # cannot be found when using a LoggerAdapter as the
                # underlying logger. Work around this issue.
                self._logger.manager = self._logger.logger.manager
        return self._logger 
Example #20
Source File: callbacks.py    From kopf with MIT License 5 votes vote down vote up
def __call__(  # lgtm[py/similar-function]  # << different mode
            self,
            *args: Any,
            body: bodies.Body,
            meta: bodies.Meta,
            spec: bodies.Spec,
            status: bodies.Status,
            uid: str,
            name: str,
            namespace: Optional[str],
            logger: Union[logging.Logger, logging.LoggerAdapter],
            stopped: primitives.SyncDaemonStopperChecker,  # << different type
            **kwargs: Any,
    ) -> Optional[Result]: ... 
Example #21
Source File: callbacks.py    From kopf with MIT License 5 votes vote down vote up
def __call__(  # lgtm[py/similar-function]
            self,
            *args: Any,
            logger: Union[logging.Logger, logging.LoggerAdapter],
            **kwargs: Any,
    ) -> _SyncOrAsyncResult: ... 
Example #22
Source File: simpleSystemBase.py    From Thespian with MIT License 5 votes vote down vote up
def logger(self, name=None):
        return logging.LoggerAdapter(logging.getLogger(name),
                                     {'actorAddress': self._addr})


# ---------------------------------------------------------------------- 
Example #23
Source File: callbacks.py    From kopf with MIT License 5 votes vote down vote up
def __call__(  # lgtm[py/similar-function]  # << different mode
            self,
            *args: Any,
            body: bodies.Body,
            meta: bodies.Meta,
            spec: bodies.Spec,
            status: bodies.Status,
            uid: str,
            name: str,
            namespace: Optional[str],
            logger: Union[logging.Logger, logging.LoggerAdapter],
            stopped: primitives.AsyncDaemonStopperChecker,  # << different type
            **kwargs: Any,
    ) -> Optional[Result]: ... 
Example #24
Source File: logging.py    From reframe with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, check=None, level=DEBUG):
        global _context_logger

        self._level = level
        self._orig_logger = _context_logger
        if check is not None:
            _context_logger = LoggerAdapter(_logger, check) 
Example #25
Source File: base_agent.py    From citest with Apache License 2.0 5 votes vote down vote up
def __init__(self, logger=None):
    if logger is None:
      logger = logging.getLogger(__name__)
      logger.setLevel(logging.INFO)

    self.__logger = logger
    self.__nojournal_logger = logging.LoggerAdapter(
        self.__logger, {'citest_journal': {'nojournal':True}})
    self.__default_max_wait_secs = None
    self.__config_dict = {} 
Example #26
Source File: signals.py    From king-phisher with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def send_safe_iter(signal, logger, sender, **kwargs):
	utilities.assert_arg_type(signal, str, 1)
	utilities.assert_arg_type(logger, (logging.Logger, logging.LoggerAdapter), 2)

	signal = blinker.signal(signal)
	for receiver in signal.receivers_for(sender):
		try:
			result = receiver(sender, **kwargs)
		except Exception:
			calling_frame = inspect.stack()[1]
			logger.error("an error occurred while emitting signal '{0}' from {1}:{2}".format(signal, calling_frame[1], calling_frame[2]), exc_info=True)
		else:
			yield (receiver, result)
	return 
Example #27
Source File: TCPMITM.py    From pyrdp with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, client: TwistedTCPLayer, server: TwistedTCPLayer, attacker: TwistedTCPLayer, log: LoggerAdapter,
                 state: RDPMITMState, recorder: Recorder, serverConnector: Coroutine, statCounter: StatCounter):
        """
        :param client: TCP layer for the client side
        :param server: TCP layer for the server side
        :param attacker: TCP layer for the attacker side
        :param log: logger for this component
        :param recorder: recorder for this connection
        :param serverConnector: coroutine that connects to the server side, closed when the client disconnects
        """

        self.statCounter = statCounter
        # To keep track of useful statistics for the connection.

        self.client = client
        self.server = server
        self.attacker = attacker
        self.log = log
        self.state = state
        self.recorder = recorder
        self.serverConnector = serverConnector

        # Allows a lower layer to raise error tagged with the correct sessionID
        self.client.log = log
        self.server.log = log

        self.clientObserver = self.client.createObserver(
            onConnection = self.onClientConnection,
            onDisconnection = self.onClientDisconnection,
        )

        self.serverObserver = self.server.createObserver(
            onConnection = self.onServerConnection,
            onDisconnection = self.onServerDisconnection,
        )

        self.attacker.createObserver(
            onConnection = self.onAttackerConnection,
            onDisconnection = self.onAttackerDisconnection,
        ) 
Example #28
Source File: validations.py    From MolVS with MIT License 5 votes vote down vote up
def __init__(self, log):
        self.log = logging.LoggerAdapter(log, {'validation': type(self).__name__}) 
Example #29
Source File: queue_worker.py    From aitom with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, host=None, port=None, instance=None, pool=None, tmp_dir=None):
        self.worker_id = str(uuid.uuid4())
        self.work_queue = RPCClient(host, port)
        self.handler = RPCLoggingHandler(self.work_queue)
        self.logger = logging.getLogger()
        self.logger.addHandler(self.handler)
        self.logger.setLevel(logging.DEBUG)
        self.logger = logging.LoggerAdapter(logging.getLogger(), {'host': os.environ.get('HOSTNAME', 'unknown'), 'job_id': os.environ.get('PBS_JOBID', 'N/A').split('.')[0], 'source_type': 'queue_worker', })
        if (tmp_dir is None):
            tmp_dir = os.getenv('TOMOMINER_TMP_DIR')
        assert (tmp_dir is not None)
        self.cache = Cache(tmp_dir=tmp_dir, logger=self.logger)
        self.cache_none = Cache(logger=self.logger)
        self.pool = pool 
Example #30
Source File: queue_master.py    From aitom with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, host, port):
        self.work_queue = RPCClient(host, port)
        self.handler = RPCLoggingHandler(self.work_queue)
        self.logger = logging.getLogger()
        self.logger.addHandler(self.handler)
        self.logger.setLevel(logging.DEBUG)
        self.logger = logging.LoggerAdapter(logging.getLogger(), {'host': os.environ.get('HOSTNAME', 'unknown'), 'job_id': os.environ.get('PBS_JOBID', 'N/A').split('.')[0], 'source_type': 'queue_master', })
        self.proj_id = str(uuid.uuid4())
        self.work_queue.new_project(self.proj_id)
        _thread.start_new_thread(QueueMaster.keep_alive, (self, RPCClient(host, port)))