Skip to content

Commit 74f6694

Browse files
committed
tweaked logging
1 parent 6c1267b commit 74f6694

File tree

3 files changed

+33
-32
lines changed

3 files changed

+33
-32
lines changed

tests/scenariosv2/l0_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ async def sim_l0_scenario(ctx: SimulatorContext):
555555

556556
@pytest.mark.asyncio
557557
async def test_l0_scenario(request):
558-
sim = Simulator()
558+
sim = Simulator("l0_scenario")
559559

560560
await sim.start(
561561
sim_l0_scenario,

tests/scenariosv2/l2_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ async def sim_l2_scenario(ctx: SimulatorContext):
155155

156156
@pytest.mark.asyncio
157157
async def test_l2_scenario(request):
158-
sim = Simulator()
158+
sim = Simulator("l2_scenario")
159159

160160
await sim.start(
161161
sim_l2_scenario,

tests/scenariosv2/sim/core.py

+31-30
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,7 @@
88
import random
99
import time
1010

11-
TIMESTAMP = datetime.now().strftime("%Y%m%d_%H%M%S")
12-
LOGS_DIR = Path(__file__).resolve().parents[1] / "logs" / TIMESTAMP
13-
LOGS_DIR.mkdir(parents=True, exist_ok=True)
14-
15-
EXECUTIONS_LOG = LOGS_DIR / "sim.executions.log"
16-
EVENTS_LOG = LOGS_DIR / "sim.events.log"
17-
ACTIVITY_LOG = LOGS_DIR / "sim.activity.log"
18-
11+
LOGS_DIR = Path(__file__).resolve().parents[1] / ".logs"
1912

2013
logging.Formatter.formatTime = (
2114
lambda self, record, datefmt=None: datetime.fromtimestamp(record.created).isoformat(
@@ -32,6 +25,24 @@
3225
)
3326

3427

28+
def make_logger(
29+
name: str,
30+
instance: str,
31+
formatter=DEFAULT_FORMATTER,
32+
level=logging.INFO,
33+
):
34+
log_file = f"{int(time.time())}_{instance}"
35+
log_path = Path(LOGS_DIR, log_file, name).with_suffix(".log")
36+
log_path.parent.mkdir(parents=True, exist_ok=True)
37+
38+
logger = logging.getLogger(name)
39+
file_handler = logging.FileHandler(log_path, mode="w")
40+
file_handler.setFormatter(formatter)
41+
logger.addHandler(file_handler)
42+
logger.setLevel(level)
43+
return logger
44+
45+
3546
class TestFailure(Exception):
3647
"""Custom exception to signal test failures"""
3748

@@ -45,14 +56,10 @@ class BaseEvent(Enum):
4556

4657

4758
class EventManager:
48-
def __init__(self):
59+
def __init__(self, name: str):
60+
self.name = name
4961
self.events = {}
50-
self.logger = logging.getLogger("events")
51-
file_handler = logging.FileHandler(EVENTS_LOG, mode="w")
52-
file_handler.setFormatter(EVENT_FORMATTER)
53-
self.logger.addHandler(file_handler)
54-
self.logger.setLevel(logging.INFO)
55-
print(f"EvenManager initialized. Logs are saved in: {LOGS_DIR}")
62+
self.logger = make_logger("events", instance=name, level=logging.INFO)
5663

5764
async def wait_for(self, event: BaseEvent):
5865
if event not in self.events:
@@ -72,22 +79,13 @@ def is_set(self, event: BaseEvent) -> bool:
7279

7380

7481
class SimulatorContext:
75-
def __init__(self, random_wait=None):
76-
self.events = EventManager()
82+
def __init__(self, name: str, random_wait=None):
83+
self.name = name
84+
self.events = EventManager(name)
7785
self.random_wait = random_wait
7886

79-
self.logger = logging.getLogger("activity")
80-
file_handler = logging.FileHandler(ACTIVITY_LOG, mode="w")
81-
file_handler.setFormatter(DEFAULT_FORMATTER)
82-
self.logger.addHandler(file_handler)
83-
self.logger.setLevel(logging.INFO)
84-
85-
# private logger
86-
self._elogger = logging.getLogger("executions")
87-
file_handler = logging.FileHandler(EXECUTIONS_LOG, mode="w")
88-
file_handler.setFormatter(DEFAULT_FORMATTER)
89-
self._elogger.addHandler(file_handler)
90-
self._elogger.setLevel(logging.DEBUG)
87+
self.logger = make_logger("activity", instance=name, level=logging.INFO)
88+
self._elogger = make_logger("executions", instance=name, level=logging.DEBUG)
9189

9290
def unfired_events(self, events: list[BaseEvent]):
9391
evts = filter(lambda e: not self.events.is_set(e), events)
@@ -104,8 +102,11 @@ async def gather(*tasks):
104102

105103

106104
class Simulator:
105+
def __init__(self, name: str):
106+
self.name = name
107+
107108
async def start(self, *tasks, check_events=None, random_wait=None, timeout=60):
108-
context = SimulatorContext(random_wait)
109+
context = SimulatorContext(self.name, random_wait)
109110
results = None
110111

111112
try:

0 commit comments

Comments
 (0)