Пример #1
0
def start_logger() -> None:
    """
    Start logging of messages passed through the python logging module.
    This sets up logging to a time based logging.
    This means that all logging messages on or above
    ``filelogginglevel`` will be written to `pythonlog.log`
    All logging messages on or above ``consolelogginglevel``
    will be written to stderr.
    ``filelogginglevel`` and ``consolelogginglevel`` are defined in the
    ``qcodesrc.json`` file.

    """
    global console_handler
    global file_handler

    # set loggers to the supplied levels
    for name, level in qc.config.logger.logger_levels.items():
        logging.getLogger(name).setLevel(level)

    root_logger = logging.getLogger()
    root_logger.setLevel(logging.DEBUG)

    # remove previously set handlers
    for handler in (console_handler, file_handler):
        if handler is not None:
            handler.close()
            root_logger.removeHandler(handler)

    # add qcodes handlers
    # console
    console_handler = logging.StreamHandler()
    console_handler.setLevel(qc.config.logger.console_level)
    console_handler.setFormatter(get_formatter())
    root_logger.addHandler(console_handler)

    # file
    filename = get_log_file_name()
    os.makedirs(os.path.dirname(filename), exist_ok=True)

    file_handler = logging.handlers.TimedRotatingFileHandler(filename,
                                                             when='midnight')

    file_handler.setLevel(qc.config.logger.file_level)
    file_handler.setFormatter(get_formatter())
    root_logger.addHandler(file_handler)

    # capture any warnings from the warnings module
    logging.captureWarnings(capture=True)

    if qc.config.telemetry.enabled:

        from applicationinsights import channel
        from applicationinsights.logging import enable

        # the telemetry_handler can be flushed
        global telemetry_handler

        loc = qc.config.GUID_components.location
        stat = qc.config.GUID_components.work_station
        sender = channel.AsynchronousSender()
        queue = channel.AsynchronousQueue(sender)
        appin_channel = channel.TelemetryChannel(context=None, queue=queue)
        appin_channel.context.user.id = f'{loc:02x}-{stat:06x}'

        # it is not completely clear which context fields get sent up.
        # Here we shuffle some info from one field to another.
        acc_name = appin_channel.context.device.id
        appin_channel.context.user.account_id = acc_name

        # note that the following function will simply silently fail if an
        # invalid instrumentation key is used. There is thus no exception to
        # catch
        telemetry_handler = enable(qc.config.telemetry.instrumentation_key,
                                   telemetry_channel=appin_channel)

    log.info("QCoDes logger setup completed")

    log_qcodes_versions(log)

    print(f'Qcodes Logfile : {filename}')
 def test_send_interval_works_as_expected(self):
     sender = channel.AsynchronousSender()
     self.assertEqual(1.0, sender.send_interval)
     sender.send_interval = 10.0
     self.assertEqual(10.0, sender.send_interval)
 def test_send_time_works_as_expected(self):
     sender = channel.AsynchronousSender()
     self.assertEqual(3.0, sender.send_time)
     sender.send_time = 10.0
     self.assertEqual(10.0, sender.send_time)
 def test_construct(self):
     sender = channel.AsynchronousSender()
     self.assertEqual('https://dc.services.visualstudio.com/v2/track',
                      sender.service_endpoint_uri)
     self.assertEqual(1.0, sender.send_interval)
     self.assertEqual(3.0, sender.send_time)