Rotary Logger  1.0.2
The middleware rotary logger
Loading...
Searching...
No Matches
test_split_logging.py
Go to the documentation of this file.
1"""
2# +==== BEGIN rotary_logger =================+
3# LOGO:
4# ..........####...####..........
5# ......###.....#.#########......
6# ....##........#.###########....
7# ...#..........#.############...
8# ...#..........#.#####.######...
9# ..#.....##....#.###..#...####..
10# .#.....#.##...#.##..##########.
11# #.....##########....##...######
12# #.....#...##..#.##..####.######
13# .#...##....##.#.##..###..#####.
14# ..#.##......#.#.####...######..
15# ..#...........#.#############..
16# ..#...........#.#############..
17# ...##.........#.############...
18# ......#.......#.#########......
19# .......#......#.########.......
20# .........#####...#####.........
21# /STOP
22# PROJECT: rotary_logger
23# FILE: test_split_logging.py
24# CREATION DATE: 01-11-2025
25# LAST Modified: 3:42:31 04-03-2026
26# DESCRIPTION:
27# A module that provides a universal python light on iops way of logging to files your program execution.
28# /STOP
29# COPYRIGHT: (c) Asperguide
30# PURPOSE: This is the file in charge of testing the split logging functionality of the Rotary logger class.
31# // AR
32# +==== END rotary_logger =================+
33"""
34import sys
35from pathlib import Path
36
37from rotary_logger.rotary_logger_cls import RotaryLogger
38from rotary_logger.tee_stream import TeeStream
39from rotary_logger.file_instance import FileInstance
40from rotary_logger import constants as CONST
41
42
43def test_merged_uses_same_instance(tmp_path: Path) -> None:
44 parent = tmp_path
45 # Instead of invoking RotaryLogger.start_logging (which opens a
46 # directory path), construct a shared FileInstance pointing to a file
47 # path and create two TeeStream wrappers that share it.
48 shared_file = parent / 'merged.log'
49 mixed_inst = FileInstance(shared_file, merged=True)
50 ts_out = TeeStream(mixed_inst, sys.stdout, mode=CONST.StdMode.STDOUT)
51 ts_err = TeeStream(mixed_inst, sys.stderr, mode=CONST.StdMode.STDERR)
52 # distinct stream objects but same underlying FileInstance
53 assert id(ts_out) != id(ts_err)
54 assert getattr(ts_out, "file_instance", None) is getattr(
55 ts_err, "file_instance", None)
56
57
58def test_split_uses_different_instances(tmp_path: Path) -> None:
59 parent = tmp_path
60 # Construct two separate FileInstance objects that point to different
61 # file paths and wrap them in TeeStream instances.
62 out_file = parent / 'out.log'
63 err_file = parent / 'err.log'
64 out_inst = FileInstance(out_file, merged=False)
65 err_inst = FileInstance(err_file, merged=False)
66 ts_out = TeeStream(out_inst, sys.stdout, mode=CONST.StdMode.STDOUT)
67 ts_err = TeeStream(err_inst, sys.stderr, mode=CONST.StdMode.STDERR)
68 assert id(ts_out) != id(ts_err)
69 assert getattr(ts_out, "file_instance", None) is not getattr(
70 ts_err, "file_instance", None)
71
72
74 """When merged=True and merge_stdin=True, stdin FileInstance must be the same object as stdout."""
75 from rotary_logger.rotary_logger_cls import RotaryLogger
76 rl = RotaryLogger(merge_stdin=True)
77 rl.start_logging(log_folder=tmp_path, merged=True,
78 merge_stdin=True, log_to_file=False)
79 try:
80 assert rl.stdin_stream is not None
81 assert rl.stdout_stream is not None
82 assert rl.stdin_stream.file_instance is rl.stdout_stream.file_instance
83 finally:
84 rl.stop_logging()
85
86
88 """With log_function_calls=True the [WRITE] prefix should appear in the log file."""
89 import io
90 log_file = tmp_path / 'prefixed.log'
91 orig = io.StringIO()
92 ts = TeeStream(
93 FileInstance(log_file, max_size_mb=1),
94 orig,
95 mode=CONST.StdMode.STDOUT,
96 log_function_calls=True
97 )
98 ts.write('tagged line\n')
99 ts.flush()
100 logfile = next(tmp_path.rglob('*.log'), None)
101 assert logfile is not None, 'No log file created'
102 content = logfile.read_text(encoding='utf-8')
103 assert CONST.PREFIX_FUNCTION_CALL_WRITE in content, (
104 f'Expected {CONST.PREFIX_FUNCTION_CALL_WRITE!r} prefix in log, got: {content!r}'
105 )
None test_split_uses_different_instances(Path tmp_path)
None test_merged_uses_same_instance(Path tmp_path)
None test_merge_stdin_true_shares_instance_with_stdout_stderr(Path tmp_path)
None test_log_function_calls_prefix_appears_in_log(Path tmp_path)