Skip to content

yex.Logging

yex.logging #

Loggers #

getLogger(name) classmethod #

Gets the yex logger with the given name.

Parameters:

Name Type Description Default
name str

the name of the logger. That is, yex.logger. plus the given string

required

Raises:

Type Description
ValueError

if name refers to a "magic" logger, like "all"

KeyError

if the logger requested is unknown

Source code in yex/logging/logging.py
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
@classmethod
def getLogger(cls, name:str):
    r"""
    Gets the yex logger with the given name.

    Args:
        name: the name of the logger. That is,
            `yex.logger.` plus the given string

    Raises:
        ValueError: if name refers to a "magic" logger,
            like `"all"`
        KeyError: if the logger requested is unknown
    """
    result = LoggerKeyword.keywords[name].builtin_logger()

    return result

selectLoggers(handlers) classmethod #

Sets the loglevel of all loggers.

This behaves as described in this module's docstring.

Note that "turning a logger off" means setting its loglevel to WARNING, and "turning a logger on" means setting its level to DEBUG if "verbose" is off, and INFO otherwise.

Parameters:

Name Type Description Default
handlers str

a comma-separated list of handlers; see this module's docstring for details of the format. If this is None, we will look in the environment variable given by ENVIRON_CHOOSE_LOGGERS.

required
Source code in yex/logging/logging.py
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
@classmethod
def selectLoggers(cls,
                  handlers: str,
                  ) -> None:
    """
    Sets the loglevel of all loggers.

    This behaves as described in this module's docstring.

    Note that "turning a logger off" means setting its
    loglevel to WARNING, and "turning a logger on" means
    setting its level to DEBUG if "verbose" is off, and
    INFO otherwise.

    Args:
        handlers: a comma-separated list of handlers;
            see this module's docstring for details of the format.
            If this is `None`, we will look in the environment
            variable given by `ENVIRON_CHOOSE_LOGGERS`.
    """
    builtin_logger = builtin_logging.getLogger('yex')

    # TODO how much of this do we need to do if we're inside a test?

    # Remove existing handlers. (Test harnesses will leave them in.)
    for handler in builtin_logger.handlers:
        builtin_logger.removeHandler(handler)

    stream_handler = builtin_logging.StreamHandler(sys.stdout)
    stream_handler.setFormatter(MainLoggingFormatter())

    builtin_logger.addHandler(stream_handler)

    if not handlers:
        try:
            handlers = os.environ[ENVIRON_CHOOSE_LOGGERS]
            source = f'environment variable {ENVIRON_CHOOSE_LOGGERS}'
        except KeyError:
            handlers = DEFAULT
            source = 'default'
    else:
        source = 'command line'

    requested = set(handlers.split(','))

    unknown = requested - LoggerKeyword.keywords.keys()

    if unknown:
        print("yex: these names are unknown:")
        print("yex:   " + ' '.join(sorted(unknown)))
        print(f"yex: (from {source})")
        print("yex: For a list, do '--loggers list'.")
        sys.exit(254)

    if LIST in requested:
        print(cls.list_text())
        sys.exit(255)

    if NONE in requested:
        requested.remove(NONE)

    verbose = VERBOSE in requested
    if verbose:
        requested.remove(VERBOSE)

    if ALL in requested:
        requested = LoggerKeyword.keywords.keys() - MAGIC - requested

    for name, handler in sorted(LoggerKeyword.keywords.items()):

        if handler.magic:
            continue

        sublogger = handler.builtin_logger()
        if name not in requested:
            sublogger.setLevel(WARNING)
        elif verbose:
            sublogger.setLevel(DEBUG)
        else:
            sublogger.setLevel(INFO)