@sockethub/logger 1.0.0-alpha.11 → 1.0.0-alpha.13

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,129 @@
1
+ import winston from "winston";
2
+ export type Logger = winston.Logger;
3
+ export interface LoggerOptions {
4
+ level?: string;
5
+ fileLevel?: string;
6
+ file?: string;
7
+ }
8
+ /**
9
+ * Initialize the logger system with global configuration.
10
+ *
11
+ * Called once at server bootstrap with config settings. All subsequent
12
+ * createLogger() calls will use these settings as defaults unless explicitly
13
+ * overridden.
14
+ *
15
+ * @param options - Global logger configuration
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * // In server bootstrap
20
+ * initLogger({
21
+ * level: config.get("logging:level"),
22
+ * fileLevel: config.get("logging:fileLevel"),
23
+ * file: config.get("logging:file"),
24
+ * });
25
+ * ```
26
+ */
27
+ export declare function initLogger(options: LoggerOptions): void;
28
+ /**
29
+ * Set the logger context for this process.
30
+ *
31
+ * All subsequent createLogger() calls will prepend this context to their namespace.
32
+ * This is typically set once at process startup to identify the process (e.g., "sockethub"
33
+ * for the main server, or "sockethub:platform:irc:abc123" for a platform child process).
34
+ *
35
+ * @param context - The context string to prepend to all logger namespaces in this process
36
+ *
37
+ * @example
38
+ * ```typescript
39
+ * // In main server process
40
+ * setLoggerContext('sockethub');
41
+ * const log = createLogger('server:listener');
42
+ * // Output namespace: "sockethub:server:listener"
43
+ *
44
+ * // In platform child process
45
+ * setLoggerContext('sockethub:platform:irc:abc123');
46
+ * const log = createLogger('main');
47
+ * // Output namespace: "sockethub:platform:irc:abc123:main"
48
+ * ```
49
+ */
50
+ export declare function setLoggerContext(context: string): void;
51
+ /**
52
+ * Get the current logger context for this process.
53
+ *
54
+ * @returns The current logger context string, or empty string if not set
55
+ *
56
+ * @example
57
+ * ```typescript
58
+ * const context = getLoggerContext();
59
+ * if (context.includes(':platform:')) {
60
+ * // We're in a platform child process
61
+ * }
62
+ * ```
63
+ */
64
+ export declare function getLoggerContext(): string;
65
+ /**
66
+ * Reset the logger context.
67
+ *
68
+ * Primarily used for testing to reset state between test cases.
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * afterEach(() => {
73
+ * resetLoggerContext();
74
+ * });
75
+ * ```
76
+ */
77
+ export declare function resetLoggerContext(): void;
78
+ /**
79
+ * Creates a Winston logger instance with console and optional file transports.
80
+ *
81
+ * Configuration priority (highest to lowest):
82
+ * 1. Explicit options parameter
83
+ * 2. Global config (set via initLogger)
84
+ * 3. Environment variables (LOG_LEVEL, LOG_FILE_LEVEL, LOG_FILE)
85
+ * 4. Defaults (info for console, debug for file)
86
+ *
87
+ * Log levels: error, warn, info, debug
88
+ *
89
+ * NODE_ENV=production disables console timestamps (for systemd)
90
+ *
91
+ * @param namespace - Logger namespace (e.g., "sockethub:data-layer:queue:...")
92
+ * @param options - Optional logger configuration overrides
93
+ * @returns Winston logger instance
94
+ *
95
+ * @example
96
+ * ```typescript
97
+ * // Uses global config if initLogger() was called
98
+ * const log = createLogger("sockethub:data-layer:queue:irc-123");
99
+ *
100
+ * // Override for specific use case (e.g., early bootstrap logging)
101
+ * const earlyLog = createLogger("sockethub:bootstrap", { level: "info" });
102
+ * ```
103
+ */
104
+ export declare function createLogger(namespace: string, options?: LoggerOptions): Logger;
105
+ /**
106
+ * Get the namespace from a logger instance.
107
+ *
108
+ * Extracts the full namespace (including context) that was set when the logger was created.
109
+ * Useful for reusing the namespace in other systems like Redis connection names.
110
+ *
111
+ * @param logger - Logger instance created by createLogger()
112
+ * @returns The full namespace string, or empty string if not found
113
+ *
114
+ * @example
115
+ * ```typescript
116
+ * const log = createLogger('data-layer:queue');
117
+ * const namespace = getLoggerNamespace(log);
118
+ * // Returns: "sockethub:data-layer:queue" (if context is "sockethub")
119
+ *
120
+ * // Use for Redis connection name
121
+ * redisConfig.connectionName = namespace;
122
+ * ```
123
+ */
124
+ export declare function getLoggerNamespace(logger: Logger): string;
125
+ /**
126
+ * Reset logger state. Used primarily for testing.
127
+ * @internal
128
+ */
129
+ export declare function resetLoggerForTesting(): void;
package/dist/index.js CHANGED
@@ -4,15 +4,29 @@ var __getProtoOf = Object.getPrototypeOf;
4
4
  var __defProp = Object.defineProperty;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ function __accessProp(key) {
8
+ return this[key];
9
+ }
10
+ var __toESMCache_node;
11
+ var __toESMCache_esm;
7
12
  var __toESM = (mod, isNodeMode, target) => {
13
+ var canCache = mod != null && typeof mod === "object";
14
+ if (canCache) {
15
+ var cache = isNodeMode ? __toESMCache_node ??= new WeakMap : __toESMCache_esm ??= new WeakMap;
16
+ var cached = cache.get(mod);
17
+ if (cached)
18
+ return cached;
19
+ }
8
20
  target = mod != null ? __create(__getProtoOf(mod)) : {};
9
21
  const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
10
22
  for (let key of __getOwnPropNames(mod))
11
23
  if (!__hasOwnProp.call(to, key))
12
24
  __defProp(to, key, {
13
- get: () => mod[key],
25
+ get: __accessProp.bind(mod, key),
14
26
  enumerable: true
15
27
  });
28
+ if (canCache)
29
+ cache.set(mod, to);
16
30
  return to;
17
31
  };
18
32
  var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
@@ -9881,6 +9895,36 @@ var globalConfig = null;
9881
9895
  var hasLoggedInit = false;
9882
9896
  var loggerContext = "";
9883
9897
  var loggerNamespaceStore = new WeakMap;
9898
+ function safeStringify(value) {
9899
+ try {
9900
+ const parents = [];
9901
+ return JSON.stringify(value, function(_key, innerValue) {
9902
+ if (innerValue instanceof Error) {
9903
+ return {
9904
+ name: innerValue.name,
9905
+ message: innerValue.message,
9906
+ stack: innerValue.stack
9907
+ };
9908
+ }
9909
+ if (typeof innerValue === "bigint") {
9910
+ return innerValue.toString();
9911
+ }
9912
+ if (typeof innerValue === "object" && innerValue !== null) {
9913
+ const parent = this;
9914
+ while (parents.length > 0 && parents[parents.length - 1] !== parent) {
9915
+ parents.pop();
9916
+ }
9917
+ if (parents.includes(innerValue)) {
9918
+ return "[Circular]";
9919
+ }
9920
+ parents.push(innerValue);
9921
+ }
9922
+ return innerValue;
9923
+ });
9924
+ } catch {
9925
+ return '"[Unserializable]"';
9926
+ }
9927
+ }
9884
9928
  function initLogger(options) {
9885
9929
  globalConfig = options;
9886
9930
  if (!hasLoggedInit) {
@@ -9913,11 +9957,11 @@ function createLogger(namespace, options = {}) {
9913
9957
  const isProduction = false;
9914
9958
  const consoleFormat = isProduction ? import_winston.default.format.combine(import_winston.default.format.colorize(), import_winston.default.format.printf(({ level, message, namespace: namespace2, ...meta }) => {
9915
9959
  const ns = namespace2 ? `${namespace2} ` : "";
9916
- const metaStr = Object.keys(meta).length > 0 ? ` ${JSON.stringify(meta)}` : "";
9960
+ const metaStr = Object.keys(meta).length > 0 ? ` ${safeStringify(meta)}` : "";
9917
9961
  return `${level}: ${ns}${message}${metaStr}`;
9918
9962
  })) : import_winston.default.format.combine(import_winston.default.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), import_winston.default.format.colorize(), import_winston.default.format.printf(({ level, message, timestamp, namespace: namespace2, ...meta }) => {
9919
9963
  const ns = namespace2 ? `${namespace2} ` : "";
9920
- const metaStr = Object.keys(meta).length > 0 ? ` ${JSON.stringify(meta)}` : "";
9964
+ const metaStr = Object.keys(meta).length > 0 ? ` ${safeStringify(meta)}` : "";
9921
9965
  return `${timestamp} ${level}: ${ns}${message}${metaStr}`;
9922
9966
  }));
9923
9967
  const transports = [
@@ -9960,4 +10004,4 @@ export {
9960
10004
  createLogger
9961
10005
  };
9962
10006
 
9963
- //# debugId=D26653BFE0342E6864756E2164756E21
10007
+ //# debugId=E499E63CEE36E4A864756E2164756E21