@dxos/log 0.8.4-main.fdfb99ef29 → 0.8.4-staging.60fe92afc8

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,76 @@
1
+ import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
+ import {
3
+ LogLevel,
4
+ shouldLog
5
+ } from "../chunk-5TBDXMQF.mjs";
6
+ import "../chunk-2SZHAWBN.mjs";
7
+
8
+ // src/processors/file-processor.ts
9
+ import { appendFileSync, mkdirSync, openSync } from "node:fs";
10
+ import { dirname } from "node:path";
11
+ var EAGAIN_MAX_DURATION = 1e3;
12
+ var createFileProcessor = ({ pathOrFd, levels, filters }) => {
13
+ let fd;
14
+ return (config, entry) => {
15
+ if (levels.length > 0 && !levels.includes(entry.level)) {
16
+ return;
17
+ }
18
+ if (!shouldLog(entry, filters)) {
19
+ return;
20
+ }
21
+ if (typeof pathOrFd === "number") {
22
+ fd = pathOrFd;
23
+ } else {
24
+ try {
25
+ mkdirSync(dirname(pathOrFd));
26
+ } catch {
27
+ }
28
+ fd = openSync(pathOrFd, "a");
29
+ }
30
+ const record = {
31
+ level: entry.level,
32
+ message: entry.message,
33
+ timestamp: entry.timestamp,
34
+ meta: entry.computedMeta,
35
+ context: entry.computedContext,
36
+ error: entry.computedError
37
+ };
38
+ let retryTS = 0;
39
+ while (true) {
40
+ try {
41
+ return appendFileSync(fd, JSON.stringify(record) + "\n");
42
+ } catch (err) {
43
+ if (err.code !== "EAGAIN") {
44
+ throw err;
45
+ }
46
+ if (retryTS === 0) {
47
+ retryTS = performance.now();
48
+ } else {
49
+ if (performance.now() - retryTS > EAGAIN_MAX_DURATION) {
50
+ console.log(`could not write after ${EAGAIN_MAX_DURATION}ms of EAGAIN failures, giving up`);
51
+ throw err;
52
+ }
53
+ }
54
+ }
55
+ }
56
+ };
57
+ };
58
+ var logFilePath;
59
+ var getLogFilePath = () => {
60
+ logFilePath ??= process.env.LOG_FILE ?? (process.env.HOME ? `${process.env.HOME}/.dxlog/${(/* @__PURE__ */ new Date()).toISOString()}.log` : void 0);
61
+ return logFilePath;
62
+ };
63
+ var FILE_PROCESSOR = createFileProcessor({
64
+ pathOrFd: getLogFilePath(),
65
+ levels: [
66
+ LogLevel.ERROR,
67
+ LogLevel.WARN,
68
+ LogLevel.INFO,
69
+ LogLevel.TRACE
70
+ ]
71
+ });
72
+ export {
73
+ FILE_PROCESSOR,
74
+ createFileProcessor
75
+ };
76
+ //# sourceMappingURL=file-processor.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../src/processors/file-processor.ts"],
4
+ "sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { appendFileSync, mkdirSync, openSync } from 'node:fs';\nimport { dirname } from 'node:path';\n\nimport { type LogFilter, LogLevel } from '../config';\nimport { type LogProcessor, shouldLog } from '../context';\n\n// Amount of time to retry writing after encountering EAGAIN before giving up.\nconst EAGAIN_MAX_DURATION = 1000;\n\n/**\n * Create a file processor.\n * @param path - Path to log file to create or append to, or existing open file descriptor e.g. stdout.\n * @param levels - Log levels to process. Takes preference over Filters.\n * @param filters - Filters to apply.\n */\nexport const createFileProcessor = ({\n pathOrFd,\n levels,\n filters,\n}: {\n pathOrFd: string | number;\n levels: LogLevel[];\n filters?: LogFilter[];\n}): LogProcessor => {\n let fd: number | undefined;\n\n return (config, entry) => {\n if (levels.length > 0 && !levels.includes(entry.level)) {\n return;\n }\n if (!shouldLog(entry, filters)) {\n return;\n }\n\n if (typeof pathOrFd === 'number') {\n fd = pathOrFd;\n } else {\n try {\n mkdirSync(dirname(pathOrFd));\n } catch {}\n fd = openSync(pathOrFd, 'a');\n }\n\n const record = {\n level: entry.level,\n message: entry.message,\n timestamp: entry.timestamp,\n meta: entry.computedMeta,\n context: entry.computedContext,\n error: entry.computedError,\n };\n let retryTS: number = 0;\n\n // Retry writing if EAGAIN is encountered.\n //\n // Node may set stdout and stderr to non-blocking. https://github.com/nodejs/node/issues/42826\n // This can cause EAGAIN errors when writing to them.\n // In order to not drop logs, make log methods asynchronous, or deal with buffering/delayed writes, spin until write succeeds.\n\n while (true) {\n try {\n return appendFileSync(fd, JSON.stringify(record) + '\\n');\n } catch (err: any) {\n if (err.code !== 'EAGAIN') {\n throw err;\n }\n if (retryTS === 0) {\n retryTS = performance.now();\n } else {\n if (performance.now() - retryTS > EAGAIN_MAX_DURATION) {\n console.log(`could not write after ${EAGAIN_MAX_DURATION}ms of EAGAIN failures, giving up`);\n throw err;\n }\n }\n }\n }\n };\n};\n\nlet logFilePath: string | undefined;\nconst getLogFilePath = () => {\n logFilePath ??=\n process.env.LOG_FILE ??\n (process.env.HOME ? `${process.env.HOME}/.dxlog/${new Date().toISOString()}.log` : undefined);\n\n return logFilePath!;\n};\n\nexport const FILE_PROCESSOR: LogProcessor = createFileProcessor({\n pathOrFd: getLogFilePath(),\n levels: [LogLevel.ERROR, LogLevel.WARN, LogLevel.INFO, LogLevel.TRACE],\n});\n"],
5
+ "mappings": ";;;;;;;;AAIA,SAASA,gBAAgBC,WAAWC,gBAAgB;AACpD,SAASC,eAAe;AAMxB,IAAMC,sBAAsB;AAQrB,IAAMC,sBAAsB,CAAC,EAClCC,UACAC,QACAC,QAAO,MAKR;AACC,MAAIC;AAEJ,SAAO,CAACC,QAAQC,UAAAA;AACd,QAAIJ,OAAOK,SAAS,KAAK,CAACL,OAAOM,SAASF,MAAMG,KAAK,GAAG;AACtD;IACF;AACA,QAAI,CAACC,UAAUJ,OAAOH,OAAAA,GAAU;AAC9B;IACF;AAEA,QAAI,OAAOF,aAAa,UAAU;AAChCG,WAAKH;IACP,OAAO;AACL,UAAI;AACFU,kBAAUC,QAAQX,QAAAA,CAAAA;MACpB,QAAQ;MAAC;AACTG,WAAKS,SAASZ,UAAU,GAAA;IAC1B;AAEA,UAAMa,SAAS;MACbL,OAAOH,MAAMG;MACbM,SAAST,MAAMS;MACfC,WAAWV,MAAMU;MACjBC,MAAMX,MAAMY;MACZC,SAASb,MAAMc;MACfC,OAAOf,MAAMgB;IACf;AACA,QAAIC,UAAkB;AAQtB,WAAO,MAAM;AACX,UAAI;AACF,eAAOC,eAAepB,IAAIqB,KAAKC,UAAUZ,MAAAA,IAAU,IAAA;MACrD,SAASa,KAAU;AACjB,YAAIA,IAAIC,SAAS,UAAU;AACzB,gBAAMD;QACR;AACA,YAAIJ,YAAY,GAAG;AACjBA,oBAAUM,YAAYC,IAAG;QAC3B,OAAO;AACL,cAAID,YAAYC,IAAG,IAAKP,UAAUxB,qBAAqB;AACrDgC,oBAAQC,IAAI,yBAAyBjC,mBAAAA,kCAAqD;AAC1F,kBAAM4B;UACR;QACF;MACF;IACF;EACF;AACF;AAEA,IAAIM;AACJ,IAAMC,iBAAiB,MAAA;AACrBD,kBACEE,QAAQC,IAAIC,aACXF,QAAQC,IAAIE,OAAO,GAAGH,QAAQC,IAAIE,IAAI,YAAW,oBAAIC,KAAAA,GAAOC,YAAW,CAAA,SAAWC;AAErF,SAAOR;AACT;AAEO,IAAMS,iBAA+B1C,oBAAoB;EAC9DC,UAAUiC,eAAAA;EACVhC,QAAQ;IAACyC,SAASC;IAAOD,SAASE;IAAMF,SAASG;IAAMH,SAASI;;AAClE,CAAA;",
6
+ "names": ["appendFileSync", "mkdirSync", "openSync", "dirname", "EAGAIN_MAX_DURATION", "createFileProcessor", "pathOrFd", "levels", "filters", "fd", "config", "entry", "length", "includes", "level", "shouldLog", "mkdirSync", "dirname", "openSync", "record", "message", "timestamp", "meta", "computedMeta", "context", "computedContext", "error", "computedError", "retryTS", "appendFileSync", "JSON", "stringify", "err", "code", "performance", "now", "console", "log", "logFilePath", "getLogFilePath", "process", "env", "LOG_FILE", "HOME", "Date", "toISOString", "undefined", "FILE_PROCESSOR", "LogLevel", "ERROR", "WARN", "INFO", "TRACE"]
7
+ }
@@ -2,5 +2,4 @@ export * from './browser-processor';
2
2
  export * from './common';
3
3
  export * from '#console-processor';
4
4
  export * from './debug-processor';
5
- export * from './file-processor';
6
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/processors/index.ts"],"names":[],"mappings":"AAIA,cAAc,qBAAqB,CAAC;AACpC,cAAc,UAAU,CAAC;AACzB,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/processors/index.ts"],"names":[],"mappings":"AAIA,cAAc,qBAAqB,CAAC;AACpC,cAAc,UAAU,CAAC;AACzB,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC"}