Example #1
0
def init_logger(debug=False, verbose=False):
    """Initializes the main logger.

    Args:
        debug (bool): If True, then set logging level to DEBUG (unless @verbose is also set).
            Also, send log output to file in addition to stdout.
        verbose (bool): If True and @debug is True, then set logging level to VDEBUG
    """
    _add_vdebug_level(logging)
    root = logging.getLogger()

    if debug:
        if verbose:
            level = logging.VDEBUG
        else:
            level = logging.DEBUG
    else:
        level = logging.INFO

    root.setLevel(level)

    sh = logging.StreamHandler()
    formatter = _getFormatter()
    sh.setFormatter(formatter)
    sh.setLevel(level)
    root.addHandler(sh)

    if debug:
        logfile_path = '{}/debug.log'.format(xdg.getdir('data'))
        fh = logging.FileHandler(logfile_path)
        formatter = _getFormatter(verbose=True)
        fh.setFormatter(formatter)
        fh.setLevel(level)
        root.addHandler(fh)
        root.debug('Debug mode enabled.')
Example #2
0
def init_logger(debug: bool = False, verbose: bool = False) -> None:
    """Initializes the main logger.

    Args:
        debug (bool): If True, then set logging level to DEBUG (unless @verbose
                      is also set). Also, send log output to file in addition
                      to stdout.
        verbose (bool): If True and @debug is True, then set logging level to
                        VDEBUG.
    """
    _add_vdebug_level(logging)
    root = logging.getLogger()
    root.handlers.clear()  # clear handlers so this function is idempotent

    if debug:
        if verbose:
            level = logging.VDEBUG  # type: ignore[attr-defined]
        else:
            level = logging.DEBUG
    else:
        level = logging.INFO

    root.setLevel(level)

    sh = logging.StreamHandler()
    formatter = _getFormatter()
    sh.setFormatter(formatter)
    sh.setLevel(level)
    root.addHandler(sh)

    if debug:
        logfile_path = "{}/debug.log".format(xdg.getdir("data"))
        fh = logging.FileHandler(logfile_path)
        formatter = _getFormatter(verbose=True)
        fh.setFormatter(formatter)
        fh.setLevel(level)
        root.addHandler(fh)
        root.debug("Debug mode enabled.")
Example #3
0
def test_getdir_failure():
    """Tests that xdg.getdir raises an exception for bad arguments."""
    with pytest.raises(ValueError):
        xdg.getdir('bad_key')
Example #4
0
def test_getdir(key, expected):
    """Tests that each user directory returned meets the XDG standard."""
    assert expected == xdg.getdir(key)