@dxos/log 0.8.4-staging.ac66bdf99f → 0.9.0

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/LICENSE +102 -5
  2. package/dist/lib/browser/{chunk-DTUPLFUN.mjs → chunk-V7FYKT4H.mjs} +1 -1
  3. package/dist/lib/browser/index.mjs +118 -72
  4. package/dist/lib/browser/index.mjs.map +4 -4
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/platform/node/index.mjs.map +2 -2
  7. package/dist/lib/browser/processors/console-processor.mjs +1 -1
  8. package/dist/lib/browser/processors/file-processor.mjs +75 -0
  9. package/dist/lib/browser/processors/file-processor.mjs.map +7 -0
  10. package/dist/lib/node-esm/{chunk-KOTHKRYW.mjs → chunk-5TBDXMQF.mjs} +1 -1
  11. package/dist/lib/node-esm/index.mjs +118 -72
  12. package/dist/lib/node-esm/index.mjs.map +4 -4
  13. package/dist/lib/node-esm/meta.json +1 -1
  14. package/dist/lib/node-esm/platform/node/index.mjs.map +2 -2
  15. package/dist/lib/node-esm/processors/console-processor.mjs +1 -1
  16. package/dist/lib/node-esm/processors/file-processor.mjs +76 -0
  17. package/dist/lib/node-esm/processors/file-processor.mjs.map +7 -0
  18. package/dist/types/src/context.d.ts.map +1 -1
  19. package/dist/types/src/decorators.d.ts.map +1 -1
  20. package/dist/types/src/environment.d.ts +24 -0
  21. package/dist/types/src/environment.d.ts.map +1 -0
  22. package/dist/types/src/environment.test.d.ts +2 -0
  23. package/dist/types/src/environment.test.d.ts.map +1 -0
  24. package/dist/types/src/experimental/ownership.d.ts.map +1 -1
  25. package/dist/types/src/index.d.ts +4 -1
  26. package/dist/types/src/index.d.ts.map +1 -1
  27. package/dist/types/src/jsonl.d.ts +53 -0
  28. package/dist/types/src/jsonl.d.ts.map +1 -0
  29. package/dist/types/src/jsonl.test.d.ts +2 -0
  30. package/dist/types/src/jsonl.test.d.ts.map +1 -0
  31. package/dist/types/src/log-buffer.d.ts +0 -20
  32. package/dist/types/src/log-buffer.d.ts.map +1 -1
  33. package/dist/types/src/log.d.ts +41 -0
  34. package/dist/types/src/log.d.ts.map +1 -1
  35. package/dist/types/src/meta.d.ts +20 -1
  36. package/dist/types/src/meta.d.ts.map +1 -1
  37. package/dist/types/src/platform/browser/index.d.ts.map +1 -1
  38. package/dist/types/src/platform/node/index.d.ts.map +1 -1
  39. package/dist/types/src/processors/common.d.ts.map +1 -1
  40. package/dist/types/src/processors/console-processor.d.ts.map +1 -1
  41. package/dist/types/src/processors/file-processor.d.ts.map +1 -1
  42. package/dist/types/src/processors/index.d.ts +0 -1
  43. package/dist/types/src/processors/index.d.ts.map +1 -1
  44. package/dist/types/src/scope.d.ts.map +1 -1
  45. package/dist/types/tsconfig.tsbuildinfo +1 -1
  46. package/package.json +16 -8
  47. package/src/environment.test.ts +222 -0
  48. package/src/environment.ts +129 -0
  49. package/src/index.ts +4 -1
  50. package/src/jsonl.test.ts +121 -0
  51. package/src/jsonl.ts +104 -0
  52. package/src/log-buffer.ts +1 -22
  53. package/src/log.ts +49 -0
  54. package/src/meta.ts +29 -1
  55. package/src/processors/index.ts +0 -1
  56. /package/dist/lib/browser/{chunk-DTUPLFUN.mjs.map → chunk-V7FYKT4H.mjs.map} +0 -0
  57. /package/dist/lib/node-esm/{chunk-KOTHKRYW.mjs.map → chunk-5TBDXMQF.mjs.map} +0 -0
package/LICENSE CHANGED
@@ -1,8 +1,105 @@
1
- MIT License
2
- Copyright (c) 2022 DXOS
1
+ # Functional Source License, Version 1.1, ALv2 Future License
3
2
 
4
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
3
+ ## Abbreviation
5
4
 
6
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
5
+ FSL-1.1-Apache-2.0
7
6
 
8
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7
+ ## Notice
8
+
9
+ Copyright 2026 DXOS
10
+
11
+ ## Terms and Conditions
12
+
13
+ ### Licensor ("We")
14
+
15
+ The party offering the Software under these Terms and Conditions.
16
+
17
+ ### The Software
18
+
19
+ The "Software" is each version of the software that we make available under
20
+ these Terms and Conditions, as indicated by our inclusion of these Terms and
21
+ Conditions with the Software.
22
+
23
+ ### License Grant
24
+
25
+ Subject to your compliance with this License Grant and the Patents,
26
+ Redistribution and Trademark clauses below, we hereby grant you the right to
27
+ use, copy, modify, create derivative works, publicly perform, publicly display
28
+ and redistribute the Software for any Permitted Purpose identified below.
29
+
30
+ ### Permitted Purpose
31
+
32
+ A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
33
+ means making the Software available to others in a commercial product or
34
+ service that:
35
+
36
+ 1. substitutes for the Software;
37
+
38
+ 2. substitutes for any other product or service we offer using the Software
39
+ that exists as of the date we make the Software available; or
40
+
41
+ 3. offers the same or substantially similar functionality as the Software.
42
+
43
+ Permitted Purposes specifically include using the Software:
44
+
45
+ 1. for your internal use and access;
46
+
47
+ 2. for non-commercial education;
48
+
49
+ 3. for non-commercial research; and
50
+
51
+ 4. in connection with professional services that you provide to a licensee
52
+ using the Software in accordance with these Terms and Conditions.
53
+
54
+ ### Patents
55
+
56
+ To the extent your use for a Permitted Purpose would necessarily infringe our
57
+ patents, the license grant above includes a license under our patents. If you
58
+ make a claim against any party that the Software infringes or contributes to
59
+ the infringement of any patent, then your patent license to the Software ends
60
+ immediately.
61
+
62
+ ### Redistribution
63
+
64
+ The Terms and Conditions apply to all copies, modifications and derivatives of
65
+ the Software.
66
+
67
+ If you redistribute any copies, modifications or derivatives of the Software,
68
+ you must include a copy of or a link to these Terms and Conditions and not
69
+ remove any copyright notices provided in or with the Software.
70
+
71
+ ### Disclaimer
72
+
73
+ THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
74
+ IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
75
+ PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
76
+
77
+ IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
78
+ SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
79
+ EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
80
+
81
+ ### Trademarks
82
+
83
+ Except for displaying the License Details and identifying us as the origin of
84
+ the Software, you have no right under these Terms and Conditions to use our
85
+ trademarks, trade names, service marks or product names.
86
+
87
+ ## Grant of Future License
88
+
89
+ We hereby irrevocably grant you an additional license to use the Software under
90
+ the Apache License, Version 2.0 that is effective on the second anniversary of
91
+ the date we make the Software available. On or after that date, you may use the
92
+ Software under the Apache License, Version 2.0, in which case the following
93
+ will apply:
94
+
95
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use
96
+ this file except in compliance with the License.
97
+
98
+ You may obtain a copy of the License at
99
+
100
+ http://www.apache.org/licenses/LICENSE-2.0
101
+
102
+ Unless required by applicable law or agreed to in writing, software distributed
103
+ under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
104
+ CONDITIONS OF ANY KIND, either express or implied. See the License for the
105
+ specific language governing permissions and limitations under the License.
@@ -308,4 +308,4 @@ export {
308
308
  shouldLog,
309
309
  getContextFromEntry
310
310
  };
311
- //# sourceMappingURL=chunk-DTUPLFUN.mjs.map
311
+ //# sourceMappingURL=chunk-V7FYKT4H.mjs.map
@@ -9,7 +9,7 @@ import {
9
9
  logInfo,
10
10
  shortLevelName,
11
11
  shouldLog
12
- } from "./chunk-DTUPLFUN.mjs";
12
+ } from "./chunk-V7FYKT4H.mjs";
13
13
  import {
14
14
  __export,
15
15
  __reExport
@@ -20,12 +20,11 @@ var index_exports = {};
20
20
  __export(index_exports, {
21
21
  BROWSER_PROCESSOR: () => BROWSER_PROCESSOR,
22
22
  DEBUG_PROCESSOR: () => DEBUG_PROCESSOR,
23
- FILE_PROCESSOR: () => FILE_PROCESSOR,
23
+ LOG_META_MARKER: () => LOG_META_MARKER,
24
24
  LogBuffer: () => LogBuffer,
25
25
  LogEntry: () => LogEntry,
26
26
  LogLevel: () => LogLevel,
27
27
  LogProcessorType: () => LogProcessorType,
28
- createFileProcessor: () => createFileProcessor,
29
28
  createLog: () => createLog,
30
29
  dbg: () => dbg,
31
30
  debug: () => debug,
@@ -33,17 +32,123 @@ __export(index_exports, {
33
32
  getContextFromEntry: () => getContextFromEntry,
34
33
  getCurrentOwnershipScope: () => getCurrentOwnershipScope,
35
34
  getRelativeFilename: () => getRelativeFilename,
35
+ inferEnvironmentName: () => inferEnvironmentName,
36
+ isLogMeta: () => isLogMeta,
36
37
  levels: () => levels,
37
38
  log: () => log,
38
39
  logInfo: () => logInfo,
39
40
  omit: () => omit,
40
41
  parseFilter: () => parseFilter,
41
42
  pick: () => pick,
43
+ serializeToJsonl: () => serializeToJsonl,
42
44
  shortLevelName: () => shortLevelName,
43
45
  shouldLog: () => shouldLog
44
46
  });
45
47
  import { omit, pick } from "@dxos/util";
46
48
 
49
+ // src/environment.ts
50
+ var TAB_SUFFIX_STORAGE_KEY = "@dxos/log:env-suffix";
51
+ var SUFFIX_LENGTH = 6;
52
+ var randomSuffix = () => {
53
+ const cryptoRef = globalThis.crypto;
54
+ if (cryptoRef?.getRandomValues) {
55
+ const bytes = new Uint8Array(SUFFIX_LENGTH);
56
+ cryptoRef.getRandomValues(bytes);
57
+ let suffix = "";
58
+ for (const byte of bytes) {
59
+ suffix += (byte % 36).toString(36);
60
+ }
61
+ return suffix;
62
+ }
63
+ return Math.random().toString(36).slice(2, 2 + SUFFIX_LENGTH).padEnd(SUFFIX_LENGTH, "0");
64
+ };
65
+ var getOrCreateTabSuffix = (session) => {
66
+ if (!session) {
67
+ return randomSuffix();
68
+ }
69
+ try {
70
+ const existing = session.getItem(TAB_SUFFIX_STORAGE_KEY);
71
+ if (existing && existing.length > 0) {
72
+ return existing;
73
+ }
74
+ const suffix = randomSuffix();
75
+ session.setItem(TAB_SUFFIX_STORAGE_KEY, suffix);
76
+ return suffix;
77
+ } catch {
78
+ return randomSuffix();
79
+ }
80
+ };
81
+ var isInstanceOf = (scope, ctorName) => {
82
+ const ctor = scope?.[ctorName];
83
+ return typeof ctor === "function" && scope instanceof ctor;
84
+ };
85
+ var CF_WORKER_USER_AGENT = "Cloudflare-Workers";
86
+ var inferEnvironmentName = (options = {}) => {
87
+ const scope = options.scope ?? globalThis;
88
+ if (scope.navigator?.userAgent === CF_WORKER_USER_AGENT) {
89
+ return `cf-worker::${randomSuffix()}`;
90
+ }
91
+ if (isInstanceOf(scope, "SharedWorkerGlobalScope")) {
92
+ return `shared-worker:${scope.name ?? ""}:${randomSuffix()}`;
93
+ }
94
+ if (isInstanceOf(scope, "ServiceWorkerGlobalScope")) {
95
+ return `service-worker::${randomSuffix()}`;
96
+ }
97
+ if (isInstanceOf(scope, "DedicatedWorkerGlobalScope")) {
98
+ return `dedicated-worker:${scope.name ?? ""}:${randomSuffix()}`;
99
+ }
100
+ if (scope.window !== void 0 && scope.window === scope) {
101
+ const origin = scope.location?.origin ?? "";
102
+ return `tab:${origin}:${getOrCreateTabSuffix(scope.sessionStorage)}`;
103
+ }
104
+ const proc = scope.process;
105
+ if (proc && typeof proc === "object" && proc.versions?.node) {
106
+ const pid = typeof proc.pid === "number" ? String(proc.pid) : "";
107
+ return `node:${pid}:${randomSuffix()}`;
108
+ }
109
+ return `unknown::${randomSuffix()}`;
110
+ };
111
+
112
+ // src/jsonl.ts
113
+ var serializeToJsonl = (entry, opts = {}) => {
114
+ if (entry.level <= LogLevel.TRACE) {
115
+ return void 0;
116
+ }
117
+ const { filename, line, context: scopeName } = entry.computedMeta;
118
+ const record = {
119
+ t: new Date(entry.timestamp).toISOString(),
120
+ l: shortLevelName[entry.level] ?? "?",
121
+ m: entry.message ?? ""
122
+ };
123
+ if (filename !== void 0) {
124
+ record.f = filename;
125
+ }
126
+ if (line !== void 0) {
127
+ record.n = line;
128
+ }
129
+ if (scopeName !== void 0) {
130
+ record.o = scopeName;
131
+ }
132
+ if (entry.computedError !== void 0) {
133
+ record.e = entry.computedError;
134
+ }
135
+ if (opts.env !== void 0) {
136
+ record.i = opts.env;
137
+ }
138
+ const computedContext = entry.computedContext;
139
+ if (Object.keys(computedContext).length > 0) {
140
+ try {
141
+ record.c = JSON.stringify(computedContext);
142
+ } catch {
143
+ }
144
+ }
145
+ try {
146
+ return JSON.stringify(record);
147
+ } catch {
148
+ return void 0;
149
+ }
150
+ };
151
+
47
152
  // src/decorators.ts
48
153
  import chalk from "chalk";
49
154
  import { inspect } from "@dxos/node-std/util";
@@ -166,8 +271,6 @@ var processors_exports = {};
166
271
  __export(processors_exports, {
167
272
  BROWSER_PROCESSOR: () => BROWSER_PROCESSOR,
168
273
  DEBUG_PROCESSOR: () => DEBUG_PROCESSOR,
169
- FILE_PROCESSOR: () => FILE_PROCESSOR,
170
- createFileProcessor: () => createFileProcessor,
171
274
  getRelativeFilename: () => getRelativeFilename
172
275
  });
173
276
 
@@ -287,71 +390,6 @@ var DEBUG_PROCESSOR = (config, entry) => {
287
390
  console.log(inspect2(entry, false, null, true));
288
391
  };
289
392
 
290
- // src/processors/file-processor.ts
291
- import { appendFileSync, mkdirSync, openSync } from "@dxos/node-std/fs";
292
- import { dirname } from "@dxos/node-std/path";
293
- var EAGAIN_MAX_DURATION = 1e3;
294
- var createFileProcessor = ({ pathOrFd, levels: levels2, filters }) => {
295
- let fd;
296
- return (config, entry) => {
297
- if (levels2.length > 0 && !levels2.includes(entry.level)) {
298
- return;
299
- }
300
- if (!shouldLog(entry, filters)) {
301
- return;
302
- }
303
- if (typeof pathOrFd === "number") {
304
- fd = pathOrFd;
305
- } else {
306
- try {
307
- mkdirSync(dirname(pathOrFd));
308
- } catch {
309
- }
310
- fd = openSync(pathOrFd, "a");
311
- }
312
- const record = {
313
- level: entry.level,
314
- message: entry.message,
315
- timestamp: entry.timestamp,
316
- meta: entry.computedMeta,
317
- context: entry.computedContext,
318
- error: entry.computedError
319
- };
320
- let retryTS = 0;
321
- while (true) {
322
- try {
323
- return appendFileSync(fd, JSON.stringify(record) + "\n");
324
- } catch (err) {
325
- if (err.code !== "EAGAIN") {
326
- throw err;
327
- }
328
- if (retryTS === 0) {
329
- retryTS = performance.now();
330
- } else {
331
- if (performance.now() - retryTS > EAGAIN_MAX_DURATION) {
332
- console.log(`could not write after ${EAGAIN_MAX_DURATION}ms of EAGAIN failures, giving up`);
333
- throw err;
334
- }
335
- }
336
- }
337
- }
338
- };
339
- };
340
- var logFilePath;
341
- var getLogFilePath = () => {
342
- logFilePath ??= process.env.LOG_FILE ?? (process.env.HOME ? `${process.env.HOME}/.dxlog/${(/* @__PURE__ */ new Date()).toISOString()}.log` : void 0);
343
- return logFilePath;
344
- };
345
- var FILE_PROCESSOR = createFileProcessor({
346
- pathOrFd: getLogFilePath(),
347
- levels: [
348
- LogLevel.ERROR,
349
- LogLevel.WARN,
350
- LogLevel.INFO,
351
- LogLevel.TRACE
352
- ]
353
- });
354
-
355
393
  // src/options.ts
356
394
  var processors = {
357
395
  [LogProcessorType.CONSOLE]: processors_exports.CONSOLE_PROCESSOR,
@@ -486,6 +524,12 @@ var getFormattedStackTrace = () => new Error().stack.split("\n").slice(3).join("
486
524
  // src/index.ts
487
525
  __reExport(index_exports, processors_exports);
488
526
 
527
+ // src/meta.ts
528
+ var LOG_META_MARKER = "~LogMeta";
529
+ var isLogMeta = (value) => {
530
+ return value != null && typeof value === "object" && value[LOG_META_MARKER] === LOG_META_MARKER;
531
+ };
532
+
489
533
  // src/dbg.ts
490
534
  var dbg = (arg, meta) => {
491
535
  if (meta?.A) {
@@ -596,12 +640,11 @@ var getCurrentOwnershipScope = (thisRef) => thisRef;
596
640
  export {
597
641
  BROWSER_PROCESSOR,
598
642
  DEBUG_PROCESSOR,
599
- FILE_PROCESSOR,
643
+ LOG_META_MARKER,
600
644
  LogBuffer,
601
645
  LogEntry,
602
646
  LogLevel,
603
647
  LogProcessorType,
604
- createFileProcessor,
605
648
  createLog,
606
649
  dbg,
607
650
  debug,
@@ -609,12 +652,15 @@ export {
609
652
  getContextFromEntry,
610
653
  getCurrentOwnershipScope,
611
654
  getRelativeFilename,
655
+ inferEnvironmentName,
656
+ isLogMeta,
612
657
  levels,
613
658
  log,
614
659
  logInfo,
615
660
  omit,
616
661
  parseFilter,
617
662
  pick,
663
+ serializeToJsonl,
618
664
  shortLevelName,
619
665
  shouldLog
620
666
  };