|
| 1 | +"""Tests for logging configuration and the JSON formatter.""" |
| 2 | + |
| 3 | +import json |
| 4 | +import logging |
| 5 | + |
| 6 | +from wanwatcher.logconfig import JSONFormatter, build_formatter, configure_logging |
| 7 | + |
| 8 | + |
| 9 | +def _record(level=logging.INFO, msg="hello", **kwargs): |
| 10 | + return logging.LogRecord( |
| 11 | + name="wanwatcher.test", |
| 12 | + level=level, |
| 13 | + pathname=__file__, |
| 14 | + lineno=1, |
| 15 | + msg=msg, |
| 16 | + args=(), |
| 17 | + exc_info=None, |
| 18 | + **kwargs, |
| 19 | + ) |
| 20 | + |
| 21 | + |
| 22 | +def test_json_formatter_basic_fields(): |
| 23 | + out = JSONFormatter().format(_record(msg="ip changed")) |
| 24 | + data = json.loads(out) |
| 25 | + assert data["level"] == "INFO" |
| 26 | + assert data["logger"] == "wanwatcher.test" |
| 27 | + assert data["message"] == "ip changed" |
| 28 | + assert "timestamp" in data |
| 29 | + |
| 30 | + |
| 31 | +def test_json_formatter_timestamp_is_utc_iso8601(): |
| 32 | + data = json.loads(JSONFormatter().format(_record())) |
| 33 | + # ISO 8601 UTC ends with +00:00 offset |
| 34 | + assert data["timestamp"].endswith("+00:00") |
| 35 | + |
| 36 | + |
| 37 | +def test_json_formatter_includes_extra_fields(): |
| 38 | + rec = _record(msg="changed") |
| 39 | + rec.extra_fields = {"old_ip": "1.1.1.1", "new_ip": "2.2.2.2"} |
| 40 | + data = json.loads(JSONFormatter().format(rec)) |
| 41 | + assert data["old_ip"] == "1.1.1.1" |
| 42 | + assert data["new_ip"] == "2.2.2.2" |
| 43 | + |
| 44 | + |
| 45 | +def test_json_formatter_includes_exception(): |
| 46 | + try: |
| 47 | + raise ValueError("boom") |
| 48 | + except ValueError: |
| 49 | + import sys |
| 50 | + |
| 51 | + rec = _record(level=logging.ERROR, msg="failed") |
| 52 | + rec.exc_info = sys.exc_info() |
| 53 | + data = json.loads(JSONFormatter().format(rec)) |
| 54 | + assert "exception" in data |
| 55 | + assert "ValueError" in data["exception"] |
| 56 | + |
| 57 | + |
| 58 | +def test_json_formatter_message_with_args(): |
| 59 | + rec = logging.LogRecord("x", logging.INFO, __file__, 1, "value=%s", ("42",), None) |
| 60 | + data = json.loads(JSONFormatter().format(rec)) |
| 61 | + assert data["message"] == "value=42" |
| 62 | + |
| 63 | + |
| 64 | +def test_build_formatter_selects_type(): |
| 65 | + assert isinstance(build_formatter("json"), JSONFormatter) |
| 66 | + assert isinstance(build_formatter("JSON"), JSONFormatter) |
| 67 | + assert not isinstance(build_formatter("text"), JSONFormatter) |
| 68 | + assert not isinstance(build_formatter("anything-else"), JSONFormatter) |
| 69 | + |
| 70 | + |
| 71 | +def test_configure_logging_text(tmp_path): |
| 72 | + log_file = tmp_path / "ww.log" |
| 73 | + configure_logging(str(log_file), "text") |
| 74 | + logging.getLogger("wanwatcher.test").info("plain line") |
| 75 | + for h in logging.getLogger().handlers: |
| 76 | + h.flush() |
| 77 | + content = log_file.read_text(encoding="utf-8") |
| 78 | + assert "plain line" in content |
| 79 | + # text format is not JSON |
| 80 | + assert not content.strip().startswith("{") |
| 81 | + |
| 82 | + |
| 83 | +def test_configure_logging_json(tmp_path): |
| 84 | + log_file = tmp_path / "ww.log" |
| 85 | + configure_logging(str(log_file), "json") |
| 86 | + logging.getLogger("wanwatcher.test").info("json line") |
| 87 | + for h in logging.getLogger().handlers: |
| 88 | + h.flush() |
| 89 | + first = log_file.read_text(encoding="utf-8").strip().splitlines()[0] |
| 90 | + data = json.loads(first) |
| 91 | + assert data["message"] == "json line" |
| 92 | + |
| 93 | + |
| 94 | +def test_configure_logging_replaces_handlers(tmp_path): |
| 95 | + log_file = tmp_path / "ww.log" |
| 96 | + configure_logging(str(log_file), "text") |
| 97 | + count1 = len(logging.getLogger().handlers) |
| 98 | + configure_logging(str(log_file), "text") |
| 99 | + count2 = len(logging.getLogger().handlers) |
| 100 | + # handlers replaced, not stacked |
| 101 | + assert count1 == count2 |
0 commit comments