@rse/ase 0.0.7 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dst/ase-log.js ADDED
@@ -0,0 +1,69 @@
1
+ /*
2
+ ** Agentic Software Engineering (ASE)
3
+ ** Copyright (c) 2025-2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
4
+ ** Licensed under GPL 3.0 <https://spdx.org/licenses/GPL-3.0-only>
5
+ */
6
+ import fs from "node:fs";
7
+ import chalk from "chalk";
8
+ import { DateTime } from "luxon";
9
+ const levels = [
10
+ { name: "error", style: chalk.red.bold },
11
+ { name: "warning", style: chalk.yellow.bold },
12
+ { name: "info", style: chalk.blue },
13
+ { name: "debug", style: chalk.green }
14
+ ];
15
+ export default class Log {
16
+ _program;
17
+ _logLevel;
18
+ _logFile;
19
+ stream = null;
20
+ logLevelIdx = 0;
21
+ constructor(_program, _logLevel, _logFile) {
22
+ this._program = _program;
23
+ this._logLevel = _logLevel;
24
+ this._logFile = _logFile;
25
+ }
26
+ async init() {
27
+ /* log messages */
28
+ const idx = levels.findIndex((l) => l.name === this._logLevel);
29
+ if (idx === -1)
30
+ throw new RangeError(`invalid log level "${this._logLevel}" (expected one of: ${levels.map((l) => l.name).join(", ")})`);
31
+ this.logLevelIdx = idx;
32
+ if (this._logFile !== "-")
33
+ this.stream = fs.createWriteStream(this._logFile, { flags: "a", encoding: "utf8" });
34
+ }
35
+ logLevel(level) {
36
+ const idx = levels.findIndex((l) => l.name === level);
37
+ if (idx === -1)
38
+ throw new RangeError(`invalid log level "${level}" (expected one of: ${levels.map((l) => l.name).join(", ")})`);
39
+ this._logLevel = level;
40
+ this.logLevelIdx = idx;
41
+ }
42
+ logFile(file) {
43
+ if (file === this._logFile)
44
+ return;
45
+ this._logFile = file;
46
+ if (this.stream !== null) {
47
+ this.stream.end();
48
+ this.stream = null;
49
+ }
50
+ if (file !== "-")
51
+ this.stream = fs.createWriteStream(file, { flags: "a", encoding: "utf8" });
52
+ }
53
+ write(level, msg) {
54
+ const idx = levels.findIndex((l) => l.name === level);
55
+ if (idx !== -1 && idx <= this.logLevelIdx) {
56
+ const timestamp = DateTime.now().toFormat("yyyy-LL-dd HH:mm:ss.SSS");
57
+ let line = `${this._program}: [${timestamp}]: `;
58
+ if (this._logFile === "-" && process.stdout.isTTY)
59
+ line += `${levels[idx].style("[" + levels[idx].name.toUpperCase() + "]")}`;
60
+ else
61
+ line += `[${levels[idx].name.toUpperCase()}]`;
62
+ line += `: ${msg}\n`;
63
+ if (this._logFile === "-")
64
+ process.stdout.write(line);
65
+ else if (this.stream !== null)
66
+ this.stream.write(line);
67
+ }
68
+ }
69
+ }