@dxos/log 0.8.4-main.f5c0578 → 0.8.4-main.fbb7a13

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 (57) 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 +132 -176
  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 +129 -260
  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/processors/browser-processor.d.ts.map +1 -1
  43. package/dist/types/src/processors/file-processor.d.ts.map +1 -1
  44. package/dist/types/src/processors/index.d.ts +3 -3
  45. package/dist/types/src/processors/index.d.ts.map +1 -1
  46. package/dist/types/tsconfig.tsbuildinfo +1 -1
  47. package/package.json +29 -9
  48. package/src/config.ts +3 -2
  49. package/src/context.ts +36 -5
  50. package/src/decorators.ts +3 -3
  51. package/src/log.test.ts +48 -18
  52. package/src/log.ts +101 -57
  53. package/src/options.ts +26 -10
  54. package/src/platform/index.ts +1 -1
  55. package/src/processors/browser-processor.ts +2 -0
  56. package/src/processors/file-processor.ts +2 -0
  57. package/src/processors/index.ts +3 -3
@@ -0,0 +1,135 @@
1
+ import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
+
3
+ // src/config.ts
4
+ var LogLevel = /* @__PURE__ */ (function(LogLevel2) {
5
+ LogLevel2[LogLevel2["TRACE"] = 5] = "TRACE";
6
+ LogLevel2[LogLevel2["DEBUG"] = 10] = "DEBUG";
7
+ LogLevel2[LogLevel2["VERBOSE"] = 11] = "VERBOSE";
8
+ LogLevel2[LogLevel2["INFO"] = 12] = "INFO";
9
+ LogLevel2[LogLevel2["WARN"] = 13] = "WARN";
10
+ LogLevel2[LogLevel2["ERROR"] = 14] = "ERROR";
11
+ return LogLevel2;
12
+ })({});
13
+ var levels = {
14
+ "*": 5,
15
+ trace: 5,
16
+ debug: 10,
17
+ verbose: 11,
18
+ info: 12,
19
+ warn: 13,
20
+ error: 14
21
+ };
22
+ var shortLevelName = {
23
+ [5]: "T",
24
+ [10]: "D",
25
+ [11]: "V",
26
+ [12]: "I",
27
+ [13]: "W",
28
+ [14]: "E"
29
+ };
30
+ var LogProcessorType = /* @__PURE__ */ (function(LogProcessorType2) {
31
+ LogProcessorType2["CONSOLE"] = "console";
32
+ LogProcessorType2["BROWSER"] = "browser";
33
+ LogProcessorType2["DEBUG"] = "debug";
34
+ return LogProcessorType2;
35
+ })({});
36
+
37
+ // src/scope.ts
38
+ var logInfoProperties = Symbol("logInfoProperties");
39
+ var logInfo = (target, propertyKey, descriptor) => {
40
+ (target[logInfoProperties] ??= []).push(propertyKey);
41
+ };
42
+ var gatherLogInfoFromScope = (scope) => {
43
+ if (!scope) {
44
+ return {};
45
+ }
46
+ const res = {};
47
+ const prototype = Object.getPrototypeOf(scope);
48
+ const infoProps = (typeof prototype === "object" && prototype !== null ? prototype[logInfoProperties] : []) ?? [];
49
+ for (const prop of infoProps) {
50
+ try {
51
+ res[prop] = typeof scope[prop] === "function" ? scope[prop]() : scope[prop];
52
+ } catch (err) {
53
+ res[prop] = err.message;
54
+ }
55
+ }
56
+ return res;
57
+ };
58
+
59
+ // src/context.ts
60
+ var matchFilter = (filter, level, path) => {
61
+ if (filter.pattern?.startsWith("-")) {
62
+ if (path?.includes(filter.pattern.slice(1))) {
63
+ if (level >= filter.level) {
64
+ return false;
65
+ }
66
+ }
67
+ } else {
68
+ if (filter.pattern?.length) {
69
+ if (path?.includes(filter.pattern)) {
70
+ return level >= filter.level;
71
+ }
72
+ } else {
73
+ if (level >= filter.level) {
74
+ return true;
75
+ }
76
+ }
77
+ }
78
+ };
79
+ var shouldLog = (entry, filters) => {
80
+ if (filters === void 0) {
81
+ return false;
82
+ }
83
+ const results = filters.map((filter) => matchFilter(filter, entry.level, entry.meta?.F)).filter((result) => result !== void 0);
84
+ return results.length > 0 && !results.some((results2) => results2 === false);
85
+ };
86
+ var getContextFromEntry = (entry) => {
87
+ let context;
88
+ if (entry.meta) {
89
+ const scopeInfo = gatherLogInfoFromScope(entry.meta.S);
90
+ if (Object.keys(scopeInfo).length > 0) {
91
+ context = Object.assign(context ?? {}, scopeInfo);
92
+ }
93
+ }
94
+ const entryContext = typeof entry.context === "function" ? entry.context() : entry.context;
95
+ if (entryContext) {
96
+ if (entryContext instanceof Error) {
97
+ const c = entryContext.context;
98
+ context = Object.assign(context ?? {}, {
99
+ error: entryContext.stack,
100
+ ...c
101
+ });
102
+ } else if (typeof entryContext === "object") {
103
+ context = Object.assign(context ?? {}, entryContext);
104
+ }
105
+ }
106
+ if (entry.error) {
107
+ context = Object.assign(context ?? {}, {
108
+ error: entry.error
109
+ });
110
+ }
111
+ return context && Object.keys(context).length > 0 ? context : void 0;
112
+ };
113
+
114
+ // src/processors/common.ts
115
+ var getRelativeFilename = (filename) => {
116
+ const match = filename.match(/.+\/(packages\/.+\/.+)/);
117
+ if (match) {
118
+ const [, filePath] = match;
119
+ return filePath;
120
+ }
121
+ return filename;
122
+ };
123
+
124
+ export {
125
+ LogLevel,
126
+ levels,
127
+ shortLevelName,
128
+ LogProcessorType,
129
+ logInfo,
130
+ gatherLogInfoFromScope,
131
+ shouldLog,
132
+ getContextFromEntry,
133
+ getRelativeFilename
134
+ };
135
+ //# sourceMappingURL=chunk-QPYJZ4SO.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": ";;;AAWO,IAAKA,WAAAA,0BAAAA,WAAAA;;;;;;;SAAAA;;AASL,IAAMC,SAAmC;EAC9C,KAAG;EACHC,OAAK;EACLC,OAAK;EACLC,SAAO;EACPC,MAAI;EACJC,MAAI;EACJC,OAAK;AACP;AAEO,IAAMC,iBAAiB;EAC5B,CAAA,CAAA,GAAkB;EAClB,CAAA,EAAA,GAAkB;EAClB,CAAA,EAAA,GAAoB;EACpB,CAAA,EAAA,GAAiB;EACjB,CAAA,EAAA,GAAiB;EACjB,CAAA,EAAA,GAAkB;AACpB;AAEO,IAAKC,mBAAAA,0BAAAA,mBAAAA;;;;SAAAA;;;;ACnCZ,IAAMC,oBAAoBC,OAAO,mBAAA;AAc1B,IAAMC,UAAU,CAACC,QAAaC,aAAqBC,eAAAA;AAEvDF,GAAAA,OAAOH,iBAAAA,MAAuB,CAAA,GAAIM,KAAKF,WAAAA;AAC1C;AAMO,IAAMG,yBAAyB,CAACC,UAAAA;AACrC,MAAI,CAACA,OAAO;AACV,WAAO,CAAC;EACV;AAEA,QAAMC,MAA2B,CAAC;AAElC,QAAMC,YAAYC,OAAOC,eAAeJ,KAAAA;AACxC,QAAMK,aAAa,OAAOH,cAAc,YAAYA,cAAc,OAAOA,UAAUV,iBAAAA,IAAqB,CAAA,MAAO,CAAA;AAC/G,aAAWc,QAAQD,WAAW;AAC5B,QAAI;AACFJ,UAAIK,IAAAA,IAAQ,OAAON,MAAMM,IAAAA,MAAU,aAAaN,MAAMM,IAAAA,EAAK,IAAKN,MAAMM,IAAAA;IACxE,SAASC,KAAU;AACjBN,UAAIK,IAAAA,IAAQC,IAAIC;IAClB;EACF;AAEA,SAAOP;AACT;;;ACVA,IAAMQ,cAAc,CAACC,QAAmBC,OAAiBC,SAAAA;AAEvD,MAAIF,OAAOG,SAASC,WAAW,GAAA,GAAM;AAEnC,QAAIF,MAAMG,SAASL,OAAOG,QAAQG,MAAM,CAAA,CAAA,GAAK;AAC3C,UAAIL,SAASD,OAAOC,OAAO;AACzB,eAAO;MACT;IACF;EACF,OAAO;AAEL,QAAID,OAAOG,SAASI,QAAQ;AAC1B,UAAIL,MAAMG,SAASL,OAAOG,OAAO,GAAG;AAClC,eAAOF,SAASD,OAAOC;MACzB;IACF,OAAO;AACL,UAAIA,SAASD,OAAOC,OAAO;AACzB,eAAO;MACT;IACF;EACF;AACF;AAKO,IAAMO,YAAY,CAACC,OAAiBC,YAAAA;AACzC,MAAIA,YAAYC,QAAW;AACzB,WAAO;EACT;AAEA,QAAMC,UAAUF,QACbG,IAAI,CAACb,WAAWD,YAAYC,QAAQS,MAAMR,OAAOQ,MAAMK,MAAMC,CAAAA,CAAAA,EAC7Df,OAAO,CAACgB,WAA8BA,WAAWL,MAAAA;AAIpD,SAAOC,QAAQL,SAAS,KAAK,CAACK,QAAQK,KAAK,CAACL,aAAYA,aAAY,KAAA;AACtE;AAEO,IAAMM,sBAAsB,CAACT,UAAAA;AAClC,MAAIU;AACJ,MAAIV,MAAMK,MAAM;AACd,UAAMM,YAAYC,uBAAuBZ,MAAMK,KAAKQ,CAAC;AACrD,QAAIC,OAAOC,KAAKJ,SAAAA,EAAWb,SAAS,GAAG;AACrCY,gBAAUI,OAAOE,OAAON,WAAW,CAAC,GAAGC,SAAAA;IACzC;EACF;AAEA,QAAMM,eAAe,OAAOjB,MAAMU,YAAY,aAAaV,MAAMU,QAAO,IAAKV,MAAMU;AACnF,MAAIO,cAAc;AAChB,QAAIA,wBAAwBC,OAAO;AAEjC,YAAMC,IAAKF,aAAqBP;AAEhCA,gBAAUI,OAAOE,OAAON,WAAW,CAAC,GAAG;QAAEU,OAAOH,aAAaI;QAAO,GAAGF;MAAE,CAAA;IAC3E,WAAW,OAAOF,iBAAiB,UAAU;AAC3CP,gBAAUI,OAAOE,OAAON,WAAW,CAAC,GAAGO,YAAAA;IACzC;EACF;AAEA,MAAIjB,MAAMoB,OAAO;AACfV,cAAUI,OAAOE,OAAON,WAAW,CAAC,GAAG;MAAEU,OAAOpB,MAAMoB;IAAM,CAAA;EAC9D;AAEA,SAAOV,WAAWI,OAAOC,KAAKL,OAAAA,EAASZ,SAAS,IAAIY,UAAUR;AAChE;;;ACjGO,IAAMoB,sBAAsB,CAACC,aAAAA;AAGlC,QAAMC,QAAQD,SAASC,MAAM,wBAAA;AAC7B,MAAIA,OAAO;AACT,UAAM,CAAA,EAAGC,QAAAA,IAAYD;AACrB,WAAOC;EACT;AAEA,SAAOF;AACT;",
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
+ }
@@ -1,103 +1,47 @@
1
1
  import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
+ import {
3
+ LogLevel,
4
+ LogProcessorType,
5
+ gatherLogInfoFromScope,
6
+ getContextFromEntry,
7
+ getRelativeFilename,
8
+ levels,
9
+ logInfo,
10
+ shortLevelName,
11
+ shouldLog
12
+ } from "./chunk-QPYJZ4SO.mjs";
13
+ import {
14
+ __export,
15
+ __reExport
16
+ } from "./chunk-2SZHAWBN.mjs";
2
17
 
3
18
  // src/index.ts
19
+ var index_exports = {};
20
+ __export(index_exports, {
21
+ BROWSER_PROCESSOR: () => BROWSER_PROCESSOR,
22
+ DEBUG_PROCESSOR: () => DEBUG_PROCESSOR,
23
+ FILE_PROCESSOR: () => FILE_PROCESSOR,
24
+ LogLevel: () => LogLevel,
25
+ LogProcessorType: () => LogProcessorType,
26
+ createFileProcessor: () => createFileProcessor,
27
+ createLog: () => createLog,
28
+ debug: () => debug,
29
+ gatherLogInfoFromScope: () => gatherLogInfoFromScope,
30
+ getContextFromEntry: () => getContextFromEntry,
31
+ getCurrentOwnershipScope: () => getCurrentOwnershipScope,
32
+ getRelativeFilename: () => getRelativeFilename,
33
+ levels: () => levels,
34
+ log: () => log,
35
+ logInfo: () => logInfo,
36
+ omit: () => omit,
37
+ parseFilter: () => parseFilter,
38
+ pick: () => pick,
39
+ shortLevelName: () => shortLevelName,
40
+ shouldLog: () => shouldLog
41
+ });
4
42
  import omit from "lodash.omit";
5
43
  import { pick } from "@dxos/util";
6
44
 
7
- // src/config.ts
8
- var LogLevel = /* @__PURE__ */ function(LogLevel2) {
9
- LogLevel2[LogLevel2["TRACE"] = 5] = "TRACE";
10
- LogLevel2[LogLevel2["DEBUG"] = 10] = "DEBUG";
11
- LogLevel2[LogLevel2["VERBOSE"] = 11] = "VERBOSE";
12
- LogLevel2[LogLevel2["INFO"] = 12] = "INFO";
13
- LogLevel2[LogLevel2["WARN"] = 13] = "WARN";
14
- LogLevel2[LogLevel2["ERROR"] = 14] = "ERROR";
15
- return LogLevel2;
16
- }({});
17
- var levels = {
18
- trace: 5,
19
- debug: 10,
20
- verbose: 11,
21
- info: 12,
22
- warn: 13,
23
- error: 14
24
- };
25
- var shortLevelName = {
26
- [5]: "T",
27
- [10]: "D",
28
- [11]: "V",
29
- [12]: "I",
30
- [13]: "W",
31
- [14]: "E"
32
- };
33
- var LogProcessorType = /* @__PURE__ */ function(LogProcessorType2) {
34
- LogProcessorType2["CONSOLE"] = "console";
35
- LogProcessorType2["BROWSER"] = "browser";
36
- LogProcessorType2["DEBUG"] = "debug";
37
- return LogProcessorType2;
38
- }({});
39
-
40
- // src/scope.ts
41
- var logInfoProperties = Symbol("logInfoProperties");
42
- var logInfo = (target, propertyKey, descriptor) => {
43
- (target[logInfoProperties] ??= []).push(propertyKey);
44
- };
45
- var gatherLogInfoFromScope = (scope) => {
46
- if (!scope) {
47
- return {};
48
- }
49
- const res = {};
50
- const prototype = Object.getPrototypeOf(scope);
51
- const infoProps = (typeof prototype === "object" && prototype !== null ? prototype[logInfoProperties] : []) ?? [];
52
- for (const prop of infoProps) {
53
- try {
54
- res[prop] = typeof scope[prop] === "function" ? scope[prop]() : scope[prop];
55
- } catch (err) {
56
- res[prop] = err.message;
57
- }
58
- }
59
- return res;
60
- };
61
-
62
- // src/context.ts
63
- var matchFilter = (filter, level, path) => {
64
- return level >= filter.level && (!filter.pattern || path.includes(filter.pattern));
65
- };
66
- var shouldLog = (entry, filters) => {
67
- if (filters === void 0) {
68
- return true;
69
- } else {
70
- return filters.some((filter) => matchFilter(filter, entry.level, entry.meta?.F ?? ""));
71
- }
72
- };
73
- var getContextFromEntry = (entry) => {
74
- let context;
75
- if (entry.meta) {
76
- const scopeInfo = gatherLogInfoFromScope(entry.meta.S);
77
- if (Object.keys(scopeInfo).length > 0) {
78
- context = Object.assign(context ?? {}, scopeInfo);
79
- }
80
- }
81
- const entryContext = typeof entry.context === "function" ? entry.context() : entry.context;
82
- if (entryContext) {
83
- if (entryContext instanceof Error) {
84
- const c = entryContext.context;
85
- context = Object.assign(context ?? {}, {
86
- error: entryContext.stack,
87
- ...c
88
- });
89
- } else if (typeof entryContext === "object") {
90
- context = Object.assign(context ?? {}, entryContext);
91
- }
92
- }
93
- if (entry.error) {
94
- context = Object.assign(context ?? {}, {
95
- error: entry.error
96
- });
97
- }
98
- return context && Object.keys(context).length > 0 ? context : void 0;
99
- };
100
-
101
45
  // src/decorators.ts
102
46
  import { inspect } from "node:util";
103
47
  import chalk from "chalk";
@@ -197,144 +141,33 @@ var logAsyncResolved = (log2, methodName, resolvedValue, promiseId, startTime, c
197
141
  var logAsyncRejected = (log2, methodName, err, promiseId, startTime, combinedMeta) => {
198
142
  log2.info(`.${formatFunction(methodName)} \u21B2 \u{1F525} ${chalk.gray("reject")} ${formatPromise(promiseId)} ${formatTimeElapsed(startTime)} ${chalk.gray("=>")} ${err}`, {}, combinedMeta);
199
143
  };
200
- var greenCheck = typeof chalk.green === "function" ? chalk.green("\u2714") : "\u2714";
201
- var formatTimeElapsed = (startTime) => chalk.gray(`${(performance.now() - startTime).toFixed(0)}ms`);
202
144
  var COLOR_FUNCTION = [
203
145
  220,
204
146
  220,
205
147
  170
206
148
  ];
149
+ var greenCheck = typeof chalk.green === "function" ? chalk.green("\u2714") : "\u2714";
150
+ var formatTimeElapsed = (startTime) => chalk.gray(`${(performance.now() - startTime).toFixed(0)}ms`);
207
151
  var formatFunction = (name) => chalk.bold(chalk.rgb(...COLOR_FUNCTION)(name));
208
152
  var formatPromise = (id) => chalk.blue(`Promise#${id}`);
209
153
 
210
154
  // src/options.ts
211
155
  import defaultsDeep from "lodash.defaultsdeep";
212
156
 
213
- // src/platform/node/index.ts
214
- import fs from "node:fs";
215
- import yaml from "js-yaml";
216
- var loadOptions = (filepath) => {
217
- if (filepath) {
218
- try {
219
- const text = fs.readFileSync(filepath, "utf-8");
220
- if (text) {
221
- return yaml.load(text);
222
- }
223
- } catch (err) {
224
- console.warn(`Invalid log file: ${filepath}`);
225
- }
226
- }
227
- };
228
-
229
- // src/processors/console-processor.ts
230
- import { inspect as inspect2 } from "node:util";
231
- import chalk2 from "chalk";
232
- import { getPrototypeSpecificInstanceId, pickBy } from "@dxos/util";
233
-
234
- // src/processors/common.ts
235
- var getRelativeFilename = (filename) => {
236
- const match = filename.match(/.+\/(packages\/.+\/.+)/);
237
- if (match) {
238
- const [, filePath] = match;
239
- return filePath;
240
- }
241
- return filename;
242
- };
243
-
244
- // src/processors/console-processor.ts
245
- var LEVEL_COLORS = {
246
- [LogLevel.TRACE]: "gray",
247
- [LogLevel.DEBUG]: "gray",
248
- [LogLevel.VERBOSE]: "gray",
249
- [LogLevel.INFO]: "white",
250
- [LogLevel.WARN]: "yellow",
251
- [LogLevel.ERROR]: "red"
252
- };
253
- var truncate = (text, length = 0, right = false) => {
254
- const str = text && length ? right ? text.slice(-length) : text.substring(0, length) : text ?? "";
255
- return right ? str.padStart(length, " ") : str.padEnd(length, " ");
256
- };
257
- var DEFAULT_FORMATTER = (config, { path, line, level, message, context, error, scope }) => {
258
- const column = config.options?.formatter?.column;
259
- const filepath = path !== void 0 && line !== void 0 ? chalk2.grey(`${path}:${line}`) : void 0;
260
- let instance;
261
- if (scope) {
262
- const prototype = Object.getPrototypeOf(scope);
263
- if (prototype !== null) {
264
- const id = getPrototypeSpecificInstanceId(scope);
265
- instance = chalk2.magentaBright(`${prototype.constructor.name}#${id}`);
266
- }
267
- }
268
- const formattedTimestamp = config.options?.formatter?.timestamp ? (/* @__PURE__ */ new Date()).toISOString() : void 0;
269
- const formattedLevel = chalk2[LEVEL_COLORS[level]](column ? shortLevelName[level] : LogLevel[level]);
270
- const padding = column && filepath ? "".padStart(column - filepath.length) : void 0;
271
- return config.options?.formatter?.timestampFirst ? [
272
- formattedTimestamp,
273
- filepath,
274
- padding,
275
- formattedLevel,
276
- instance,
277
- message,
278
- context,
279
- error
280
- ] : [
281
- // NOTE: File path must come fist for console hyperlinks.
282
- // Must not truncate for terminal output.
283
- filepath,
284
- padding,
285
- formattedTimestamp,
286
- formattedLevel,
287
- instance,
288
- message,
289
- context,
290
- error
291
- ];
292
- };
293
- var SHORT_FORMATTER = (config, { path, level, message }) => {
294
- return [
295
- chalk2.grey(truncate(path, 16, true)),
296
- chalk2[LEVEL_COLORS[level]](shortLevelName[level]),
297
- message
298
- ];
299
- };
300
- var formatter = DEFAULT_FORMATTER;
301
- var CONSOLE_PROCESSOR = (config, entry) => {
302
- const { level, message, meta, error } = entry;
303
- if (!shouldLog(entry, config.filters)) {
304
- return;
305
- }
306
- const parts = {
307
- level,
308
- message,
309
- error,
310
- path: void 0,
311
- line: void 0,
312
- scope: void 0,
313
- context: void 0
314
- };
315
- if (meta) {
316
- parts.path = getRelativeFilename(meta.F);
317
- parts.line = meta.L;
318
- parts.scope = meta.S;
319
- }
320
- const context = getContextFromEntry(entry);
321
- if (context) {
322
- parts.context = inspect2(pickBy(context, (value) => value !== void 0), {
323
- depth: config.options.depth,
324
- colors: true,
325
- maxArrayLength: 8,
326
- sorted: false
327
- });
328
- }
329
- const line = formatter(config, parts).filter(Boolean).join(" ");
330
- console.log(line);
331
- };
157
+ // src/platform/index.ts
158
+ var platform_exports = {};
159
+ __reExport(platform_exports, platform_star);
160
+ import * as platform_star from "#platform";
332
161
 
333
- // src/processors/debug-processor.ts
334
- import { inspect as inspect3 } from "node:util";
335
- var DEBUG_PROCESSOR = (config, entry) => {
336
- console.log(inspect3(entry, false, null, true));
337
- };
162
+ // src/processors/index.ts
163
+ var processors_exports = {};
164
+ __export(processors_exports, {
165
+ BROWSER_PROCESSOR: () => BROWSER_PROCESSOR,
166
+ DEBUG_PROCESSOR: () => DEBUG_PROCESSOR,
167
+ FILE_PROCESSOR: () => FILE_PROCESSOR,
168
+ createFileProcessor: () => createFileProcessor,
169
+ getRelativeFilename: () => getRelativeFilename
170
+ });
338
171
 
339
172
  // src/processors/browser-processor.ts
340
173
  import { getDebugName, safariCheck } from "@dxos/util";
@@ -376,6 +209,8 @@ var APP_BROWSER_PROCESSOR = (config, entry) => {
376
209
  if (context) {
377
210
  if (Object.keys(context).length === 1 && "error" in context) {
378
211
  args.push(context.error);
212
+ } else if (Object.keys(context).length === 1 && "err" in context) {
213
+ args.push(context.err);
379
214
  } else {
380
215
  args.push(context);
381
216
  }
@@ -441,6 +276,16 @@ var TEST_BROWSER_PROCESSOR = (config, entry) => {
441
276
  };
442
277
  var BROWSER_PROCESSOR = CONFIG.useTestProcessor ? TEST_BROWSER_PROCESSOR : APP_BROWSER_PROCESSOR;
443
278
 
279
+ // src/processors/index.ts
280
+ __reExport(processors_exports, console_processor_star);
281
+ import * as console_processor_star from "#console-processor";
282
+
283
+ // src/processors/debug-processor.ts
284
+ import { inspect as inspect2 } from "node:util";
285
+ var DEBUG_PROCESSOR = (config, entry) => {
286
+ console.log(inspect2(entry, false, null, true));
287
+ };
288
+
444
289
  // src/processors/file-processor.ts
445
290
  import { appendFileSync, mkdirSync, openSync } from "node:fs";
446
291
  import { dirname } from "node:path";
@@ -512,14 +357,15 @@ var FILE_PROCESSOR = createFileProcessor({
512
357
 
513
358
  // src/options.ts
514
359
  var processors = {
515
- [LogProcessorType.CONSOLE]: CONSOLE_PROCESSOR,
360
+ [LogProcessorType.CONSOLE]: processors_exports.CONSOLE_PROCESSOR,
516
361
  [LogProcessorType.BROWSER]: BROWSER_PROCESSOR,
517
362
  [LogProcessorType.DEBUG]: DEBUG_PROCESSOR
518
363
  };
519
- var IS_BROWSER = typeof window !== "undefined" || typeof navigator !== "undefined";
364
+ var browser = (typeof window !== "undefined" || typeof navigator !== "undefined") && !(typeof process !== "undefined" && process?.env?.VITEST);
520
365
  var DEFAULT_PROCESSORS = [
521
- IS_BROWSER ? BROWSER_PROCESSOR : CONSOLE_PROCESSOR
366
+ browser ? BROWSER_PROCESSOR : processors_exports.CONSOLE_PROCESSOR
522
367
  ];
368
+ var parseLogLevel = (level, defValue = LogLevel.WARN) => levels[level.toLowerCase()] ?? defValue;
523
369
  var parseFilter = (filter) => {
524
370
  if (typeof filter === "number") {
525
371
  return [
@@ -528,7 +374,6 @@ var parseFilter = (filter) => {
528
374
  }
529
375
  ];
530
376
  }
531
- const parseLogLevel = (level, defValue = LogLevel.WARN) => levels[level.toLowerCase()] ?? defValue;
532
377
  const lines = typeof filter === "string" ? filter.split(/,\s*/) : filter;
533
378
  return lines.map((filter2) => {
534
379
  const [pattern, level] = filter2.split(":");
@@ -540,54 +385,37 @@ var parseFilter = (filter) => {
540
385
  };
541
386
  });
542
387
  };
543
- var getConfig = (options) => {
544
- const nodeOptions = "process" in globalThis ? {
388
+ var createConfig = (options) => {
389
+ const envOptions = "process" in globalThis ? {
545
390
  file: process.env.LOG_CONFIG,
546
391
  filter: process.env.LOG_FILTER,
547
392
  processor: process.env.LOG_PROCESSOR
548
393
  } : void 0;
549
- const mergedOptions = defaultsDeep({}, loadOptions(nodeOptions?.file), nodeOptions, options);
394
+ const mergedOptions = defaultsDeep({}, (0, platform_exports.loadOptions)(envOptions?.file), envOptions, options);
550
395
  return {
551
396
  options: mergedOptions,
552
397
  filters: parseFilter(mergedOptions.filter ?? LogLevel.INFO),
553
398
  captureFilters: parseFilter(mergedOptions.captureFilter ?? LogLevel.WARN),
554
399
  processors: mergedOptions.processor ? [
555
400
  processors[mergedOptions.processor]
556
- ] : DEFAULT_PROCESSORS,
401
+ ] : [
402
+ ...DEFAULT_PROCESSORS
403
+ ],
557
404
  prefix: mergedOptions.prefix
558
405
  };
559
406
  };
560
407
 
561
408
  // src/log.ts
409
+ var logCount = 0;
562
410
  var createLog = () => {
563
411
  const log2 = (...params) => processLog(LogLevel.DEBUG, ...params);
564
- log2._config = getConfig();
412
+ Object.assign(log2, {
413
+ _id: `log-${++logCount}`,
414
+ _config: createConfig()
415
+ });
565
416
  Object.defineProperty(log2, "runtimeConfig", {
566
417
  get: () => log2._config
567
418
  });
568
- log2.addProcessor = (processor) => {
569
- if (DEFAULT_PROCESSORS.filter((p) => p === processor).length === 0) {
570
- DEFAULT_PROCESSORS.push(processor);
571
- }
572
- if (log2._config.processors.filter((p) => p === processor).length === 0) {
573
- log2._config.processors.push(processor);
574
- }
575
- };
576
- log2.config = (options) => {
577
- log2._config = getConfig(options);
578
- };
579
- log2.trace = (...params) => processLog(LogLevel.TRACE, ...params);
580
- log2.debug = (...params) => processLog(LogLevel.DEBUG, ...params);
581
- log2.verbose = (...params) => processLog(LogLevel.VERBOSE, ...params);
582
- log2.info = (...params) => processLog(LogLevel.INFO, ...params);
583
- log2.warn = (...params) => processLog(LogLevel.WARN, ...params);
584
- log2.error = (...params) => processLog(LogLevel.ERROR, ...params);
585
- log2.catch = (error, context, meta) => processLog(LogLevel.ERROR, void 0, context, meta, error);
586
- 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");
587
- log2.stack = (message, context, meta) => processLog(LogLevel.INFO, `${message ?? "Stack Dump"}
588
- ${getFormattedStackTrace()}`, context, meta);
589
- log2.method = createMethodLogDecorator(log2);
590
- log2.func = createFunctionLogDecorator(log2);
591
419
  const processLog = (level, message, context = {}, meta, error) => {
592
420
  log2._config.processors.forEach((processor) => processor(log2._config, {
593
421
  level,
@@ -597,9 +425,50 @@ ${getFormattedStackTrace()}`, context, meta);
597
425
  error
598
426
  }));
599
427
  };
428
+ Object.assign(log2, {
429
+ /**
430
+ * Update config.
431
+ * NOTE: Preserves any processors that were already added to this logger instance
432
+ * unless an explicit processor option is provided.
433
+ */
434
+ config: ({ processor, ...options }) => {
435
+ const config = createConfig(options);
436
+ const processors2 = processor ? config.processors : log2._config.processors;
437
+ log2._config = {
438
+ ...config,
439
+ processors: processors2
440
+ };
441
+ return log2;
442
+ },
443
+ /**
444
+ * Adds a processor to the logger.
445
+ */
446
+ addProcessor: (processor) => {
447
+ if (log2._config.processors.filter((p) => p === processor).length === 0) {
448
+ log2._config.processors.push(processor);
449
+ }
450
+ return () => {
451
+ log2._config.processors = log2._config.processors.filter((p) => p !== processor);
452
+ };
453
+ },
454
+ trace: (...params) => processLog(LogLevel.TRACE, ...params),
455
+ debug: (...params) => processLog(LogLevel.DEBUG, ...params),
456
+ verbose: (...params) => processLog(LogLevel.VERBOSE, ...params),
457
+ info: (...params) => processLog(LogLevel.INFO, ...params),
458
+ warn: (...params) => processLog(LogLevel.WARN, ...params),
459
+ error: (...params) => processLog(LogLevel.ERROR, ...params),
460
+ catch: (error, context, meta) => processLog(LogLevel.ERROR, void 0, context, meta, error),
461
+ method: createMethodLogDecorator(log2),
462
+ function: createFunctionLogDecorator(log2),
463
+ break: () => log2.info("-".repeat(80)),
464
+ stack: (message, context, meta) => {
465
+ return processLog(LogLevel.INFO, `${message ?? "Stack Dump"}
466
+ ${getFormattedStackTrace()}`, context, meta);
467
+ }
468
+ });
600
469
  return log2;
601
470
  };
602
- var log = globalThis.dx_log ??= createLog();
471
+ var log = globalThis.DX_LOG ??= createLog();
603
472
  var start = Date.now();
604
473
  var last = start;
605
474
  var debug = (label, args) => {
@@ -616,8 +485,11 @@ var debug = (label, args) => {
616
485
  };
617
486
  var getFormattedStackTrace = () => new Error().stack.split("\n").slice(3).join("\n");
618
487
 
488
+ // src/index.ts
489
+ __reExport(index_exports, processors_exports);
490
+
619
491
  // src/experimental/ownership.ts
620
- import { inspect as inspect4 } from "node:util";
492
+ import { inspect as inspect3 } from "node:util";
621
493
  var kOwnershipScope = Symbol("kOwnershipScope");
622
494
  var kCurrentOwnershipScope = Symbol("kCurrentOwnershipScope");
623
495
  var kDebugInfoProperties = Symbol("kDebugInfoProperties");
@@ -640,7 +512,7 @@ var OwnershipScope = class {
640
512
  }
641
513
  return info;
642
514
  }
643
- [inspect4.custom]() {
515
+ [inspect3.custom]() {
644
516
  return {
645
517
  className: this.constr.name,
646
518
  info: this.getInfo(),
@@ -651,14 +523,12 @@ var OwnershipScope = class {
651
523
  var getCurrentOwnershipScope = (thisRef) => thisRef;
652
524
  export {
653
525
  BROWSER_PROCESSOR,
654
- CONSOLE_PROCESSOR,
655
526
  DEBUG_PROCESSOR,
656
- DEFAULT_FORMATTER,
657
527
  FILE_PROCESSOR,
658
528
  LogLevel,
659
529
  LogProcessorType,
660
- SHORT_FORMATTER,
661
530
  createFileProcessor,
531
+ createLog,
662
532
  debug,
663
533
  gatherLogInfoFromScope,
664
534
  getContextFromEntry,
@@ -671,7 +541,6 @@ export {
671
541
  parseFilter,
672
542
  pick,
673
543
  shortLevelName,
674
- shouldLog,
675
- truncate
544
+ shouldLog
676
545
  };
677
546
  //# sourceMappingURL=index.mjs.map