oct
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 L2 - (none) the write is reported at L3
21 L3 - log write events
22 L4 - (none)
23
24Contracts
25---------
26- Always writes JSON (``indent=2``, UTF-8).
27- Filename: ``oct-{command}-{YYYYMMDD-HHMMSS}.json``.
28- Returns the written :class:`~pathlib.Path`.
29
30Dependencies
31------------
32- oct.core.option_c_dir (resolve_logs_dir log-directory resolver)
33- oct.core.diagnostics (_dbg structured logger)
34"""
35
36from __future__ import annotations
37
38import json
39from datetime import datetime
40from pathlib import Path
41
42from oct.core.option_c_dir import resolve_logs_dir
43from oct.core.diagnostics import _dbg
44
45
46def write_command_log(project_root: Path, command: str, data: dict) -> Path:
47 """Write *data* as JSON to the project log directory.
48
49 Returns the path of the written file.
50 """
51 func = "write_command_log"
52 _dbg("OCT-CORE", func, f"writing the {command} log under {project_root}", 3)
53 log_dir = resolve_logs_dir(project_root)
54 log_dir.mkdir(parents=True, exist_ok=True)
55 stamp = datetime.now().strftime("%Y%m%d-%H%M%S")
56 log_path = log_dir / f"oct-{command}-{stamp}.json"
57 log_path.write_text(json.dumps(data, indent=2), encoding="utf-8")
58 return log_path
Path write_command_log(Path project_root, str command, dict data)
Definition log_writer.py:46