@nyssa-ai/logging 0.0.0-stage → 0.2.2

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.
Files changed (78) hide show
  1. package/README.md +50 -2
  2. package/dist/LogLevel.d.ts +4 -0
  3. package/dist/LogLevel.d.ts.map +1 -0
  4. package/dist/LogLevel.js +50 -0
  5. package/dist/LogLevel.js.map +1 -0
  6. package/dist/Logger.d.ts +11 -0
  7. package/dist/Logger.d.ts.map +1 -0
  8. package/dist/Logger.js +12 -0
  9. package/dist/Logger.js.map +1 -0
  10. package/dist/LoggerProvider.d.ts +6 -0
  11. package/dist/LoggerProvider.d.ts.map +1 -0
  12. package/dist/LoggerProvider.js +7 -0
  13. package/dist/LoggerProvider.js.map +1 -0
  14. package/dist/configuration/CompositeLogger.d.ts +14 -0
  15. package/dist/configuration/CompositeLogger.d.ts.map +1 -0
  16. package/dist/configuration/CompositeLogger.js +51 -0
  17. package/dist/configuration/CompositeLogger.js.map +1 -0
  18. package/dist/configuration/LoggingBuilder.d.ts +23 -0
  19. package/dist/configuration/LoggingBuilder.d.ts.map +1 -0
  20. package/dist/configuration/LoggingBuilder.js +89 -0
  21. package/dist/configuration/LoggingBuilder.js.map +1 -0
  22. package/dist/configuration/createNyssaLogging.d.ts +5 -0
  23. package/dist/configuration/createNyssaLogging.d.ts.map +1 -0
  24. package/dist/configuration/createNyssaLogging.js +13 -0
  25. package/dist/configuration/createNyssaLogging.js.map +1 -0
  26. package/dist/consoleLogging/ConsoleOutputLogger.d.ts +16 -0
  27. package/dist/consoleLogging/ConsoleOutputLogger.d.ts.map +1 -0
  28. package/dist/consoleLogging/ConsoleOutputLogger.js +81 -0
  29. package/dist/consoleLogging/ConsoleOutputLogger.js.map +1 -0
  30. package/dist/consoleLogging/ConsoleOutputLoggerProvider.d.ts +9 -0
  31. package/dist/consoleLogging/ConsoleOutputLoggerProvider.d.ts.map +1 -0
  32. package/dist/consoleLogging/ConsoleOutputLoggerProvider.js +20 -0
  33. package/dist/consoleLogging/ConsoleOutputLoggerProvider.js.map +1 -0
  34. package/dist/consoleLogging/StderrOutputLogger.d.ts +16 -0
  35. package/dist/consoleLogging/StderrOutputLogger.d.ts.map +1 -0
  36. package/dist/consoleLogging/StderrOutputLogger.js +81 -0
  37. package/dist/consoleLogging/StderrOutputLogger.js.map +1 -0
  38. package/dist/consoleLogging/StderrOutputLoggerProvider.d.ts +9 -0
  39. package/dist/consoleLogging/StderrOutputLoggerProvider.d.ts.map +1 -0
  40. package/dist/consoleLogging/StderrOutputLoggerProvider.js +20 -0
  41. package/dist/consoleLogging/StderrOutputLoggerProvider.js.map +1 -0
  42. package/dist/fileLogging/Clock.d.ts +4 -0
  43. package/dist/fileLogging/Clock.d.ts.map +1 -0
  44. package/dist/fileLogging/Clock.js +6 -0
  45. package/dist/fileLogging/Clock.js.map +1 -0
  46. package/dist/fileLogging/JsonFileLogger.d.ts +21 -0
  47. package/dist/fileLogging/JsonFileLogger.d.ts.map +1 -0
  48. package/dist/fileLogging/JsonFileLogger.js +90 -0
  49. package/dist/fileLogging/JsonFileLogger.js.map +1 -0
  50. package/dist/fileLogging/JsonFileLoggerProvider.d.ts +15 -0
  51. package/dist/fileLogging/JsonFileLoggerProvider.d.ts.map +1 -0
  52. package/dist/fileLogging/JsonFileLoggerProvider.js +47 -0
  53. package/dist/fileLogging/JsonFileLoggerProvider.js.map +1 -0
  54. package/dist/fileLogging/LogFile.d.ts +24 -0
  55. package/dist/fileLogging/LogFile.d.ts.map +1 -0
  56. package/dist/fileLogging/LogFile.js +179 -0
  57. package/dist/fileLogging/LogFile.js.map +1 -0
  58. package/dist/fileLogging/LogRecord.d.ts +10 -0
  59. package/dist/fileLogging/LogRecord.d.ts.map +1 -0
  60. package/dist/fileLogging/LogRecord.js +12 -0
  61. package/dist/fileLogging/LogRecord.js.map +1 -0
  62. package/dist/fileLogging/LogRecordSerializer.d.ts +5 -0
  63. package/dist/fileLogging/LogRecordSerializer.d.ts.map +1 -0
  64. package/dist/fileLogging/LogRecordSerializer.js +52 -0
  65. package/dist/fileLogging/LogRecordSerializer.js.map +1 -0
  66. package/dist/fileLogging/OwnerOnlyAccess.d.ts +9 -0
  67. package/dist/fileLogging/OwnerOnlyAccess.d.ts.map +1 -0
  68. package/dist/fileLogging/OwnerOnlyAccess.js +78 -0
  69. package/dist/fileLogging/OwnerOnlyAccess.js.map +1 -0
  70. package/dist/fileLogging/StderrDiagnostics.d.ts +8 -0
  71. package/dist/fileLogging/StderrDiagnostics.d.ts.map +1 -0
  72. package/dist/fileLogging/StderrDiagnostics.js +31 -0
  73. package/dist/fileLogging/StderrDiagnostics.js.map +1 -0
  74. package/dist/fileLogging/SystemClock.d.ts +4 -0
  75. package/dist/fileLogging/SystemClock.d.ts.map +1 -0
  76. package/dist/fileLogging/SystemClock.js +10 -0
  77. package/dist/fileLogging/SystemClock.js.map +1 -0
  78. package/package.json +53 -4
@@ -0,0 +1,81 @@
1
+ // Responsibility: Output formatted operational log lines to standard error with sink isolation.
2
+ // Public interface:
3
+ // `-- StderrOutputLogger
4
+ // +-- critical(message, error, fields, operation)
5
+ // +-- debug(message, fields, operation)
6
+ // +-- error(message, error, fields, operation)
7
+ // +-- info(message, fields, operation)
8
+ // +-- isEnabled(level)
9
+ // +-- new(categoryName, minimumLevel)
10
+ // +-- trace(message, fields, operation)
11
+ // `-- warn(message, fields, operation)
12
+ import * as os from "node:os";
13
+ import { isLogLevelEnabled } from "../LogLevel.js";
14
+ const LEVEL_TAGS = {
15
+ Trace: "trce",
16
+ Debug: "dbug",
17
+ Information: "info",
18
+ Warning: "warn",
19
+ Error: "fail",
20
+ Critical: "crit",
21
+ None: "none"
22
+ };
23
+ export class StderrOutputLogger {
24
+ categoryName;
25
+ minimumLevel;
26
+ constructor(categoryName, minimumLevel) {
27
+ this.categoryName = categoryName;
28
+ this.minimumLevel = minimumLevel;
29
+ }
30
+ isEnabled(level) {
31
+ return isLogLevelEnabled(level, this.minimumLevel);
32
+ }
33
+ trace(message, fields, operation) {
34
+ this.write("Trace", message, fields, operation, null);
35
+ }
36
+ debug(message, fields, operation) {
37
+ this.write("Debug", message, fields, operation, null);
38
+ }
39
+ info(message, fields, operation) {
40
+ this.write("Information", message, fields, operation, null);
41
+ }
42
+ warn(message, fields, operation) {
43
+ this.write("Warning", message, fields, operation, null);
44
+ }
45
+ error(message, error, fields, operation) {
46
+ this.write("Error", message, fields, operation, error);
47
+ }
48
+ critical(message, error, fields, operation) {
49
+ this.write("Critical", message, fields, operation, error);
50
+ }
51
+ write(level, message, fields, _operation, error) {
52
+ if (!this.isEnabled(level)) {
53
+ return;
54
+ }
55
+ try {
56
+ const tag = LEVEL_TAGS[level] ?? "info";
57
+ const formatted = this.formatTemplate(message, fields);
58
+ let output = `${tag}: ${this.categoryName}${os.EOL} ${formatted}${os.EOL}`;
59
+ if (error !== undefined && error !== null) {
60
+ const errorText = error instanceof Error ? error.stack ?? error.message : String(error);
61
+ output += ` ${errorText}${os.EOL}`;
62
+ }
63
+ process.stderr.write(output);
64
+ }
65
+ catch {
66
+ // TS-LOG: Sink failures are contained and do not fail the business operation.
67
+ }
68
+ }
69
+ formatTemplate(template, fields) {
70
+ if (!fields || Object.keys(fields).length === 0) {
71
+ return template;
72
+ }
73
+ return template.replace(/\{(\w+)\}/g, (match, key) => {
74
+ if (key in fields && fields[key] !== undefined) {
75
+ return String(fields[key]);
76
+ }
77
+ return match;
78
+ });
79
+ }
80
+ }
81
+ //# sourceMappingURL=StderrOutputLogger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StderrOutputLogger.js","sourceRoot":"","sources":["../../src/consoleLogging/StderrOutputLogger.ts"],"names":[],"mappings":"AAAA,gGAAgG;AAChG,oBAAoB;AACpB,yBAAyB;AACzB,sDAAsD;AACtD,4CAA4C;AAC5C,mDAAmD;AACnD,2CAA2C;AAC3C,2BAA2B;AAC3B,0CAA0C;AAC1C,4CAA4C;AAC5C,2CAA2C;AAE3C,OAAO,KAAK,EAA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AE3C,IAAI,CAAC,MAAM,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAChD,OAAO,QAAQ,CAAC;QAClB,CAAC;QACD,OAAO,QAAQ,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC,KAAK,EAAE,GAAW,EAAE,EAAE;YAC3D,IAAI,GAAG,IAAI,MAAM,IAAI,MAAM,CAAC,GAAG,CAAC,KAAK,SAAS,EAAE,CAAC;gBAC/C,OAAO,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;YAC7B,CAAC;YACD,OAAO,KAAK,CAAC;QACf,CAAC,CAAC,CAAC;IACL,CAAC;CACF"}
@@ -0,0 +1,9 @@
1
+ import type { Logger } from "../Logger.js";
2
+ import type { LogLevel } from "../LogLevel.js";
3
+ export declare class StderrOutputLoggerProvider {
4
+ private readonly minimumLevel;
5
+ constructor(minimumLevel?: LogLevel);
6
+ createLogger(categoryName: string): Logger;
7
+ dispose(): void;
8
+ }
9
+ //# sourceMappingURL=StderrOutputLoggerProvider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StderrOutputLoggerProvider.d.ts","sourceRoot":"","sources":["../../src/consoleLogging/StderrOutputLoggerProvider.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAG/C,qBAAa,0BAA0B;IACrC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAW;gBAErB,YAAY,CAAC,EAAE,QAAQ;IAInC,YAAY,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM;IAI1C,OAAO,IAAI,IAAI;CAGvB"}
@@ -0,0 +1,20 @@
1
+ // Responsibility: Provider for category-scoped standard error loggers.
2
+ // Public interface:
3
+ // `-- StderrOutputLoggerProvider
4
+ // +-- createLogger(categoryName)
5
+ // +-- dispose()
6
+ // `-- new(minimumLevel)
7
+ import { StderrOutputLogger } from "./StderrOutputLogger.js";
8
+ export class StderrOutputLoggerProvider {
9
+ minimumLevel;
10
+ constructor(minimumLevel) {
11
+ this.minimumLevel = minimumLevel ?? "Information";
12
+ }
13
+ createLogger(categoryName) {
14
+ return new StderrOutputLogger(categoryName, this.minimumLevel);
15
+ }
16
+ dispose() {
17
+ // Stderr requires no resource teardown.
18
+ }
19
+ }
20
+ //# sourceMappingURL=StderrOutputLoggerProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StderrOutputLoggerProvider.js","sourceRoot":"","sources":["../../src/consoleLogging/StderrOutputLoggerProvider.ts"],"names":[],"mappings":"AAAA,uEAAuE;AACvE,oBAAoB;AACpB,iCAAiC;AACjC,qCAAqC;AACrC,oBAAoB;AACpB,4BAA4B;AAI5B,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAE7D,MAAM,OAAO,0BAA0B;IACpB,YAAY,CAAW;IAExC,YAAmB,YAAuB;QACxC,IAAI,CAAC,YAAY,GAAG,YAAY,IAAI,aAAa,CAAC;IACpD,CAAC;IAEM,YAAY,CAAC,YAAoB;QACtC,OAAO,IAAI,kBAAkB,CAAC,YAAY,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC;IACjE,CAAC;IAEM,OAAO;QACZ,wCAAwC;IAC1C,CAAC;CACF"}
@@ -0,0 +1,4 @@
1
+ export interface Clock {
2
+ now(): Date;
3
+ }
4
+ //# sourceMappingURL=Clock.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Clock.d.ts","sourceRoot":"","sources":["../../src/fileLogging/Clock.ts"],"names":[],"mappings":"AAKA,MAAM,WAAW,KAAK;IACpB,GAAG,IAAI,IAAI,CAAC;CACb"}
@@ -0,0 +1,6 @@
1
+ // Responsibility: Time abstraction interface for log timestamps and retention calculations.
2
+ // Public interface:
3
+ // `-- Clock
4
+ // `-- now()
5
+ export {};
6
+ //# sourceMappingURL=Clock.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Clock.js","sourceRoot":"","sources":["../../src/fileLogging/Clock.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,oBAAoB;AACpB,YAAY;AACZ,gBAAgB"}
@@ -0,0 +1,21 @@
1
+ import { type LogLevel } from "../LogLevel.js";
2
+ import type { Clock } from "./Clock.js";
3
+ import type { LogFile } from "./LogFile.js";
4
+ export declare class JsonFileLogger {
5
+ private readonly categoryName;
6
+ private readonly logFile;
7
+ private readonly clock;
8
+ private readonly minimumLevel;
9
+ constructor(categoryName: string, logFile: LogFile, clock: Clock, minimumLevel: LogLevel);
10
+ isEnabled(level: LogLevel): boolean;
11
+ trace(message: string, fields?: Record<string, unknown>, operation?: string): void;
12
+ debug(message: string, fields?: Record<string, unknown>, operation?: string): void;
13
+ info(message: string, fields?: Record<string, unknown>, operation?: string): void;
14
+ warn(message: string, fields?: Record<string, unknown>, operation?: string): void;
15
+ error(message: string, error?: unknown, fields?: Record<string, unknown>, operation?: string): void;
16
+ critical(message: string, error?: unknown, fields?: Record<string, unknown>, operation?: string): void;
17
+ private write;
18
+ private formatTemplate;
19
+ private formatError;
20
+ }
21
+ //# sourceMappingURL=JsonFileLogger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"JsonFileLogger.d.ts","sourceRoot":"","sources":["../../src/fileLogging/JsonFileLogger.ts"],"names":[],"mappings":"AAYA,OAAO,EAAqB,KAAK,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAI5C,qBAAa,cAAc;IACzB,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAS;IACtC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAU;IAClC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAQ;IAC9B,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAW;gBAGtC,YAAY,EAAE,MAAM,EACpB,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,KAAK,EACZ,YAAY,EAAE,QAAQ;IAQjB,SAAS,CAAC,KAAK,EAAE,QAAQ,GAAG,OAAO;IAInC,KAAK,CACV,OAAO,EAAE,MAAM,EACf,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChC,SAAS,CAAC,EAAE,MAAM,GACjB,IAAI;IAIA,KAAK,CACV,OAAO,EAAE,MAAM,EACf,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChC,SAAS,CAAC,EAAE,MAAM,GACjB,IAAI;IAIA,IAAI,CACT,OAAO,EAAE,MAAM,EACf,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChC,SAAS,CAAC,EAAE,MAAM,GACjB,IAAI;IAIA,IAAI,CACT,OAAO,EAAE,MAAM,EACf,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChC,SAAS,CAAC,EAAE,MAAM,GACjB,IAAI;IAIA,KAAK,CACV,OAAO,EAAE,MAAM,EACf,KAAK,CAAC,EAAE,OAAO,EACf,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChC,SAAS,CAAC,EAAE,MAAM,GACjB,IAAI;IAIA,QAAQ,CACb,OAAO,EAAE,MAAM,EACf,KAAK,CAAC,EAAE,OAAO,EACf,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChC,SAAS,CAAC,EAAE,MAAM,GACjB,IAAI;IAIP,OAAO,CAAC,KAAK;IAgCb,OAAO,CAAC,cAAc;IAetB,OAAO,CAAC,WAAW;CASpB"}
@@ -0,0 +1,90 @@
1
+ // Responsibility: Category-scoped logger writing structured JSON log records to a LogFile.
2
+ // Public interface:
3
+ // `-- JsonFileLogger
4
+ // +-- critical(message, error, fields, operation)
5
+ // +-- debug(message, fields, operation)
6
+ // +-- error(message, error, fields, operation)
7
+ // +-- info(message, fields, operation)
8
+ // +-- isEnabled(level)
9
+ // +-- new(categoryName, logFile, clock, minimumLevel)
10
+ // +-- trace(message, fields, operation)
11
+ // `-- warn(message, fields, operation)
12
+ import { isLogLevelEnabled } from "../LogLevel.js";
13
+ import { StderrDiagnostics } from "./StderrDiagnostics.js";
14
+ export class JsonFileLogger {
15
+ categoryName;
16
+ logFile;
17
+ clock;
18
+ minimumLevel;
19
+ constructor(categoryName, logFile, clock, minimumLevel) {
20
+ this.categoryName = categoryName;
21
+ this.logFile = logFile;
22
+ this.clock = clock;
23
+ this.minimumLevel = minimumLevel;
24
+ }
25
+ isEnabled(level) {
26
+ return isLogLevelEnabled(level, this.minimumLevel);
27
+ }
28
+ trace(message, fields, operation) {
29
+ this.write("Trace", message, fields, operation, null);
30
+ }
31
+ debug(message, fields, operation) {
32
+ this.write("Debug", message, fields, operation, null);
33
+ }
34
+ info(message, fields, operation) {
35
+ this.write("Information", message, fields, operation, null);
36
+ }
37
+ warn(message, fields, operation) {
38
+ this.write("Warning", message, fields, operation, null);
39
+ }
40
+ error(message, error, fields, operation) {
41
+ this.write("Error", message, fields, operation, error);
42
+ }
43
+ critical(message, error, fields, operation) {
44
+ this.write("Critical", message, fields, operation, error);
45
+ }
46
+ write(level, message, fields, operation, error) {
47
+ if (!this.isEnabled(level)) {
48
+ return;
49
+ }
50
+ try {
51
+ const formattedMessage = this.formatTemplate(message, fields);
52
+ const exception = this.formatError(error);
53
+ const record = {
54
+ Timestamp: this.clock.now().toISOString(),
55
+ Level: level,
56
+ Message: formattedMessage,
57
+ Module: this.categoryName,
58
+ Operation: operation ?? "",
59
+ Exception: exception,
60
+ AdditionalFields: fields ?? {}
61
+ };
62
+ this.logFile.writeRecord(record);
63
+ }
64
+ catch (writeError) {
65
+ const msg = writeError instanceof Error ? writeError.message : String(writeError);
66
+ StderrDiagnostics.error(this.categoryName, `failed to log to file: ${msg}`);
67
+ }
68
+ }
69
+ formatTemplate(template, fields) {
70
+ if (!fields || Object.keys(fields).length === 0) {
71
+ return template;
72
+ }
73
+ return template.replace(/\{(\w+)\}/g, (match, key) => {
74
+ if (key in fields && fields[key] !== undefined) {
75
+ return String(fields[key]);
76
+ }
77
+ return match;
78
+ });
79
+ }
80
+ formatError(error) {
81
+ if (error === undefined || error === null) {
82
+ return null;
83
+ }
84
+ if (error instanceof Error) {
85
+ return error.stack ?? error.message;
86
+ }
87
+ return String(error);
88
+ }
89
+ }
90
+ //# sourceMappingURL=JsonFileLogger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"JsonFileLogger.js","sourceRoot":"","sources":["../../src/fileLogging/JsonFileLogger.ts"],"names":[],"mappings":"AAAA,2FAA2F;AAC3F,oBAAoB;AACpB,qBAAqB;AACrB,sDAAsD;AACtD,4CAA4C;AAC5C,mDAAmD;AACnD,2CAA2C;AAC3C,2BAA2B;AAC3B,0DAA0D;AAC1D,4CAA4C;AAC5C,2CAA2C;AAE3C,OAAO,EAAE,iBAAiB,EAAiB,MAAM,gBAAgB,CAAC;AAIlE,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAE3D,MAAM,OAAO,cAAc;IACR,YAAY,CAAS;IACrB,OAAO,CAAU;IACjB,KAAK,CAAQ;IACb,YAAY,CAAW;IAExC,YACE,YAAoB,EACpB,OAAgB,EAChB,KAAY,EACZ,YAAsB;QAEtB,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;IACnC,CAAC;IAEM,SAAS,CAAC,KAAe;QAC9B,OAAO,iBAAiB,CAAC,KAAK,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC;IACrD,CAAC;IAEM,KAAK,CACV,OAAe,EACf,MAAgC,EACh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E,CAAC;YAC1C,OAAO,IAAI,CAAC;QACd,CAAC;QACD,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;YAC3B,OAAO,KAAK,CAAC,KAAK,IAAI,KAAK,CAAC,OAAO,CAAC;QACtC,CAAC;QACD,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC;CACF"}
@@ -0,0 +1,15 @@
1
+ import type { Logger } from "../Logger.js";
2
+ import type { LogLevel } from "../LogLevel.js";
3
+ import type { Clock } from "./Clock.js";
4
+ import { LogFile } from "./LogFile.js";
5
+ export declare class JsonFileLoggerProvider {
6
+ private readonly logFile;
7
+ private readonly clock;
8
+ private readonly minimumLevel;
9
+ private disposed;
10
+ constructor(logFile: LogFile, clock: Clock, minimumLevel: LogLevel);
11
+ static tryCreate(filePath: string, clock?: Clock, minimumLevel?: LogLevel): JsonFileLoggerProvider | null;
12
+ createLogger(categoryName: string): Logger;
13
+ dispose(): void;
14
+ }
15
+ //# sourceMappingURL=JsonFileLoggerProvider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"JsonFileLoggerProvider.d.ts","sourceRoot":"","sources":["../../src/fileLogging/JsonFileLoggerProvider.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAGxC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAGvC,qBAAa,sBAAsB;IACjC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAU;IAClC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAQ;IAC9B,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAW;IACxC,OAAO,CAAC,QAAQ,CAAS;gBAGvB,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,KAAK,EACZ,YAAY,EAAE,QAAQ;WAOV,SAAS,CACrB,QAAQ,EAAE,MAAM,EAChB,KAAK,CAAC,EAAE,KAAK,EACb,YAAY,CAAC,EAAE,QAAQ,GACtB,sBAAsB,GAAG,IAAI;IAqBzB,YAAY,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM;IAS1C,OAAO,IAAI,IAAI;CAOvB"}
@@ -0,0 +1,47 @@
1
+ // Responsibility: Factory and lifecycle manager for rotating JSON file logger instances.
2
+ // Public interface:
3
+ // `-- JsonFileLoggerProvider
4
+ // +-- createLogger(categoryName)
5
+ // +-- dispose()
6
+ // +-- new(logFile, clock, minimumLevel)
7
+ // `-- tryCreate(filePath, clock, minimumLevel)
8
+ import { SystemClock } from "./SystemClock.js";
9
+ import { JsonFileLogger } from "./JsonFileLogger.js";
10
+ import { LogFile } from "./LogFile.js";
11
+ import { StderrDiagnostics } from "./StderrDiagnostics.js";
12
+ export class JsonFileLoggerProvider {
13
+ logFile;
14
+ clock;
15
+ minimumLevel;
16
+ disposed = false;
17
+ constructor(logFile, clock, minimumLevel) {
18
+ this.logFile = logFile;
19
+ this.clock = clock;
20
+ this.minimumLevel = minimumLevel;
21
+ }
22
+ static tryCreate(filePath, clock, minimumLevel) {
23
+ if (!filePath || filePath.trim().length === 0) {
24
+ return null;
25
+ }
26
+ const effectiveClock = clock ?? new SystemClock();
27
+ const logFile = new LogFile(filePath, undefined, effectiveClock);
28
+ const error = logFile.initialize();
29
+ if (error !== null) {
30
+ logFile.dispose();
31
+ StderrDiagnostics.error(filePath, error);
32
+ return null;
33
+ }
34
+ return new JsonFileLoggerProvider(logFile, effectiveClock, minimumLevel ?? "Trace");
35
+ }
36
+ createLogger(categoryName) {
37
+ return new JsonFileLogger(categoryName, this.logFile, this.clock, this.minimumLevel);
38
+ }
39
+ dispose() {
40
+ if (this.disposed) {
41
+ return;
42
+ }
43
+ this.disposed = true;
44
+ this.logFile.dispose();
45
+ }
46
+ }
47
+ //# sourceMappingURL=JsonFileLoggerProvider.js.map
@@ -0,0 +1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CAAC;YACzC,OAAO,IAAI,CAAC;QACd,CAAC;QAED,OAAO,IAAI,sBAAsB,CAC/B,OAAO,EACP,cAAc,EACd,YAAY,IAAI,OAAO,CACxB,CAAC;IACJ,CAAC;IAEM,YAAY,CAAC,YAAoB;QACtC,OAAO,IAAI,cAAc,CACvB,YAAY,EACZ,IAAI,CAAC,OAAO,EACZ,IAAI,CAAC,KAAK,EACV,IAAI,CAAC,YAAY,CAClB,CAAC;IACJ,CAAC;IAEM,OAAO;QACZ,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAClB,OAAO;QACT,CAAC;QACD,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;QACrB,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;IACzB,CAAC;CACF"}
@@ -0,0 +1,24 @@
1
+ import type { Clock } from "./Clock.js";
2
+ import type { LogRecord } from "./LogRecord.js";
3
+ export declare class LogFile {
4
+ static readonly ROTATION_TRIGGER_MB = 10;
5
+ static readonly BACKUP_RETENTION = 5;
6
+ static readonly RETENTION_DAYS = 30;
7
+ private readonly logPath;
8
+ private readonly rotationTriggerBytes;
9
+ private readonly clock;
10
+ private fd;
11
+ private bytesWritten;
12
+ private rotationRetryAtBytes;
13
+ private reopenRetryAtMs;
14
+ private disposed;
15
+ constructor(logPath: string, rotationTriggerBytes?: number, clock?: Clock);
16
+ initialize(): string | null;
17
+ writeRecord(record: LogRecord): void;
18
+ dispose(): void;
19
+ private openAppend;
20
+ private rotate;
21
+ private deleteExpiredBackups;
22
+ private tryReopenAfterFailedRotation;
23
+ }
24
+ //# sourceMappingURL=LogFile.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LogFile.d.ts","sourceRoot":"","sources":["../../src/fileLogging/LogFile.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAExC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAQhD,qBAAa,OAAO;IAClB,gBAAuB,mBAAmB,MAAM;IAChD,gBAAuB,gBAAgB,KAAK;IAC5C,gBAAuB,cAAc,MAAM;IAE3C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAAS;IAC9C,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAQ;IAC9B,OAAO,CAAC,EAAE,CAAuB;IACjC,OAAO,CAAC,YAAY,CAAK;IACzB,OAAO,CAAC,oBAAoB,CAAK;IACjC,OAAO,CAAC,eAAe,CAAuB;IAC9C,OAAO,CAAC,QAAQ,CAAS;gBAGvB,OAAO,EAAE,MAAM,EACf,oBAAoB,CAAC,EAAE,MAAM,EAC7B,KAAK,CAAC,EAAE,KAAK;IAQR,UAAU,IAAI,MAAM,GAAG,IAAI;IAsB3B,WAAW,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI;IAwCpC,OAAO,IAAI,IAAI;IAetB,OAAO,CAAC,UAAU;IAIlB,OAAO,CAAC,MAAM;IA4Cd,OAAO,CAAC,oBAAoB;IA4B5B,OAAO,CAAC,4BAA4B;CAerC"}
@@ -0,0 +1,179 @@
1
+ // Responsibility: Manage active log file, synchronous appending, size rotation, and retention pruning.
2
+ // Public interface:
3
+ // `-- LogFile
4
+ // +-- BACKUP_RETENTION
5
+ // +-- RETENTION_DAYS
6
+ // +-- ROTATION_TRIGGER_MB
7
+ // +-- dispose()
8
+ // +-- initialize()
9
+ // +-- new(logPath, rotationTriggerBytes, clock)
10
+ // `-- writeRecord(record)
11
+ import * as fs from "node:fs";
12
+ import * as path from "node:path";
13
+ import { SystemClock } from "./SystemClock.js";
14
+ import { LogRecordSerializer } from "./LogRecordSerializer.js";
15
+ import { OwnerOnlyAccess } from "./OwnerOnlyAccess.js";
16
+ import { StderrDiagnostics } from "./StderrDiagnostics.js";
17
+ const BYTES_PER_MEGABYTE = 1024 * 1024;
18
+ const REOPEN_RETRY_DELAY_MS = 1000;
19
+ export class LogFile {
20
+ static ROTATION_TRIGGER_MB = 10;
21
+ static BACKUP_RETENTION = 5;
22
+ static RETENTION_DAYS = 30;
23
+ logPath;
24
+ rotationTriggerBytes;
25
+ clock;
26
+ fd = null;
27
+ bytesWritten = 0;
28
+ rotationRetryAtBytes = 0;
29
+ reopenRetryAtMs = null;
30
+ disposed = false;
31
+ constructor(logPath, rotationTriggerBytes, clock) {
32
+ this.logPath = logPath;
33
+ this.rotationTriggerBytes =
34
+ rotationTriggerBytes ?? LogFile.ROTATION_TRIGGER_MB * BYTES_PER_MEGABYTE;
35
+ this.clock = clock ?? new SystemClock();
36
+ }
37
+ initialize() {
38
+ try {
39
+ const directoryPath = path.dirname(this.logPath);
40
+ if (!directoryPath || directoryPath === ".") {
41
+ return `Invalid log path: ${this.logPath}`;
42
+ }
43
+ OwnerOnlyAccess.createDirectory(directoryPath);
44
+ this.fd = this.openAppend(this.logPath);
45
+ this.bytesWritten = fs.fstatSync(this.fd).size;
46
+ OwnerOnlyAccess.applyToFile(this.logPath);
47
+ this.deleteExpiredBackups();
48
+ return null;
49
+ }
50
+ catch (error) {
51
+ const message = error instanceof Error ? error.message : String(error);
52
+ return `Failed to initialize log file: ${message}`;
53
+ }
54
+ }
55
+ writeRecord(record) {
56
+ if (this.disposed) {
57
+ return;
58
+ }
59
+ try {
60
+ if (this.fd === null) {
61
+ const nowMs = this.clock.now().getTime();
62
+ if (this.reopenRetryAtMs !== null && nowMs < this.reopenRetryAtMs) {
63
+ return;
64
+ }
65
+ this.tryReopenAfterFailedRotation();
66
+ if (this.fd === null) {
67
+ return;
68
+ }
69
+ }
70
+ if (this.bytesWritten > this.rotationTriggerBytes &&
71
+ this.bytesWritten >= this.rotationRetryAtBytes) {
72
+ this.rotate();
73
+ }
74
+ if (this.fd === null) {
75
+ return;
76
+ }
77
+ const line = LogRecordSerializer.serialize(record);
78
+ fs.writeSync(this.fd, line + "\n");
79
+ fs.fsyncSync(this.fd);
80
+ this.bytesWritten = fs.fstatSync(this.fd).size;
81
+ }
82
+ catch (error) {
83
+ const message = error instanceof Error ? error.message : String(error);
84
+ StderrDiagnostics.error(this.logPath, `failed to write log record: ${message}`);
85
+ }
86
+ }
87
+ dispose() {
88
+ if (this.disposed) {
89
+ return;
90
+ }
91
+ this.disposed = true;
92
+ if (this.fd !== null) {
93
+ try {
94
+ fs.closeSync(this.fd);
95
+ }
96
+ catch {
97
+ // Disposed best-effort
98
+ }
99
+ this.fd = null;
100
+ }
101
+ }
102
+ openAppend(targetPath) {
103
+ return fs.openSync(targetPath, "a", OwnerOnlyAccess.OWNER_READ_WRITE);
104
+ }
105
+ rotate() {
106
+ try {
107
+ if (this.fd !== null) {
108
+ fs.closeSync(this.fd);
109
+ this.fd = null;
110
+ }
111
+ for (let index = LogFile.BACKUP_RETENTION - 1; index >= 1; index--) {
112
+ const olderPath = `${this.logPath}.${index}`;
113
+ const newerPath = `${this.logPath}.${index + 1}`;
114
+ if (!fs.existsSync(olderPath)) {
115
+ continue;
116
+ }
117
+ if (fs.existsSync(newerPath)) {
118
+ fs.unlinkSync(newerPath);
119
+ }
120
+ fs.renameSync(olderPath, newerPath);
121
+ }
122
+ const archivePath = `${this.logPath}.1`;
123
+ if (fs.existsSync(archivePath)) {
124
+ fs.unlinkSync(archivePath);
125
+ }
126
+ if (fs.existsSync(this.logPath)) {
127
+ fs.renameSync(this.logPath, archivePath);
128
+ }
129
+ this.deleteExpiredBackups();
130
+ this.fd = this.openAppend(this.logPath);
131
+ OwnerOnlyAccess.applyToFile(this.logPath);
132
+ this.bytesWritten = 0;
133
+ this.rotationRetryAtBytes = 0;
134
+ }
135
+ catch (error) {
136
+ const message = error instanceof Error ? error.message : String(error);
137
+ StderrDiagnostics.error(this.logPath, `failed to rotate log file: ${message}`);
138
+ this.tryReopenAfterFailedRotation();
139
+ }
140
+ }
141
+ deleteExpiredBackups() {
142
+ const directory = path.dirname(this.logPath);
143
+ if (!directory || !fs.existsSync(directory)) {
144
+ return;
145
+ }
146
+ const baseName = path.basename(this.logPath);
147
+ const cutoffMs = this.clock.now().getTime() - LogFile.RETENTION_DAYS * 24 * 60 * 60 * 1000;
148
+ for (let index = 1; index <= LogFile.BACKUP_RETENTION; index++) {
149
+ const backupPath = path.join(directory, `${baseName}.${index}`);
150
+ if (!fs.existsSync(backupPath)) {
151
+ continue;
152
+ }
153
+ try {
154
+ const stat = fs.statSync(backupPath);
155
+ if (stat.mtimeMs < cutoffMs) {
156
+ fs.unlinkSync(backupPath);
157
+ }
158
+ }
159
+ catch (error) {
160
+ const message = error instanceof Error ? error.message : String(error);
161
+ StderrDiagnostics.warning(backupPath, `failed to prune expired backup: ${message}`);
162
+ }
163
+ }
164
+ }
165
+ tryReopenAfterFailedRotation() {
166
+ try {
167
+ this.fd = this.openAppend(this.logPath);
168
+ this.bytesWritten = fs.fstatSync(this.fd).size;
169
+ this.rotationRetryAtBytes = this.bytesWritten + BYTES_PER_MEGABYTE;
170
+ this.reopenRetryAtMs = null;
171
+ }
172
+ catch (error) {
173
+ this.reopenRetryAtMs = this.clock.now().getTime() + REOPEN_RETRY_DELAY_MS;
174
+ const message = error instanceof Error ? error.message : String(error);
175
+ StderrDiagnostics.error(this.logPath, `failed to reopen log file after failed rotation: ${message}`);
176
+ }
177
+ }
178
+ }
179
+ //# sourceMappingURL=LogFile.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LogFile.js","sourceRoot":"","sources":["../../src/fileLogging/LogFile.ts"],"names":[],"mappings":"AAAA,uGAAuG;AACvG,oBAAoB;AACpB,cAAc;AACd,2BAA2B;AAC3B,yBAAyB;AACzB,8BAA8B;AAC9B,oBAAoB;AACpB,uBAAuB;AACvB,oDAAoD;AACpD,8BAA8B;AAE9B,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAElC,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CA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BACf,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;gBACvE,iBAAiB,CAAC,OAAO,CAAC,UAAU,EAAE,mCAAmC,OAAO,EAAE,CAAC,CAAC;YACtF,CAAC;QACH,CAAC;IACH,CAAC;IAEO,4BAA4B;QAClC,IAAI,CAAC;YACH,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YACxC,IAAI,CAAC,YAAY,GAAG,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC;YAC/C,IAAI,CAAC,oBAAoB,GAAG,IAAI,CAAC,YAAY,GAAG,kBAAkB,CAAC;YACnE,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC;QAC9B,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,GAAG,qBAAqB,CAAC;YAC1E,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YACvE,iBAAiB,CAAC,KAAK,CACrB,IAAI,CAAC,OAAO,EACZ,oDAAoD,OAAO,EAAE,CAC9D,CAAC;QACJ,CAAC;IACH,CAAC"}
@@ -0,0 +1,10 @@
1
+ export interface LogRecord {
2
+ readonly Timestamp: string;
3
+ readonly Level: string;
4
+ readonly Message: string;
5
+ readonly Module: string;
6
+ readonly Operation: string;
7
+ readonly Exception: string | null;
8
+ readonly AdditionalFields: Readonly<Record<string, unknown>>;
9
+ }
10
+ //# sourceMappingURL=LogRecord.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LogRecord.d.ts","sourceRoot":"","sources":["../../src/fileLogging/LogRecord.ts"],"names":[],"mappings":"AAWA,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IAClC,QAAQ,CAAC,gBAAgB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;CAC9D"}
@@ -0,0 +1,12 @@
1
+ // Responsibility: Structured log record matching the canonical Nyssa JSON format.
2
+ // Public interface:
3
+ // `-- LogRecord
4
+ // +-- AdditionalFields
5
+ // +-- Exception
6
+ // +-- Level
7
+ // +-- Message
8
+ // +-- Module
9
+ // +-- Operation
10
+ // `-- Timestamp
11
+ export {};
12
+ //# sourceMappingURL=LogRecord.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LogRecord.js","sourceRoot":"","sources":["../../src/fileLogging/LogRecord.ts"],"names":[],"mappings":"AAAA,kFAAkF;AAClF,oBAAoB;AACpB,gBAAgB;AAChB,2BAA2B;AAC3B,oBAAoB;AACpB,gBAAgB;AAChB,kBAAkB;AAClB,iBAAiB;AACjB,oBAAoB;AACpB,oBAAoB"}
@@ -0,0 +1,5 @@
1
+ import type { LogRecord } from "./LogRecord.js";
2
+ export declare class LogRecordSerializer {
3
+ static serialize(record: LogRecord): string;
4
+ }
5
+ //# sourceMappingURL=LogRecordSerializer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LogRecordSerializer.d.ts","sourceRoot":"","sources":["../../src/fileLogging/LogRecordSerializer.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AA+BhD,qBAAa,mBAAmB;WAChB,SAAS,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM;CAkBnD"}