@ckirg/corelib 0.1.22

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.
@@ -0,0 +1,102 @@
1
+ import {
2
+ getSysInfo
3
+ } from "./chunk-HPE2XSTW.js";
4
+
5
+ // src/loggers/implementations/cloudflare.ts
6
+ var LEVEL_MAP = {
7
+ trace: 10,
8
+ debug: 20,
9
+ info: 30,
10
+ warn: 40,
11
+ error: 50,
12
+ fatal: 60,
13
+ silent: Infinity
14
+ };
15
+ var CloudflareLogger = class _CloudflareLogger {
16
+ state;
17
+ context;
18
+ _level = process.env.LOG_LEVEL || "info";
19
+ constructor(state = { telemetryEnabled: false }, context = {}) {
20
+ this.state = state;
21
+ this.context = context;
22
+ }
23
+ getTelemetry() {
24
+ return this.state.telemetryEnabled ? getSysInfo() : void 0;
25
+ }
26
+ validate(msg, extras) {
27
+ if (typeof msg !== "string") {
28
+ throw new Error(
29
+ "Logger requires string message first, optional object second"
30
+ );
31
+ }
32
+ if (extras !== void 0 && (typeof extras !== "object" || extras === null || Array.isArray(extras))) {
33
+ throw new Error(
34
+ "Logger requires string message first, optional object second"
35
+ );
36
+ }
37
+ }
38
+ log(level, msg, extras) {
39
+ if ((LEVEL_MAP[level] ?? 30) < this.levelVal) return;
40
+ this.validate(msg, extras);
41
+ const output = {
42
+ level,
43
+ time: Date.now(),
44
+ ...this.context,
45
+ ...extras,
46
+ ...this.state.telemetryEnabled && { telemetry: this.getTelemetry() },
47
+ msg
48
+ };
49
+ console.log(JSON.stringify(output));
50
+ }
51
+ trace(msg, extras) {
52
+ this.log("trace", msg, extras);
53
+ }
54
+ debug(msg, extras) {
55
+ this.log("debug", msg, extras);
56
+ }
57
+ info(msg, extras) {
58
+ this.log("info", msg, extras);
59
+ }
60
+ warn(msg, extras) {
61
+ this.log("warn", msg, extras);
62
+ }
63
+ error(msg, extras) {
64
+ this.log("error", msg, extras);
65
+ }
66
+ fatal(msg, extras) {
67
+ this.log("fatal", msg, extras);
68
+ }
69
+ child(bindings) {
70
+ return new _CloudflareLogger(this.state, { ...this.context, ...bindings });
71
+ }
72
+ setTelemetry(mode) {
73
+ if (mode !== "on" && mode !== "off") {
74
+ throw new Error("setTelemetry accepts only 'on' or 'off'");
75
+ }
76
+ this.state.telemetryEnabled = mode === "on";
77
+ }
78
+ get level() {
79
+ return this._level;
80
+ }
81
+ set level(val) {
82
+ this._level = val;
83
+ }
84
+ get levelVal() {
85
+ return LEVEL_MAP[this._level] ?? 30;
86
+ }
87
+ bindings() {
88
+ return { ...this.context };
89
+ }
90
+ silent() {
91
+ this._level = "silent";
92
+ }
93
+ flush(cb) {
94
+ cb?.();
95
+ }
96
+ };
97
+ function createCloudflareLogger() {
98
+ return new CloudflareLogger();
99
+ }
100
+ export {
101
+ createCloudflareLogger as default
102
+ };
@@ -0,0 +1,41 @@
1
+ import {
2
+ StrictLoggerWrapper
3
+ } from "./chunk-HOOAMOFY.js";
4
+ import "./chunk-HPE2XSTW.js";
5
+
6
+ // src/loggers/implementations/deno.ts
7
+ import { Writable } from "stream";
8
+ import pino from "pino";
9
+ import pretty from "pino-pretty";
10
+ var isPretty = process.env.LOG_PRETTY === "true" || process.env.NODE_ENV !== "production" && process.env.LOG_PRETTY !== "false";
11
+ var level = process.env.LOG_LEVEL || "info";
12
+ var wsProxy = new Writable({
13
+ write(chunk, _encoding, cb) {
14
+ const fn = globalThis.__wsLogWrite;
15
+ if (fn) fn(typeof chunk === "string" ? chunk : chunk.toString("utf8"));
16
+ cb();
17
+ }
18
+ });
19
+ var dest = pino.multistream([
20
+ {
21
+ level,
22
+ stream: isPretty ? pretty({
23
+ colorize: true,
24
+ translateTime: "SYS:standard",
25
+ ignore: "pid,hostname"
26
+ }) : process.stdout
27
+ },
28
+ { level, stream: wsProxy }
29
+ ]);
30
+ var pinoInstance = pino(
31
+ {
32
+ level,
33
+ redact: ["password", "secret", "token", "authorization", "apiKey"]
34
+ },
35
+ dest
36
+ );
37
+ var logger = new StrictLoggerWrapper(pinoInstance);
38
+ var deno_default = logger;
39
+ export {
40
+ deno_default as default
41
+ };
@@ -0,0 +1,92 @@
1
+ declare global {
2
+ /**
3
+ * Global logger instance, if available.
4
+ */
5
+ var logger: StrictLogger | undefined;
6
+ }
7
+ /**
8
+ * Method signature for all log levels.
9
+ *
10
+ * @param {string} msg - The log message.
11
+ * @param {Record<string, unknown>} [extras] - Optional metadata to include with the log entry.
12
+ */
13
+ type LogMethod = (msg: string, extras?: Record<string, unknown>) => void;
14
+ /**
15
+ * StrictLogger Interface
16
+ * Defines a consistent API across all runtimes (Node, Bun, Deno, Cloudflare, etc.)
17
+ * This ensures that logging behavior is predictable and uniform regardless of the deployment target.
18
+ */
19
+ interface StrictLogger {
20
+ /** Logs at the 'trace' level (most verbose). */
21
+ trace: LogMethod;
22
+ /** Logs at the 'debug' level. */
23
+ debug: LogMethod;
24
+ /** Logs at the 'info' level (default). */
25
+ info: LogMethod;
26
+ /** Logs at the 'warn' level. */
27
+ warn: LogMethod;
28
+ /** Logs at the 'error' level. */
29
+ error: LogMethod;
30
+ /** Logs at the 'fatal' level (highest priority). */
31
+ fatal: LogMethod;
32
+ /**
33
+ * Creates a child logger with additional bindings.
34
+ * Child loggers inherit the parent's configuration but add their own metadata.
35
+ *
36
+ * @param {Record<string, unknown>} bindings - Metadata to bind to all logs from the child logger.
37
+ * @returns {StrictLogger} A new logger instance.
38
+ */
39
+ child: (bindings: Record<string, unknown>) => StrictLogger;
40
+ /**
41
+ * Enables or disables system telemetry injection in log entries.
42
+ *
43
+ * @param {"on" | "off"} mode - The telemetry mode.
44
+ */
45
+ setTelemetry: (mode: "on" | "off") => void;
46
+ /** Current log level as a string (e.g., 'info', 'debug'). */
47
+ level: string;
48
+ /** Current log level as a numeric value. */
49
+ levelVal: number;
50
+ /**
51
+ * Returns the current bindings of the logger.
52
+ * @returns {Record<string, unknown>} The current bindings.
53
+ */
54
+ bindings: () => Record<string, unknown>;
55
+ /**
56
+ * Sets the log level to 'silent', disabling all output.
57
+ */
58
+ silent: () => void;
59
+ /**
60
+ * Flush any buffered log output (e.g. pino's sonic-boom buffer).
61
+ * Safe to call as a no-op when no buffer is in use.
62
+ *
63
+ * @param {(err?: Error | null) => void} [cb] - Optional callback called after flushing.
64
+ */
65
+ flush: (cb?: (err?: Error | null) => void) => void;
66
+ }
67
+
68
+ /**
69
+ * Returns the next process-wide monotonic correlation id. Used to pair a
70
+ * flight-recorder operation's start line (e.g. `query: exec`, `poll: start`)
71
+ * with its terminus (`query: ok` / `poll: done` / `*: error`). Cheap (one int++),
72
+ * globally unique across all corelib packages so two interleaved operations
73
+ * never share an id. For cross-operation tracing, pair this with a higher-level
74
+ * `traceId` threaded by the consuming application.
75
+ */
76
+ declare function nextCid(): number;
77
+ /**
78
+ * Core, strictly-typed flight-recorder extras. The named fields are fixed so an
79
+ * AI can grep them reliably; arbitrary extra keys carry domain detail.
80
+ */
81
+ interface FlightRecorderExtras {
82
+ /** Correlation id pairing an operation's start with its terminus. */
83
+ cid?: number | string;
84
+ /** Wall-clock duration of the operation, in milliseconds (set on the terminus line). */
85
+ durationMs?: number;
86
+ /** What initiated the operation (cron name, route, event) — lets an AI trace up the stack. */
87
+ trigger?: string;
88
+ /** Domain-specific detail (symbol, account, counts, sample, errorMsg, …). */
89
+ [key: string]: unknown;
90
+ }
91
+
92
+ export { type FlightRecorderExtras as F, type LogMethod as L, type StrictLogger as S, nextCid as n };
@@ -0,0 +1,39 @@
1
+ import {
2
+ StrictLoggerWrapper
3
+ } from "./chunk-HOOAMOFY.js";
4
+ import "./chunk-HPE2XSTW.js";
5
+
6
+ // src/loggers/implementations/gcp.ts
7
+ import * as gcpConfig from "@google-cloud/pino-logging-gcp-config";
8
+ import pino from "pino";
9
+ function createGcpLogger() {
10
+ try {
11
+ const configFactory = gcpConfig.createGcpLoggingPinoConfig || gcpConfig.default?.createGcpLoggingPinoConfig || gcpConfig.default;
12
+ if (typeof configFactory !== "function") {
13
+ throw new Error(
14
+ "Could not find createGcpLoggingPinoConfig function in @google-cloud/pino-logging-gcp-config"
15
+ );
16
+ }
17
+ const config = configFactory(
18
+ {},
19
+ // GCP options
20
+ {
21
+ level: process.env.LOG_LEVEL || "info",
22
+ timestamp: pino.stdTimeFunctions.isoTime,
23
+ redact: ["password", "secret", "token", "authorization"]
24
+ }
25
+ );
26
+ const pinoInstance = pino(config);
27
+ return new StrictLoggerWrapper(pinoInstance);
28
+ } catch (err) {
29
+ console.error("[GCP-LOGGER] Failed to initialize GCP logger:", err);
30
+ const fallback = pino({
31
+ level: process.env.LOG_LEVEL || "info",
32
+ timestamp: pino.stdTimeFunctions.isoTime
33
+ });
34
+ return new StrictLoggerWrapper(fallback);
35
+ }
36
+ }
37
+ export {
38
+ createGcpLogger as default
39
+ };