Option C Tools
Loading...
Searching...
No Matches
log_writer.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# oct/core/log_writer.py
4
5"""
6Purpose
7-------
8Write structured JSON command logs to the project's log directory.
9
10Responsibilities
11----------------
12- Resolve the log directory via :func:`oct.core.option_c_dir.resolve_logs_dir`.
13- Create the directory if it does not exist.
14- Write JSON with a deterministic filename convention.
15
16Diagnostics
17-----------
18Domain: OCT-CORE
19Levels:
20 L3 — log write events
21
22Contracts
23---------
24- Always writes JSON (``indent=2``, UTF-8).
25- Filename: ``oct-{command}-{YYYYMMDD-HHMMSS}.json``.
26- Returns the written :class:`~pathlib.Path`.
27"""
28
29from __future__ import annotations
30
31import json
32from datetime import datetime
33from pathlib import Path
34
35from oct.core.option_c_dir import resolve_logs_dir
36
37
38def write_command_log(project_root: Path, command: str, data: dict) -> Path:
39 """Write *data* as JSON to the project log directory.
40
41 Returns the path of the written file.
42 """
43 log_dir = resolve_logs_dir(project_root)
44 log_dir.mkdir(parents=True, exist_ok=True)
45 stamp = datetime.now().strftime("%Y%m%d-%H%M%S")
46 log_path = log_dir / f"oct-{command}-{stamp}.json"
47 log_path.write_text(json.dumps(data, indent=2), encoding="utf-8")
48 return log_path
Path write_command_log(Path project_root, str command, dict data)
Definition log_writer.py:38