summaryrefslogtreecommitdiffstats
path: root/src/ansiblelint/logger.py
blob: f0477cd59ce66571c58afe06b831f5c296003b6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""Utils related to logging."""
import logging
import time
from collections.abc import Iterator
from contextlib import contextmanager
from typing import Any

_logger = logging.getLogger(__name__)


@contextmanager
def timed_info(msg: Any, *args: Any) -> Iterator[None]:
    """Context manager for logging slow operations, mentions duration."""
    start = time.time()
    try:
        yield
    finally:
        elapsed = time.time() - start
        _logger.info(msg + " (%.2fs)", *(*args, elapsed))  # noqa: G003


def warn_or_fail(message: str) -> None:
    """Warn or fail depending on the strictness level."""
    # pylint: disable=import-outside-toplevel
    from ansiblelint.config import options
    from ansiblelint.errors import StrictModeError

    if options.strict:
        raise StrictModeError(message)

    _logger.warning(message)