@dxos/log 0.8.4-main.67995b8 → 0.8.4-main.69d29f4

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 (63) hide show
  1. package/dist/lib/browser/chunk-GPOFUMLO.mjs +133 -0
  2. package/dist/lib/browser/chunk-GPOFUMLO.mjs.map +7 -0
  3. package/dist/lib/browser/chunk-IEP6GGEX.mjs +23 -0
  4. package/dist/lib/browser/chunk-IEP6GGEX.mjs.map +7 -0
  5. package/dist/lib/browser/index.mjs +136 -177
  6. package/dist/lib/browser/index.mjs.map +4 -4
  7. package/dist/lib/browser/meta.json +1 -1
  8. package/dist/lib/browser/platform/browser/index.mjs +26 -0
  9. package/dist/lib/browser/platform/browser/index.mjs.map +7 -0
  10. package/dist/lib/browser/platform/node/index.mjs +21 -0
  11. package/dist/lib/browser/platform/node/index.mjs.map +7 -0
  12. package/dist/lib/browser/processors/console-processor.mjs +107 -0
  13. package/dist/lib/browser/processors/console-processor.mjs.map +7 -0
  14. package/dist/lib/browser/processors/console-stub.mjs +9 -0
  15. package/dist/lib/browser/processors/console-stub.mjs.map +7 -0
  16. package/dist/lib/node-esm/chunk-2SZHAWBN.mjs +24 -0
  17. package/dist/lib/node-esm/chunk-2SZHAWBN.mjs.map +7 -0
  18. package/dist/lib/node-esm/chunk-QPYJZ4SO.mjs +135 -0
  19. package/dist/lib/node-esm/chunk-QPYJZ4SO.mjs.map +7 -0
  20. package/dist/lib/node-esm/index.mjs +133 -259
  21. package/dist/lib/node-esm/index.mjs.map +4 -4
  22. package/dist/lib/node-esm/meta.json +1 -1
  23. package/dist/lib/node-esm/platform/browser/index.mjs +27 -0
  24. package/dist/lib/node-esm/platform/browser/index.mjs.map +7 -0
  25. package/dist/lib/node-esm/platform/node/index.mjs +22 -0
  26. package/dist/lib/node-esm/platform/node/index.mjs.map +7 -0
  27. package/dist/lib/node-esm/processors/console-processor.mjs +108 -0
  28. package/dist/lib/node-esm/processors/console-processor.mjs.map +7 -0
  29. package/dist/lib/node-esm/processors/console-stub.mjs +10 -0
  30. package/dist/lib/node-esm/processors/console-stub.mjs.map +7 -0
  31. package/dist/types/src/config.d.ts +2 -3
  32. package/dist/types/src/config.d.ts.map +1 -1
  33. package/dist/types/src/context.d.ts.map +1 -1
  34. package/dist/types/src/decorators.d.ts +1 -1
  35. package/dist/types/src/decorators.d.ts.map +1 -1
  36. package/dist/types/src/log.d.ts +12 -18
  37. package/dist/types/src/log.d.ts.map +1 -1
  38. package/dist/types/src/options.d.ts +1 -6
  39. package/dist/types/src/options.d.ts.map +1 -1
  40. package/dist/types/src/platform/index.d.ts +1 -1
  41. package/dist/types/src/platform/index.d.ts.map +1 -1
  42. package/dist/types/src/platform/node/index.d.ts.map +1 -1
  43. package/dist/types/src/processors/browser-processor.d.ts.map +1 -1
  44. package/dist/types/src/processors/console-processor.d.ts.map +1 -1
  45. package/dist/types/src/processors/file-processor.d.ts.map +1 -1
  46. package/dist/types/src/processors/index.d.ts +3 -3
  47. package/dist/types/src/processors/index.d.ts.map +1 -1
  48. package/dist/types/tsconfig.tsbuildinfo +1 -1
  49. package/package.json +31 -11
  50. package/src/config.ts +3 -2
  51. package/src/context.ts +36 -5
  52. package/src/decorators.ts +5 -4
  53. package/src/experimental/classes.test.ts +2 -1
  54. package/src/log.test.ts +49 -18
  55. package/src/log.ts +101 -57
  56. package/src/options.ts +27 -11
  57. package/src/platform/index.ts +1 -1
  58. package/src/platform/node/index.ts +2 -1
  59. package/src/processors/browser-processor.ts +3 -1
  60. package/src/processors/console-processor.ts +9 -5
  61. package/src/processors/file-processor.ts +4 -1
  62. package/src/processors/index.ts +3 -3
  63. package/src/scope.ts +1 -1
@@ -0,0 +1,133 @@
1
+ // src/config.ts
2
+ var LogLevel = /* @__PURE__ */ (function(LogLevel2) {
3
+ LogLevel2[LogLevel2["TRACE"] = 5] = "TRACE";
4
+ LogLevel2[LogLevel2["DEBUG"] = 10] = "DEBUG";
5
+ LogLevel2[LogLevel2["VERBOSE"] = 11] = "VERBOSE";
6
+ LogLevel2[LogLevel2["INFO"] = 12] = "INFO";
7
+ LogLevel2[LogLevel2["WARN"] = 13] = "WARN";
8
+ LogLevel2[LogLevel2["ERROR"] = 14] = "ERROR";
9
+ return LogLevel2;
10
+ })({});
11
+ var levels = {
12
+ "*": 5,
13
+ trace: 5,
14
+ debug: 10,
15
+ verbose: 11,
16
+ info: 12,
17
+ warn: 13,
18
+ error: 14
19
+ };
20
+ var shortLevelName = {
21
+ [5]: "T",
22
+ [10]: "D",
23
+ [11]: "V",
24
+ [12]: "I",
25
+ [13]: "W",
26
+ [14]: "E"
27
+ };
28
+ var LogProcessorType = /* @__PURE__ */ (function(LogProcessorType2) {
29
+ LogProcessorType2["CONSOLE"] = "console";
30
+ LogProcessorType2["BROWSER"] = "browser";
31
+ LogProcessorType2["DEBUG"] = "debug";
32
+ return LogProcessorType2;
33
+ })({});
34
+
35
+ // src/scope.ts
36
+ var logInfoProperties = Symbol("logInfoProperties");
37
+ var logInfo = (target, propertyKey, descriptor) => {
38
+ (target[logInfoProperties] ??= []).push(propertyKey);
39
+ };
40
+ var gatherLogInfoFromScope = (scope) => {
41
+ if (!scope) {
42
+ return {};
43
+ }
44
+ const res = {};
45
+ const prototype = Object.getPrototypeOf(scope);
46
+ const infoProps = (typeof prototype === "object" && prototype !== null ? prototype[logInfoProperties] : []) ?? [];
47
+ for (const prop of infoProps) {
48
+ try {
49
+ res[prop] = typeof scope[prop] === "function" ? scope[prop]() : scope[prop];
50
+ } catch (err) {
51
+ res[prop] = err.message;
52
+ }
53
+ }
54
+ return res;
55
+ };
56
+
57
+ // src/context.ts
58
+ var matchFilter = (filter, level, path) => {
59
+ if (filter.pattern?.startsWith("-")) {
60
+ if (path?.includes(filter.pattern.slice(1))) {
61
+ if (level >= filter.level) {
62
+ return false;
63
+ }
64
+ }
65
+ } else {
66
+ if (filter.pattern?.length) {
67
+ if (path?.includes(filter.pattern)) {
68
+ return level >= filter.level;
69
+ }
70
+ } else {
71
+ if (level >= filter.level) {
72
+ return true;
73
+ }
74
+ }
75
+ }
76
+ };
77
+ var shouldLog = (entry, filters) => {
78
+ if (filters === void 0) {
79
+ return false;
80
+ }
81
+ const results = filters.map((filter) => matchFilter(filter, entry.level, entry.meta?.F)).filter((result) => result !== void 0);
82
+ return results.length > 0 && !results.some((results2) => results2 === false);
83
+ };
84
+ var getContextFromEntry = (entry) => {
85
+ let context;
86
+ if (entry.meta) {
87
+ const scopeInfo = gatherLogInfoFromScope(entry.meta.S);
88
+ if (Object.keys(scopeInfo).length > 0) {
89
+ context = Object.assign(context ?? {}, scopeInfo);
90
+ }
91
+ }
92
+ const entryContext = typeof entry.context === "function" ? entry.context() : entry.context;
93
+ if (entryContext) {
94
+ if (entryContext instanceof Error) {
95
+ const c = entryContext.context;
96
+ context = Object.assign(context ?? {}, {
97
+ error: entryContext.stack,
98
+ ...c
99
+ });
100
+ } else if (typeof entryContext === "object") {
101
+ context = Object.assign(context ?? {}, entryContext);
102
+ }
103
+ }
104
+ if (entry.error) {
105
+ context = Object.assign(context ?? {}, {
106
+ error: entry.error
107
+ });
108
+ }
109
+ return context && Object.keys(context).length > 0 ? context : void 0;
110
+ };
111
+
112
+ // src/processors/common.ts
113
+ var getRelativeFilename = (filename) => {
114
+ const match = filename.match(/.+\/(packages\/.+\/.+)/);
115
+ if (match) {
116
+ const [, filePath] = match;
117
+ return filePath;
118
+ }
119
+ return filename;
120
+ };
121
+
122
+ export {
123
+ LogLevel,
124
+ levels,
125
+ shortLevelName,
126
+ LogProcessorType,
127
+ logInfo,
128
+ gatherLogInfoFromScope,
129
+ shouldLog,
130
+ getContextFromEntry,
131
+ getRelativeFilename
132
+ };
133
+ //# sourceMappingURL=chunk-GPOFUMLO.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/config.ts", "../../../src/scope.ts", "../../../src/context.ts", "../../../src/processors/common.ts"],
4
+ "sourcesContent": ["//\n// Copyright 2022 DXOS.org\n//\n\nimport { type LogProcessor } from './context';\n\n/**\n * Standard levels.\n * NOTE: Keep aligned with LogLevel in @dxos/protocols.\n */\n// TODO(burdon): Update numbers?\nexport enum LogLevel {\n TRACE = 5,\n DEBUG = 10,\n VERBOSE = 11,\n INFO = 12,\n WARN = 13,\n ERROR = 14,\n}\n\nexport const levels: Record<string, LogLevel> = {\n '*': LogLevel.TRACE,\n trace: LogLevel.TRACE,\n debug: LogLevel.DEBUG,\n verbose: LogLevel.VERBOSE,\n info: LogLevel.INFO,\n warn: LogLevel.WARN,\n error: LogLevel.ERROR,\n};\n\nexport const shortLevelName = {\n [LogLevel.TRACE]: 'T',\n [LogLevel.DEBUG]: 'D',\n [LogLevel.VERBOSE]: 'V',\n [LogLevel.INFO]: 'I',\n [LogLevel.WARN]: 'W',\n [LogLevel.ERROR]: 'E',\n};\n\nexport enum LogProcessorType {\n CONSOLE = 'console',\n BROWSER = 'browser',\n DEBUG = 'debug',\n}\n\n/**\n * Individual filter condition.\n */\nexport type LogFilter = {\n level: LogLevel;\n pattern?: string;\n};\n\n/**\n * Options to set inline or load from the YML file.\n */\nexport type LogOptions = {\n file?: string;\n filter?: string | string[] | LogLevel;\n captureFilter?: string | string[] | LogLevel;\n depth?: number; // Context object depth.\n processor?: string | LogProcessorType;\n formatter?: {\n column: number;\n timestamp: boolean;\n timestampFirst: boolean;\n };\n prefix?: string;\n};\n\n/**\n * Runtime config.\n */\nexport interface LogConfig {\n options: LogOptions;\n filters?: LogFilter[];\n captureFilters?: LogFilter[];\n processors: LogProcessor[];\n prefix?: string;\n}\n", "//\n// Copyright 2022 DXOS.org\n//\n\nconst logInfoProperties = Symbol('logInfoProperties');\n\n/**\n * Decorate fields, properties, or methods to automatically include their values in log messages.\n *\n * Example:\n *\n * ```typescript\n * class Example {\n * @logInfo\n * peerId: PublicKey;\n * }\n * ```\n */\nexport const logInfo = (target: any, propertyKey: string, descriptor?: PropertyDescriptor) => {\n // console.log(target, propertyKey, descriptor);\n (target[logInfoProperties] ??= []).push(propertyKey);\n};\n\n/**\n * Introspects class instance to find decorated metadata.\n * @param scope Class instance.\n */\nexport const gatherLogInfoFromScope = (scope: any): Record<string, any> => {\n if (!scope) {\n return {};\n }\n\n const res: Record<string, any> = {};\n\n const prototype = Object.getPrototypeOf(scope);\n const infoProps = (typeof prototype === 'object' && prototype !== null ? prototype[logInfoProperties] : []) ?? [];\n for (const prop of infoProps) {\n try {\n res[prop] = typeof scope[prop] === 'function' ? scope[prop]() : scope[prop];\n } catch (err: any) {\n res[prop] = err.message;\n }\n }\n\n return res;\n};\n", "//\n// Copyright 2022 DXOS.org\n//\n\nimport { type LogConfig, type LogFilter, type LogLevel } from './config';\nimport { type CallMetadata } from './meta';\nimport { gatherLogInfoFromScope } from './scope';\n\n/**\n * Optional object passed to the logging API.\n */\nexport type LogContext = Record<string, any> | Error | any;\n\n/**\n * Record for current log line.\n */\nexport interface LogEntry {\n level: LogLevel;\n message?: string;\n context?: LogContext;\n meta?: CallMetadata;\n error?: Error;\n}\n\n/**\n * Processes (e.g., prints, forwards) log entries.\n */\nexport type LogProcessor = (config: LogConfig, entry: LogEntry) => void;\n\n/**\n * Returns:\n * true if the log entry matches the filter,\n * false if should be excluded, or\n * undefined if it the filter doesn't match the level.\n */\nconst matchFilter = (filter: LogFilter, level: LogLevel, path?: string): boolean | undefined => {\n // TODO(burdon): Support regexp.\n if (filter.pattern?.startsWith('-')) {\n // Exclude.\n if (path?.includes(filter.pattern.slice(1))) {\n if (level >= filter.level) {\n return false;\n }\n }\n } else {\n // Include.\n if (filter.pattern?.length) {\n if (path?.includes(filter.pattern)) {\n return level >= filter.level;\n }\n } else {\n if (level >= filter.level) {\n return true;\n }\n }\n }\n};\n\n/**\n * Determines if the current line should be logged (called by the processor).\n */\nexport const shouldLog = (entry: LogEntry, filters?: LogFilter[]): boolean => {\n if (filters === undefined) {\n return false;\n }\n\n const results = filters\n .map((filter) => matchFilter(filter, entry.level, entry.meta?.F))\n .filter((result): result is boolean => result !== undefined);\n\n // Skip if any are explicitely false.\n // console.log({ level: entry.level, path: entry.meta?.F }, filters, results, results.length);\n return results.length > 0 && !results.some((results) => results === false);\n};\n\nexport const getContextFromEntry = (entry: LogEntry): Record<string, any> | undefined => {\n let context;\n if (entry.meta) {\n const scopeInfo = gatherLogInfoFromScope(entry.meta.S);\n if (Object.keys(scopeInfo).length > 0) {\n context = Object.assign(context ?? {}, scopeInfo);\n }\n }\n\n const entryContext = typeof entry.context === 'function' ? entry.context() : entry.context;\n if (entryContext) {\n if (entryContext instanceof Error) {\n // Additional context from Error.\n const c = (entryContext as any).context;\n // If ERROR then show stacktrace.\n context = Object.assign(context ?? {}, { error: entryContext.stack, ...c });\n } else if (typeof entryContext === 'object') {\n context = Object.assign(context ?? {}, entryContext);\n }\n }\n\n if (entry.error) {\n context = Object.assign(context ?? {}, { error: entry.error });\n }\n\n return context && Object.keys(context).length > 0 ? context : undefined;\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\nexport const getRelativeFilename = (filename: string) => {\n // TODO(burdon): Hack uses \"packages\" as an anchor (pre-parse NX?)\n // Including `packages/` part of the path so that excluded paths (e.g. from dist) are clickable in vscode.\n const match = filename.match(/.+\\/(packages\\/.+\\/.+)/);\n if (match) {\n const [, filePath] = match;\n return filePath;\n }\n\n return filename;\n};\n"],
5
+ "mappings": "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",
6
+ "names": ["LogLevel", "levels", "trace", "debug", "verbose", "info", "warn", "error", "shortLevelName", "LogProcessorType", "logInfoProperties", "Symbol", "logInfo", "target", "propertyKey", "descriptor", "push", "gatherLogInfoFromScope", "scope", "res", "prototype", "Object", "getPrototypeOf", "infoProps", "prop", "err", "message", "matchFilter", "filter", "level", "path", "pattern", "startsWith", "includes", "slice", "length", "shouldLog", "entry", "filters", "undefined", "results", "map", "meta", "F", "result", "some", "getContextFromEntry", "context", "scopeInfo", "gatherLogInfoFromScope", "S", "Object", "keys", "assign", "entryContext", "Error", "c", "error", "stack", "getRelativeFilename", "filename", "match", "filePath"]
7
+ }
@@ -0,0 +1,23 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
18
+
19
+ export {
20
+ __export,
21
+ __reExport
22
+ };
23
+ //# sourceMappingURL=chunk-IEP6GGEX.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [],
5
+ "mappings": "",
6
+ "names": []
7
+ }
@@ -1,104 +1,49 @@
1
+ import {
2
+ LogLevel,
3
+ LogProcessorType,
4
+ gatherLogInfoFromScope,
5
+ getContextFromEntry,
6
+ getRelativeFilename,
7
+ levels,
8
+ logInfo,
9
+ shortLevelName,
10
+ shouldLog
11
+ } from "./chunk-GPOFUMLO.mjs";
12
+ import {
13
+ __export,
14
+ __reExport
15
+ } from "./chunk-IEP6GGEX.mjs";
16
+
1
17
  // src/index.ts
18
+ var index_exports = {};
19
+ __export(index_exports, {
20
+ BROWSER_PROCESSOR: () => BROWSER_PROCESSOR,
21
+ DEBUG_PROCESSOR: () => DEBUG_PROCESSOR,
22
+ FILE_PROCESSOR: () => FILE_PROCESSOR,
23
+ LogLevel: () => LogLevel,
24
+ LogProcessorType: () => LogProcessorType,
25
+ createFileProcessor: () => createFileProcessor,
26
+ createLog: () => createLog,
27
+ debug: () => debug,
28
+ gatherLogInfoFromScope: () => gatherLogInfoFromScope,
29
+ getContextFromEntry: () => getContextFromEntry,
30
+ getCurrentOwnershipScope: () => getCurrentOwnershipScope,
31
+ getRelativeFilename: () => getRelativeFilename,
32
+ levels: () => levels,
33
+ log: () => log,
34
+ logInfo: () => logInfo,
35
+ omit: () => omit,
36
+ parseFilter: () => parseFilter,
37
+ pick: () => pick,
38
+ shortLevelName: () => shortLevelName,
39
+ shouldLog: () => shouldLog
40
+ });
2
41
  import omit from "lodash.omit";
3
42
  import { pick } from "@dxos/util";
4
43
 
5
- // src/config.ts
6
- var LogLevel = /* @__PURE__ */ function(LogLevel2) {
7
- LogLevel2[LogLevel2["TRACE"] = 5] = "TRACE";
8
- LogLevel2[LogLevel2["DEBUG"] = 10] = "DEBUG";
9
- LogLevel2[LogLevel2["VERBOSE"] = 11] = "VERBOSE";
10
- LogLevel2[LogLevel2["INFO"] = 12] = "INFO";
11
- LogLevel2[LogLevel2["WARN"] = 13] = "WARN";
12
- LogLevel2[LogLevel2["ERROR"] = 14] = "ERROR";
13
- return LogLevel2;
14
- }({});
15
- var levels = {
16
- trace: 5,
17
- debug: 10,
18
- verbose: 11,
19
- info: 12,
20
- warn: 13,
21
- error: 14
22
- };
23
- var shortLevelName = {
24
- [5]: "T",
25
- [10]: "D",
26
- [11]: "V",
27
- [12]: "I",
28
- [13]: "W",
29
- [14]: "E"
30
- };
31
- var LogProcessorType = /* @__PURE__ */ function(LogProcessorType2) {
32
- LogProcessorType2["CONSOLE"] = "console";
33
- LogProcessorType2["BROWSER"] = "browser";
34
- LogProcessorType2["DEBUG"] = "debug";
35
- return LogProcessorType2;
36
- }({});
37
-
38
- // src/scope.ts
39
- var logInfoProperties = Symbol("logInfoProperties");
40
- var logInfo = (target, propertyKey, descriptor) => {
41
- (target[logInfoProperties] ??= []).push(propertyKey);
42
- };
43
- var gatherLogInfoFromScope = (scope) => {
44
- if (!scope) {
45
- return {};
46
- }
47
- const res = {};
48
- const prototype = Object.getPrototypeOf(scope);
49
- const infoProps = prototype[logInfoProperties] ?? [];
50
- for (const prop of infoProps) {
51
- try {
52
- res[prop] = typeof scope[prop] === "function" ? scope[prop]() : scope[prop];
53
- } catch (err) {
54
- res[prop] = err.message;
55
- }
56
- }
57
- return res;
58
- };
59
-
60
- // src/context.ts
61
- var matchFilter = (filter, level, path) => {
62
- return level >= filter.level && (!filter.pattern || path.includes(filter.pattern));
63
- };
64
- var shouldLog = (entry, filters) => {
65
- if (filters === void 0) {
66
- return true;
67
- } else {
68
- return filters.some((filter) => matchFilter(filter, entry.level, entry.meta?.F ?? ""));
69
- }
70
- };
71
- var getContextFromEntry = (entry) => {
72
- let context;
73
- if (entry.meta) {
74
- const scopeInfo = gatherLogInfoFromScope(entry.meta.S);
75
- if (Object.keys(scopeInfo).length > 0) {
76
- context = Object.assign(context ?? {}, scopeInfo);
77
- }
78
- }
79
- const entryContext = typeof entry.context === "function" ? entry.context() : entry.context;
80
- if (entryContext) {
81
- if (entryContext instanceof Error) {
82
- const c = entryContext.context;
83
- context = Object.assign(context ?? {}, {
84
- error: entryContext.stack,
85
- ...c
86
- });
87
- } else if (typeof entryContext === "object") {
88
- context = Object.assign(context ?? {}, entryContext);
89
- }
90
- }
91
- if (entry.error) {
92
- context = Object.assign(context ?? {}, {
93
- error: entry.error
94
- });
95
- }
96
- return context && Object.keys(context).length > 0 ? context : void 0;
97
- };
98
-
99
44
  // src/decorators.ts
100
- import chalk from "chalk";
101
45
  import { inspect } from "@dxos/node-std/util";
46
+ import chalk from "chalk";
102
47
  var nextPromiseId = 0;
103
48
  var createMethodLogDecorator = (log2) => (arg0, arg1, meta) => (target, propertyKey, descriptor) => {
104
49
  const method = descriptor.value;
@@ -195,53 +140,37 @@ var logAsyncResolved = (log2, methodName, resolvedValue, promiseId, startTime, c
195
140
  var logAsyncRejected = (log2, methodName, err, promiseId, startTime, combinedMeta) => {
196
141
  log2.info(`.${formatFunction(methodName)} \u21B2 \u{1F525} ${chalk.gray("reject")} ${formatPromise(promiseId)} ${formatTimeElapsed(startTime)} ${chalk.gray("=>")} ${err}`, {}, combinedMeta);
197
142
  };
198
- var greenCheck = typeof chalk.green === "function" ? chalk.green("\u2714") : "\u2714";
199
- var formatTimeElapsed = (startTime) => chalk.gray(`${(performance.now() - startTime).toFixed(0)}ms`);
200
143
  var COLOR_FUNCTION = [
201
144
  220,
202
145
  220,
203
146
  170
204
147
  ];
148
+ var greenCheck = typeof chalk.green === "function" ? chalk.green("\u2714") : "\u2714";
149
+ var formatTimeElapsed = (startTime) => chalk.gray(`${(performance.now() - startTime).toFixed(0)}ms`);
205
150
  var formatFunction = (name) => chalk.bold(chalk.rgb(...COLOR_FUNCTION)(name));
206
151
  var formatPromise = (id) => chalk.blue(`Promise#${id}`);
207
152
 
208
153
  // src/options.ts
209
154
  import defaultsDeep from "lodash.defaultsdeep";
210
155
 
211
- // src/platform/browser/index.ts
212
- var loadOptions = (filepath) => {
213
- try {
214
- let dxlog;
215
- if (typeof localStorage === "undefined") {
216
- if (globalThis.localStorage_dxlog) {
217
- dxlog = globalThis.localStorage_dxlog;
218
- }
219
- } else {
220
- dxlog = localStorage.getItem("dxlog") ?? void 0;
221
- }
222
- if (!dxlog) {
223
- return void 0;
224
- }
225
- return JSON.parse(dxlog);
226
- } catch (err) {
227
- console.info("can't parse dxlog config", err);
228
- return void 0;
229
- }
230
- };
231
-
232
- // src/processors/console-stub.ts
233
- var CONSOLE_PROCESSOR = () => {
234
- };
156
+ // src/platform/index.ts
157
+ var platform_exports = {};
158
+ __reExport(platform_exports, platform_star);
159
+ import * as platform_star from "#platform";
235
160
 
236
- // src/processors/debug-processor.ts
237
- import { inspect as inspect2 } from "@dxos/node-std/util";
238
- var DEBUG_PROCESSOR = (config, entry) => {
239
- console.log(inspect2(entry, false, null, true));
240
- };
161
+ // src/processors/index.ts
162
+ var processors_exports = {};
163
+ __export(processors_exports, {
164
+ BROWSER_PROCESSOR: () => BROWSER_PROCESSOR,
165
+ DEBUG_PROCESSOR: () => DEBUG_PROCESSOR,
166
+ FILE_PROCESSOR: () => FILE_PROCESSOR,
167
+ createFileProcessor: () => createFileProcessor,
168
+ getRelativeFilename: () => getRelativeFilename
169
+ });
241
170
 
242
171
  // src/processors/browser-processor.ts
243
172
  import { getDebugName, safariCheck } from "@dxos/util";
244
- var getRelativeFilename = (filename) => {
173
+ var getRelativeFilename2 = (filename) => {
245
174
  const match = filename.match(/.+\/(packages\/.+\/.+)/);
246
175
  if (match) {
247
176
  const [, filePath] = match;
@@ -261,7 +190,7 @@ var APP_BROWSER_PROCESSOR = (config, entry) => {
261
190
  const LOG_BROWSER_CSS = [];
262
191
  let link = "";
263
192
  if (entry.meta) {
264
- const filename = getRelativeFilename(entry.meta.F);
193
+ const filename = getRelativeFilename2(entry.meta.F);
265
194
  const filepath = `${LOG_BROWSER_PREFIX.replace(/\/$/, "")}/${filename}`;
266
195
  link = `${filepath}#L${entry.meta.L}`;
267
196
  }
@@ -279,6 +208,8 @@ var APP_BROWSER_PROCESSOR = (config, entry) => {
279
208
  if (context) {
280
209
  if (Object.keys(context).length === 1 && "error" in context) {
281
210
  args.push(context.error);
211
+ } else if (Object.keys(context).length === 1 && "err" in context) {
212
+ args.push(context.err);
282
213
  } else {
283
214
  args.push(context);
284
215
  }
@@ -315,7 +246,7 @@ var TEST_BROWSER_PROCESSOR = (config, entry) => {
315
246
  }
316
247
  let path = "";
317
248
  if (entry.meta) {
318
- path = `${getRelativeFilename(entry.meta.F)}:${entry.meta.L}`;
249
+ path = `${getRelativeFilename2(entry.meta.F)}:${entry.meta.L}`;
319
250
  }
320
251
  let args = [];
321
252
  const processPrefix = entry.meta?.S?.hostSessionId ? "[worker] " : "";
@@ -344,22 +275,20 @@ var TEST_BROWSER_PROCESSOR = (config, entry) => {
344
275
  };
345
276
  var BROWSER_PROCESSOR = CONFIG.useTestProcessor ? TEST_BROWSER_PROCESSOR : APP_BROWSER_PROCESSOR;
346
277
 
347
- // src/processors/file-processor.ts
348
- import { appendFileSync, mkdirSync, openSync } from "@dxos/node-std/fs";
349
- import { dirname } from "@dxos/node-std/path";
350
- import { jsonlogify } from "@dxos/util";
278
+ // src/processors/index.ts
279
+ __reExport(processors_exports, console_processor_star);
280
+ import * as console_processor_star from "#console-processor";
351
281
 
352
- // src/processors/common.ts
353
- var getRelativeFilename2 = (filename) => {
354
- const match = filename.match(/.+\/(packages\/.+\/.+)/);
355
- if (match) {
356
- const [, filePath] = match;
357
- return filePath;
358
- }
359
- return filename;
282
+ // src/processors/debug-processor.ts
283
+ import { inspect as inspect2 } from "@dxos/node-std/util";
284
+ var DEBUG_PROCESSOR = (config, entry) => {
285
+ console.log(inspect2(entry, false, null, true));
360
286
  };
361
287
 
362
288
  // src/processors/file-processor.ts
289
+ import { appendFileSync, mkdirSync, openSync } from "@dxos/node-std/fs";
290
+ import { dirname } from "@dxos/node-std/path";
291
+ import { jsonlogify } from "@dxos/util";
363
292
  var EAGAIN_MAX_DURATION = 1e3;
364
293
  var createFileProcessor = ({ pathOrFd, levels: levels2, filters }) => {
365
294
  let fd;
@@ -384,7 +313,7 @@ var createFileProcessor = ({ pathOrFd, levels: levels2, filters }) => {
384
313
  timestamp: Date.now(),
385
314
  ...entry.meta ? {
386
315
  meta: {
387
- file: getRelativeFilename2(entry.meta.F),
316
+ file: getRelativeFilename(entry.meta.F),
388
317
  line: entry.meta.L
389
318
  }
390
319
  } : {},
@@ -427,14 +356,15 @@ var FILE_PROCESSOR = createFileProcessor({
427
356
 
428
357
  // src/options.ts
429
358
  var processors = {
430
- [LogProcessorType.CONSOLE]: CONSOLE_PROCESSOR,
359
+ [LogProcessorType.CONSOLE]: processors_exports.CONSOLE_PROCESSOR,
431
360
  [LogProcessorType.BROWSER]: BROWSER_PROCESSOR,
432
361
  [LogProcessorType.DEBUG]: DEBUG_PROCESSOR
433
362
  };
434
- var IS_BROWSER = typeof window !== "undefined" || typeof navigator !== "undefined";
363
+ var browser = (typeof window !== "undefined" || typeof navigator !== "undefined") && !(typeof process !== "undefined" && process?.env?.VITEST);
435
364
  var DEFAULT_PROCESSORS = [
436
- IS_BROWSER ? BROWSER_PROCESSOR : CONSOLE_PROCESSOR
365
+ browser ? BROWSER_PROCESSOR : processors_exports.CONSOLE_PROCESSOR
437
366
  ];
367
+ var parseLogLevel = (level, defValue = LogLevel.WARN) => levels[level.toLowerCase()] ?? defValue;
438
368
  var parseFilter = (filter) => {
439
369
  if (typeof filter === "number") {
440
370
  return [
@@ -443,7 +373,6 @@ var parseFilter = (filter) => {
443
373
  }
444
374
  ];
445
375
  }
446
- const parseLogLevel = (level, defValue = LogLevel.WARN) => levels[level.toLowerCase()] ?? defValue;
447
376
  const lines = typeof filter === "string" ? filter.split(/,\s*/) : filter;
448
377
  return lines.map((filter2) => {
449
378
  const [pattern, level] = filter2.split(":");
@@ -455,54 +384,37 @@ var parseFilter = (filter) => {
455
384
  };
456
385
  });
457
386
  };
458
- var getConfig = (options) => {
459
- const nodeOptions = "process" in globalThis ? {
387
+ var createConfig = (options) => {
388
+ const envOptions = "process" in globalThis ? {
460
389
  file: process.env.LOG_CONFIG,
461
390
  filter: process.env.LOG_FILTER,
462
391
  processor: process.env.LOG_PROCESSOR
463
392
  } : void 0;
464
- const mergedOptions = defaultsDeep({}, loadOptions(nodeOptions?.file), nodeOptions, options);
393
+ const mergedOptions = defaultsDeep({}, (0, platform_exports.loadOptions)(envOptions?.file), envOptions, options);
465
394
  return {
466
395
  options: mergedOptions,
467
396
  filters: parseFilter(mergedOptions.filter ?? LogLevel.INFO),
468
397
  captureFilters: parseFilter(mergedOptions.captureFilter ?? LogLevel.WARN),
469
398
  processors: mergedOptions.processor ? [
470
399
  processors[mergedOptions.processor]
471
- ] : DEFAULT_PROCESSORS,
400
+ ] : [
401
+ ...DEFAULT_PROCESSORS
402
+ ],
472
403
  prefix: mergedOptions.prefix
473
404
  };
474
405
  };
475
406
 
476
407
  // src/log.ts
408
+ var logCount = 0;
477
409
  var createLog = () => {
478
410
  const log2 = (...params) => processLog(LogLevel.DEBUG, ...params);
479
- log2._config = getConfig();
411
+ Object.assign(log2, {
412
+ _id: `log-${++logCount}`,
413
+ _config: createConfig()
414
+ });
480
415
  Object.defineProperty(log2, "runtimeConfig", {
481
416
  get: () => log2._config
482
417
  });
483
- log2.addProcessor = (processor) => {
484
- if (DEFAULT_PROCESSORS.filter((p) => p === processor).length === 0) {
485
- DEFAULT_PROCESSORS.push(processor);
486
- }
487
- if (log2._config.processors.filter((p) => p === processor).length === 0) {
488
- log2._config.processors.push(processor);
489
- }
490
- };
491
- log2.config = (options) => {
492
- log2._config = getConfig(options);
493
- };
494
- log2.trace = (...params) => processLog(LogLevel.TRACE, ...params);
495
- log2.debug = (...params) => processLog(LogLevel.DEBUG, ...params);
496
- log2.verbose = (...params) => processLog(LogLevel.VERBOSE, ...params);
497
- log2.info = (...params) => processLog(LogLevel.INFO, ...params);
498
- log2.warn = (...params) => processLog(LogLevel.WARN, ...params);
499
- log2.error = (...params) => processLog(LogLevel.ERROR, ...params);
500
- log2.catch = (error, context, meta) => processLog(LogLevel.ERROR, void 0, context, meta, error);
501
- log2.break = () => log2.info("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014");
502
- log2.stack = (message, context, meta) => processLog(LogLevel.INFO, `${message ?? "Stack Dump"}
503
- ${getFormattedStackTrace()}`, context, meta);
504
- log2.method = createMethodLogDecorator(log2);
505
- log2.func = createFunctionLogDecorator(log2);
506
418
  const processLog = (level, message, context = {}, meta, error) => {
507
419
  log2._config.processors.forEach((processor) => processor(log2._config, {
508
420
  level,
@@ -512,9 +424,50 @@ ${getFormattedStackTrace()}`, context, meta);
512
424
  error
513
425
  }));
514
426
  };
427
+ Object.assign(log2, {
428
+ /**
429
+ * Update config.
430
+ * NOTE: Preserves any processors that were already added to this logger instance
431
+ * unless an explicit processor option is provided.
432
+ */
433
+ config: ({ processor, ...options }) => {
434
+ const config = createConfig(options);
435
+ const processors2 = processor ? config.processors : log2._config.processors;
436
+ log2._config = {
437
+ ...config,
438
+ processors: processors2
439
+ };
440
+ return log2;
441
+ },
442
+ /**
443
+ * Adds a processor to the logger.
444
+ */
445
+ addProcessor: (processor) => {
446
+ if (log2._config.processors.filter((p) => p === processor).length === 0) {
447
+ log2._config.processors.push(processor);
448
+ }
449
+ return () => {
450
+ log2._config.processors = log2._config.processors.filter((p) => p !== processor);
451
+ };
452
+ },
453
+ trace: (...params) => processLog(LogLevel.TRACE, ...params),
454
+ debug: (...params) => processLog(LogLevel.DEBUG, ...params),
455
+ verbose: (...params) => processLog(LogLevel.VERBOSE, ...params),
456
+ info: (...params) => processLog(LogLevel.INFO, ...params),
457
+ warn: (...params) => processLog(LogLevel.WARN, ...params),
458
+ error: (...params) => processLog(LogLevel.ERROR, ...params),
459
+ catch: (error, context, meta) => processLog(LogLevel.ERROR, void 0, context, meta, error),
460
+ method: createMethodLogDecorator(log2),
461
+ function: createFunctionLogDecorator(log2),
462
+ break: () => log2.info("-".repeat(80)),
463
+ stack: (message, context, meta) => {
464
+ return processLog(LogLevel.INFO, `${message ?? "Stack Dump"}
465
+ ${getFormattedStackTrace()}`, context, meta);
466
+ }
467
+ });
515
468
  return log2;
516
469
  };
517
- var log = globalThis.dx_log ??= createLog();
470
+ var log = globalThis.DX_LOG ??= createLog();
518
471
  var start = Date.now();
519
472
  var last = start;
520
473
  var debug = (label, args) => {
@@ -531,12 +484,18 @@ var debug = (label, args) => {
531
484
  };
532
485
  var getFormattedStackTrace = () => new Error().stack.split("\n").slice(3).join("\n");
533
486
 
487
+ // src/index.ts
488
+ __reExport(index_exports, processors_exports);
489
+
534
490
  // src/experimental/ownership.ts
535
491
  import { inspect as inspect3 } from "@dxos/node-std/util";
536
492
  var kOwnershipScope = Symbol("kOwnershipScope");
537
493
  var kCurrentOwnershipScope = Symbol("kCurrentOwnershipScope");
538
494
  var kDebugInfoProperties = Symbol("kDebugInfoProperties");
539
495
  var OwnershipScope = class {
496
+ constr;
497
+ parent;
498
+ instance;
540
499
  constructor(constr, parent) {
541
500
  this.constr = constr;
542
501
  this.parent = parent;
@@ -563,17 +522,17 @@ var OwnershipScope = class {
563
522
  var getCurrentOwnershipScope = (thisRef) => thisRef;
564
523
  export {
565
524
  BROWSER_PROCESSOR,
566
- CONSOLE_PROCESSOR,
567
525
  DEBUG_PROCESSOR,
568
526
  FILE_PROCESSOR,
569
527
  LogLevel,
570
528
  LogProcessorType,
571
529
  createFileProcessor,
530
+ createLog,
572
531
  debug,
573
532
  gatherLogInfoFromScope,
574
533
  getContextFromEntry,
575
534
  getCurrentOwnershipScope,
576
- getRelativeFilename2 as getRelativeFilename,
535
+ getRelativeFilename,
577
536
  levels,
578
537
  log,
579
538
  logInfo,