Skip to content

Logging

TELECOM-NET-SIM uses the standard logging module via a thin wrapper in telecom_logging.py. No custom framework, no external dependencies.

Quick start

from telecom_logging import setup_logging, get_logger

setup_logging(level="INFO")
logger = get_logger(__name__)
logger.info("hello")

CLI options

The simulator (telecom_net_sim.py) exposes three flags:

Flag Effect
--verbose, -v Enable DEBUG-level logging
--quiet, -q Only show WARNING and above
--log-file PATH Also write logs to PATH (UTF-8)

Examples

python telecom_net_sim.py
python telecom_net_sim.py --verbose
python telecom_net_sim.py --quiet
python telecom_net_sim.py --verbose --log-file logs/run.log
python telecom_net_sim.py --subs 100 --cdrs 500 --verbose

Log format

HH:MM:SS | LEVEL   | module                | message

Example:

10:51:00 | INFO    | telecom_net_sim       | [1/7] Building network topology...
10:51:00 | INFO    | telecom_net_sim       |       Cells: 294 | Core nodes: 119
10:51:00 | INFO    | telecom_attack        | Generating 25 scenarios...
10:51:00 | WARNING | telecom_attack        | Attack simulation skipped
  • Timestamp: HH:MM:SS (local time).
  • Level: DEBUG / INFO / WARNING / ERROR / CRITICAL.
  • Module: the logger name (usually __name__).
  • Message: free-form.

Why not just print

  • Levels: filter at runtime (--quiet in CI, --verbose when debugging).
  • Routing: stdout, stderr, a file, or all three.
  • Testability: caplog fixture lets you assert log messages in tests.
  • Metadata: timestamp, level, and module name come for free.

Custom usage

from telecom_logging import setup_logging, get_logger, silence

setup_logging(level="DEBUG", log_file="logs/app.log")

logger = get_logger(__name__)
logger.debug("detailed trace")
logger.info("normal progress")
logger.warning("recoverable issue")
logger.error("hard failure")

silence("streamlit", level=30)

Testing

import logging
from telecom_logging import setup_logging, get_logger

def test_logs_message(caplog):
    setup_logging(level="INFO")
    logger = get_logger("my.module")
    with caplog.at_level(logging.INFO):
        logger.info("hello")
    assert "hello" in caplog.text

The full suite lives in tests/test_logging.py.

Design notes

  • Idempotent: calling setup_logging() twice does not duplicate handlers.
  • UTF-8 safe: file handler uses encoding="utf-8".
  • No global state pollution: previous handlers are removed first.
  • Report output is not logging: the final report.txt is still produced by build_report() using print().

API reference

telecom_logging.setup_logging

setup_logging(level: str = 'INFO', log_file: str | None = None, simple: bool = False) -> None

Configure the root logger.

Idempotent: safe to call multiple times — old handlers are removed.

Parameters:

Name Type Description Default
level str

one of DEBUG / INFO / WARNING / ERROR / CRITICAL.

'INFO'
log_file str | None

optional path to a log file (UTF-8).

None
simple bool

if True, use a compact format without timestamps.

False
Source code in telecom_logging.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def setup_logging(
    level: str = "INFO",
    log_file: str | None = None,
    simple: bool = False,
) -> None:
    """Configure the root logger.

    Idempotent: safe to call multiple times — old handlers are removed.

    Args:
        level: one of DEBUG / INFO / WARNING / ERROR / CRITICAL.
        log_file: optional path to a log file (UTF-8).
        simple: if True, use a compact format without timestamps.
    """
    root = logging.getLogger()

    # Remove any handlers added by previous calls (important for tests).
    for h in list(root.handlers):
        root.removeHandler(h)

    fmt = _SIMPLE_FMT if simple else _VERBOSE_FMT
    formatter = logging.Formatter(fmt, datefmt=_DATE_FMT)

    # Console handler
    console = logging.StreamHandler(stream=sys.stdout)
    console.setFormatter(formatter)
    root.addHandler(console)

    # File handler (optional)
    if log_file:
        path = Path(log_file)
        path.parent.mkdir(parents=True, exist_ok=True)
        fh = logging.FileHandler(path, encoding="utf-8", mode="a")
        fh.setFormatter(formatter)
        root.addHandler(fh)

    root.setLevel(_parse_level(level))

telecom_logging.get_logger

get_logger(name: str) -> logging.Logger

Return a module-level logger.

Thin wrapper around logging.getLogger so callers depend only on this module, not directly on the stdlib.

Source code in telecom_logging.py
79
80
81
82
83
84
85
def get_logger(name: str) -> logging.Logger:
    """Return a module-level logger.

    Thin wrapper around ``logging.getLogger`` so callers depend only on
    this module, not directly on the stdlib.
    """
    return logging.getLogger(name)

telecom_logging.silence

silence(name: str, level: int = logging.WARNING) -> None

Raise the level of a specific logger.

Useful for quieting noisy third-party libraries.

Source code in telecom_logging.py
 95
 96
 97
 98
 99
100
def silence(name: str, level: int = logging.WARNING) -> None:
    """Raise the level of a specific logger.

    Useful for quieting noisy third-party libraries.
    """
    logging.getLogger(name).setLevel(level)