@logtape/file 0.9.0-dev.1
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.
- package/LICENSE +20 -0
- package/README.md +43 -0
- package/esm/_dnt.shims.js +57 -0
- package/esm/file/filesink.base.js +78 -0
- package/esm/file/filesink.node.js +47 -0
- package/esm/file/mod.js +1 -0
- package/esm/logtape/config.js +287 -0
- package/esm/logtape/context.js +23 -0
- package/esm/logtape/filter.js +42 -0
- package/esm/logtape/formatter.js +261 -0
- package/esm/logtape/level.js +59 -0
- package/esm/logtape/logger.js +480 -0
- package/esm/logtape/mod.js +8 -0
- package/esm/logtape/nodeUtil.js +2 -0
- package/esm/logtape/record.js +1 -0
- package/esm/logtape/sink.js +96 -0
- package/esm/package.json +3 -0
- package/package.json +55 -0
- package/script/_dnt.shims.js +60 -0
- package/script/file/filesink.base.js +82 -0
- package/script/file/filesink.node.js +55 -0
- package/script/file/mod.js +6 -0
- package/script/logtape/config.js +321 -0
- package/script/logtape/context.js +26 -0
- package/script/logtape/filter.js +46 -0
- package/script/logtape/formatter.js +270 -0
- package/script/logtape/level.js +64 -0
- package/script/logtape/logger.js +511 -0
- package/script/logtape/mod.js +34 -0
- package/script/logtape/nodeUtil.js +7 -0
- package/script/logtape/record.js +2 -0
- package/script/logtape/sink.js +101 -0
- package/script/package.json +3 -0
- package/types/_dnt.shims.d.ts +2 -0
- package/types/_dnt.shims.d.ts.map +1 -0
- package/types/_dnt.test_shims.d.ts.map +1 -0
- package/types/deps/jsr.io/@david/which-runtime/0.2.1/mod.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/0.222.1/_constants.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/0.222.1/_diff.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/0.222.1/_format.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/0.222.1/assert_equals.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/0.222.1/assertion_error.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/0.222.1/equal.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/fmt/0.222.1/colors.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/path/1.0.8/_common/assert_path.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/path/1.0.8/_common/constants.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/path/1.0.8/_common/normalize.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/path/1.0.8/_common/normalize_string.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/path/1.0.8/_os.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/path/1.0.8/join.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/path/1.0.8/posix/_util.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/path/1.0.8/posix/join.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/path/1.0.8/posix/normalize.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/path/1.0.8/windows/_util.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/path/1.0.8/windows/join.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/path/1.0.8/windows/normalize.d.ts.map +1 -0
- package/types/file/filesink.base.d.ts +89 -0
- package/types/file/filesink.base.d.ts.map +1 -0
- package/types/file/filesink.node.d.ts +34 -0
- package/types/file/filesink.node.d.ts.map +1 -0
- package/types/file/filesink.test.d.ts.map +1 -0
- package/types/file/mod.d.ts +3 -0
- package/types/file/mod.d.ts.map +1 -0
- package/types/logtape/config.d.ts +183 -0
- package/types/logtape/config.d.ts.map +1 -0
- package/types/logtape/context.d.ts +35 -0
- package/types/logtape/context.d.ts.map +1 -0
- package/types/logtape/filter.d.ts +31 -0
- package/types/logtape/filter.d.ts.map +1 -0
- package/types/logtape/fixtures.d.ts.map +1 -0
- package/types/logtape/formatter.d.ts +260 -0
- package/types/logtape/formatter.d.ts.map +1 -0
- package/types/logtape/level.d.ts +32 -0
- package/types/logtape/level.d.ts.map +1 -0
- package/types/logtape/logger.d.ts +423 -0
- package/types/logtape/logger.d.ts.map +1 -0
- package/types/logtape/mod.d.ts +9 -0
- package/types/logtape/mod.d.ts.map +1 -0
- package/types/logtape/nodeUtil.d.ts +3 -0
- package/types/logtape/nodeUtil.d.ts.map +1 -0
- package/types/logtape/record.d.ts +44 -0
- package/types/logtape/record.d.ts.map +1 -0
- package/types/logtape/sink.d.ts +108 -0
- package/types/logtape/sink.d.ts.map +1 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.dntGlobalThis = void 0;
|
|
4
|
+
const dntGlobals = {};
|
|
5
|
+
exports.dntGlobalThis = createMergeProxy(globalThis, dntGlobals);
|
|
6
|
+
function createMergeProxy(baseObj, extObj) {
|
|
7
|
+
return new Proxy(baseObj, {
|
|
8
|
+
get(_target, prop, _receiver) {
|
|
9
|
+
if (prop in extObj) {
|
|
10
|
+
return extObj[prop];
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
return baseObj[prop];
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
set(_target, prop, value) {
|
|
17
|
+
if (prop in extObj) {
|
|
18
|
+
delete extObj[prop];
|
|
19
|
+
}
|
|
20
|
+
baseObj[prop] = value;
|
|
21
|
+
return true;
|
|
22
|
+
},
|
|
23
|
+
deleteProperty(_target, prop) {
|
|
24
|
+
let success = false;
|
|
25
|
+
if (prop in extObj) {
|
|
26
|
+
delete extObj[prop];
|
|
27
|
+
success = true;
|
|
28
|
+
}
|
|
29
|
+
if (prop in baseObj) {
|
|
30
|
+
delete baseObj[prop];
|
|
31
|
+
success = true;
|
|
32
|
+
}
|
|
33
|
+
return success;
|
|
34
|
+
},
|
|
35
|
+
ownKeys(_target) {
|
|
36
|
+
const baseKeys = Reflect.ownKeys(baseObj);
|
|
37
|
+
const extKeys = Reflect.ownKeys(extObj);
|
|
38
|
+
const extKeysSet = new Set(extKeys);
|
|
39
|
+
return [...baseKeys.filter((k) => !extKeysSet.has(k)), ...extKeys];
|
|
40
|
+
},
|
|
41
|
+
defineProperty(_target, prop, desc) {
|
|
42
|
+
if (prop in extObj) {
|
|
43
|
+
delete extObj[prop];
|
|
44
|
+
}
|
|
45
|
+
Reflect.defineProperty(baseObj, prop, desc);
|
|
46
|
+
return true;
|
|
47
|
+
},
|
|
48
|
+
getOwnPropertyDescriptor(_target, prop) {
|
|
49
|
+
if (prop in extObj) {
|
|
50
|
+
return Reflect.getOwnPropertyDescriptor(extObj, prop);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
return Reflect.getOwnPropertyDescriptor(baseObj, prop);
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
has(_target, prop) {
|
|
57
|
+
return prop in extObj || prop in baseObj;
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getBaseFileSink = getBaseFileSink;
|
|
4
|
+
exports.getBaseRotatingFileSink = getBaseRotatingFileSink;
|
|
5
|
+
const mod_js_1 = require("../logtape/mod.js");
|
|
6
|
+
/**
|
|
7
|
+
* Get a platform-independent file sink.
|
|
8
|
+
*
|
|
9
|
+
* @typeParam TFile The type of the file descriptor.
|
|
10
|
+
* @param path A path to the file to write to.
|
|
11
|
+
* @param options The options for the sink and the file driver.
|
|
12
|
+
* @returns A sink that writes to the file. The sink is also a disposable
|
|
13
|
+
* object that closes the file when disposed.
|
|
14
|
+
*/
|
|
15
|
+
function getBaseFileSink(path, options) {
|
|
16
|
+
const formatter = options.formatter ?? mod_js_1.defaultTextFormatter;
|
|
17
|
+
const encoder = options.encoder ?? new TextEncoder();
|
|
18
|
+
const fd = options.openSync(path);
|
|
19
|
+
const sink = (record) => {
|
|
20
|
+
options.writeSync(fd, encoder.encode(formatter(record)));
|
|
21
|
+
options.flushSync(fd);
|
|
22
|
+
};
|
|
23
|
+
sink[Symbol.dispose] = () => options.closeSync(fd);
|
|
24
|
+
return sink;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Get a platform-independent rotating file sink.
|
|
28
|
+
*
|
|
29
|
+
* This sink writes log records to a file, and rotates the file when it reaches
|
|
30
|
+
* the `maxSize`. The rotated files are named with the original file name
|
|
31
|
+
* followed by a dot and a number, starting from 1. The number is incremented
|
|
32
|
+
* for each rotation, and the maximum number of files to keep is `maxFiles`.
|
|
33
|
+
*
|
|
34
|
+
* @param path A path to the file to write to.
|
|
35
|
+
* @param options The options for the sink and the file driver.
|
|
36
|
+
* @returns A sink that writes to the file. The sink is also a disposable
|
|
37
|
+
* object that closes the file when disposed.
|
|
38
|
+
*/
|
|
39
|
+
function getBaseRotatingFileSink(path, options) {
|
|
40
|
+
const formatter = options.formatter ?? mod_js_1.defaultTextFormatter;
|
|
41
|
+
const encoder = options.encoder ?? new TextEncoder();
|
|
42
|
+
const maxSize = options.maxSize ?? 1024 * 1024;
|
|
43
|
+
const maxFiles = options.maxFiles ?? 5;
|
|
44
|
+
let offset = 0;
|
|
45
|
+
try {
|
|
46
|
+
const stat = options.statSync(path);
|
|
47
|
+
offset = stat.size;
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
// Continue as the offset is already 0.
|
|
51
|
+
}
|
|
52
|
+
let fd = options.openSync(path);
|
|
53
|
+
function shouldRollover(bytes) {
|
|
54
|
+
return offset + bytes.length > maxSize;
|
|
55
|
+
}
|
|
56
|
+
function performRollover() {
|
|
57
|
+
options.closeSync(fd);
|
|
58
|
+
for (let i = maxFiles - 1; i > 0; i--) {
|
|
59
|
+
const oldPath = `${path}.${i}`;
|
|
60
|
+
const newPath = `${path}.${i + 1}`;
|
|
61
|
+
try {
|
|
62
|
+
options.renameSync(oldPath, newPath);
|
|
63
|
+
}
|
|
64
|
+
catch (_) {
|
|
65
|
+
// Continue if the file does not exist.
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
options.renameSync(path, `${path}.1`);
|
|
69
|
+
offset = 0;
|
|
70
|
+
fd = options.openSync(path);
|
|
71
|
+
}
|
|
72
|
+
const sink = (record) => {
|
|
73
|
+
const bytes = encoder.encode(formatter(record));
|
|
74
|
+
if (shouldRollover(bytes))
|
|
75
|
+
performRollover();
|
|
76
|
+
options.writeSync(fd, bytes);
|
|
77
|
+
options.flushSync(fd);
|
|
78
|
+
offset += bytes.length;
|
|
79
|
+
};
|
|
80
|
+
sink[Symbol.dispose] = () => options.closeSync(fd);
|
|
81
|
+
return sink;
|
|
82
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.nodeDriver = void 0;
|
|
7
|
+
exports.getFileSink = getFileSink;
|
|
8
|
+
exports.getRotatingFileSink = getRotatingFileSink;
|
|
9
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
10
|
+
const filesink_base_js_1 = require("./filesink.base.js");
|
|
11
|
+
/**
|
|
12
|
+
* A Node.js-specific file sink driver.
|
|
13
|
+
*/
|
|
14
|
+
exports.nodeDriver = {
|
|
15
|
+
openSync(path) {
|
|
16
|
+
return node_fs_1.default.openSync(path, "a");
|
|
17
|
+
},
|
|
18
|
+
writeSync: node_fs_1.default.writeSync,
|
|
19
|
+
flushSync: node_fs_1.default.fsyncSync,
|
|
20
|
+
closeSync: node_fs_1.default.closeSync,
|
|
21
|
+
statSync: node_fs_1.default.statSync,
|
|
22
|
+
renameSync: node_fs_1.default.renameSync,
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Get a file sink.
|
|
26
|
+
*
|
|
27
|
+
* Note that this function is unavailable in the browser.
|
|
28
|
+
*
|
|
29
|
+
* @param path A path to the file to write to.
|
|
30
|
+
* @param options The options for the sink.
|
|
31
|
+
* @returns A sink that writes to the file. The sink is also a disposable
|
|
32
|
+
* object that closes the file when disposed.
|
|
33
|
+
*/
|
|
34
|
+
function getFileSink(path, options = {}) {
|
|
35
|
+
return (0, filesink_base_js_1.getBaseFileSink)(path, { ...options, ...exports.nodeDriver });
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Get a rotating file sink.
|
|
39
|
+
*
|
|
40
|
+
* This sink writes log records to a file, and rotates the file when it reaches
|
|
41
|
+
* the `maxSize`. The rotated files are named with the original file name
|
|
42
|
+
* followed by a dot and a number, starting from 1. The number is incremented
|
|
43
|
+
* for each rotation, and the maximum number of files to keep is `maxFiles`.
|
|
44
|
+
*
|
|
45
|
+
* Note that this function is unavailable in the browser.
|
|
46
|
+
*
|
|
47
|
+
* @param path A path to the file to write to.
|
|
48
|
+
* @param options The options for the sink and the file driver.
|
|
49
|
+
* @returns A sink that writes to the file. The sink is also a disposable
|
|
50
|
+
* object that closes the file when disposed.
|
|
51
|
+
*/
|
|
52
|
+
function getRotatingFileSink(path, options = {}) {
|
|
53
|
+
return (0, filesink_base_js_1.getBaseRotatingFileSink)(path, { ...options, ...exports.nodeDriver });
|
|
54
|
+
}
|
|
55
|
+
// cSpell: ignore filesink
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getRotatingFileSink = exports.getFileSink = void 0;
|
|
4
|
+
var filesink_node_js_1 = require("./filesink.node.js");
|
|
5
|
+
Object.defineProperty(exports, "getFileSink", { enumerable: true, get: function () { return filesink_node_js_1.getFileSink; } });
|
|
6
|
+
Object.defineProperty(exports, "getRotatingFileSink", { enumerable: true, get: function () { return filesink_node_js_1.getRotatingFileSink; } });
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.ConfigError = void 0;
|
|
27
|
+
exports.configure = configure;
|
|
28
|
+
exports.configureSync = configureSync;
|
|
29
|
+
exports.getConfig = getConfig;
|
|
30
|
+
exports.reset = reset;
|
|
31
|
+
exports.resetSync = resetSync;
|
|
32
|
+
exports.dispose = dispose;
|
|
33
|
+
exports.disposeSync = disposeSync;
|
|
34
|
+
const dntShim = __importStar(require("../_dnt.shims.js"));
|
|
35
|
+
const filter_js_1 = require("./filter.js");
|
|
36
|
+
const logger_js_1 = require("./logger.js");
|
|
37
|
+
const sink_js_1 = require("./sink.js");
|
|
38
|
+
/**
|
|
39
|
+
* The current configuration, if any. Otherwise, `null`.
|
|
40
|
+
*/
|
|
41
|
+
let currentConfig = null;
|
|
42
|
+
/**
|
|
43
|
+
* Strong references to the loggers.
|
|
44
|
+
* This is to prevent the loggers from being garbage collected so that their
|
|
45
|
+
* sinks and filters are not removed.
|
|
46
|
+
*/
|
|
47
|
+
const strongRefs = new Set();
|
|
48
|
+
/**
|
|
49
|
+
* Disposables to dispose when resetting the configuration.
|
|
50
|
+
*/
|
|
51
|
+
const disposables = new Set();
|
|
52
|
+
/**
|
|
53
|
+
* Async disposables to dispose when resetting the configuration.
|
|
54
|
+
*/
|
|
55
|
+
const asyncDisposables = new Set();
|
|
56
|
+
/**
|
|
57
|
+
* Check if a config is for the meta logger.
|
|
58
|
+
*/
|
|
59
|
+
function isLoggerConfigMeta(cfg) {
|
|
60
|
+
return cfg.category.length === 0 ||
|
|
61
|
+
(cfg.category.length === 1 && cfg.category[0] === "logtape") ||
|
|
62
|
+
(cfg.category.length === 2 &&
|
|
63
|
+
cfg.category[0] === "logtape" &&
|
|
64
|
+
cfg.category[1] === "meta");
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Configure the loggers with the specified configuration.
|
|
68
|
+
*
|
|
69
|
+
* Note that if the given sinks or filters are disposable, they will be
|
|
70
|
+
* disposed when the configuration is reset, or when the process exits.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* await configure({
|
|
75
|
+
* sinks: {
|
|
76
|
+
* console: getConsoleSink(),
|
|
77
|
+
* },
|
|
78
|
+
* filters: {
|
|
79
|
+
* slow: (log) =>
|
|
80
|
+
* "duration" in log.properties &&
|
|
81
|
+
* log.properties.duration as number > 1000,
|
|
82
|
+
* },
|
|
83
|
+
* loggers: [
|
|
84
|
+
* {
|
|
85
|
+
* category: "my-app",
|
|
86
|
+
* sinks: ["console"],
|
|
87
|
+
* level: "info",
|
|
88
|
+
* },
|
|
89
|
+
* {
|
|
90
|
+
* category: ["my-app", "sql"],
|
|
91
|
+
* filters: ["slow"],
|
|
92
|
+
* level: "debug",
|
|
93
|
+
* },
|
|
94
|
+
* {
|
|
95
|
+
* category: "logtape",
|
|
96
|
+
* sinks: ["console"],
|
|
97
|
+
* level: "error",
|
|
98
|
+
* },
|
|
99
|
+
* ],
|
|
100
|
+
* });
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* @param config The configuration.
|
|
104
|
+
*/
|
|
105
|
+
async function configure(config) {
|
|
106
|
+
if (currentConfig != null && !config.reset) {
|
|
107
|
+
throw new ConfigError("Already configured; if you want to reset, turn on the reset flag.");
|
|
108
|
+
}
|
|
109
|
+
await reset();
|
|
110
|
+
try {
|
|
111
|
+
configureInternal(config, true);
|
|
112
|
+
}
|
|
113
|
+
catch (e) {
|
|
114
|
+
if (e instanceof ConfigError)
|
|
115
|
+
await reset();
|
|
116
|
+
throw e;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Configure sync loggers with the specified configuration.
|
|
121
|
+
*
|
|
122
|
+
* Note that if the given sinks or filters are disposable, they will be
|
|
123
|
+
* disposed when the configuration is reset, or when the process exits.
|
|
124
|
+
*
|
|
125
|
+
* Also note that passing async sinks or filters will throw. If
|
|
126
|
+
* necessary use {@link resetSync} or {@link disposeSync}.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* configureSync({
|
|
131
|
+
* sinks: {
|
|
132
|
+
* console: getConsoleSink(),
|
|
133
|
+
* },
|
|
134
|
+
* loggers: [
|
|
135
|
+
* {
|
|
136
|
+
* category: "my-app",
|
|
137
|
+
* sinks: ["console"],
|
|
138
|
+
* level: "info",
|
|
139
|
+
* },
|
|
140
|
+
* {
|
|
141
|
+
* category: "logtape",
|
|
142
|
+
* sinks: ["console"],
|
|
143
|
+
* level: "error",
|
|
144
|
+
* },
|
|
145
|
+
* ],
|
|
146
|
+
* });
|
|
147
|
+
* ```
|
|
148
|
+
*
|
|
149
|
+
* @param config The configuration.
|
|
150
|
+
* @since 0.9.0
|
|
151
|
+
*/
|
|
152
|
+
function configureSync(config) {
|
|
153
|
+
if (currentConfig != null && !config.reset) {
|
|
154
|
+
throw new ConfigError("Already configured; if you want to reset, turn on the reset flag.");
|
|
155
|
+
}
|
|
156
|
+
if (asyncDisposables.size > 0) {
|
|
157
|
+
throw new ConfigError("Previously configured async disposables are still active. " +
|
|
158
|
+
"Use configure() instead or explicitly dispose them using dispose().");
|
|
159
|
+
}
|
|
160
|
+
resetSync();
|
|
161
|
+
try {
|
|
162
|
+
configureInternal(config, false);
|
|
163
|
+
}
|
|
164
|
+
catch (e) {
|
|
165
|
+
if (e instanceof ConfigError)
|
|
166
|
+
resetSync();
|
|
167
|
+
throw e;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
function configureInternal(config, allowAsync) {
|
|
171
|
+
currentConfig = config;
|
|
172
|
+
let metaConfigured = false;
|
|
173
|
+
let levelUsed = false;
|
|
174
|
+
for (const cfg of config.loggers) {
|
|
175
|
+
if (isLoggerConfigMeta(cfg)) {
|
|
176
|
+
metaConfigured = true;
|
|
177
|
+
}
|
|
178
|
+
const logger = logger_js_1.LoggerImpl.getLogger(cfg.category);
|
|
179
|
+
for (const sinkId of cfg.sinks ?? []) {
|
|
180
|
+
const sink = config.sinks[sinkId];
|
|
181
|
+
if (!sink) {
|
|
182
|
+
throw new ConfigError(`Sink not found: ${sinkId}.`);
|
|
183
|
+
}
|
|
184
|
+
logger.sinks.push(sink);
|
|
185
|
+
}
|
|
186
|
+
logger.parentSinks = cfg.parentSinks ?? "inherit";
|
|
187
|
+
if (cfg.lowestLevel !== undefined) {
|
|
188
|
+
logger.lowestLevel = cfg.lowestLevel;
|
|
189
|
+
}
|
|
190
|
+
if (cfg.level !== undefined) {
|
|
191
|
+
levelUsed = true;
|
|
192
|
+
logger.filters.push((0, filter_js_1.toFilter)(cfg.level));
|
|
193
|
+
}
|
|
194
|
+
for (const filterId of cfg.filters ?? []) {
|
|
195
|
+
const filter = config.filters?.[filterId];
|
|
196
|
+
if (filter === undefined) {
|
|
197
|
+
throw new ConfigError(`Filter not found: ${filterId}.`);
|
|
198
|
+
}
|
|
199
|
+
logger.filters.push((0, filter_js_1.toFilter)(filter));
|
|
200
|
+
}
|
|
201
|
+
strongRefs.add(logger);
|
|
202
|
+
}
|
|
203
|
+
logger_js_1.LoggerImpl.getLogger().contextLocalStorage = config.contextLocalStorage;
|
|
204
|
+
for (const sink of Object.values(config.sinks)) {
|
|
205
|
+
if (Symbol.asyncDispose in sink) {
|
|
206
|
+
if (allowAsync)
|
|
207
|
+
asyncDisposables.add(sink);
|
|
208
|
+
else {
|
|
209
|
+
throw new ConfigError("Async disposables cannot be used with configureSync().");
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (Symbol.dispose in sink)
|
|
213
|
+
disposables.add(sink);
|
|
214
|
+
}
|
|
215
|
+
for (const filter of Object.values(config.filters ?? {})) {
|
|
216
|
+
if (filter == null || typeof filter === "string")
|
|
217
|
+
continue;
|
|
218
|
+
if (Symbol.asyncDispose in filter) {
|
|
219
|
+
if (allowAsync)
|
|
220
|
+
asyncDisposables.add(filter);
|
|
221
|
+
else {
|
|
222
|
+
throw new ConfigError("Async disposables cannot be used with configureSync().");
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
if (Symbol.dispose in filter)
|
|
226
|
+
disposables.add(filter);
|
|
227
|
+
}
|
|
228
|
+
if ("process" in dntShim.dntGlobalThis && !("Deno" in dntShim.dntGlobalThis)) {
|
|
229
|
+
// @ts-ignore: It's fine to use process in Node
|
|
230
|
+
// deno-lint-ignore no-process-global
|
|
231
|
+
process.on("exit", allowAsync ? dispose : disposeSync);
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
// @ts-ignore: It's fine to addEventListener() on the browser/Deno
|
|
235
|
+
addEventListener("unload", allowAsync ? dispose : disposeSync);
|
|
236
|
+
}
|
|
237
|
+
const meta = logger_js_1.LoggerImpl.getLogger(["logtape", "meta"]);
|
|
238
|
+
if (!metaConfigured) {
|
|
239
|
+
meta.sinks.push((0, sink_js_1.getConsoleSink)());
|
|
240
|
+
}
|
|
241
|
+
meta.info("LogTape loggers are configured. Note that LogTape itself uses the meta " +
|
|
242
|
+
"logger, which has category {metaLoggerCategory}. The meta logger " +
|
|
243
|
+
"purposes to log internal errors such as sink exceptions. If you " +
|
|
244
|
+
"are seeing this message, the meta logger is automatically configured. " +
|
|
245
|
+
"It's recommended to configure the meta logger with a separate sink " +
|
|
246
|
+
"so that you can easily notice if logging itself fails or is " +
|
|
247
|
+
"misconfigured. To turn off this message, configure the meta logger " +
|
|
248
|
+
"with higher log levels than {dismissLevel}. See also " +
|
|
249
|
+
"<https://logtape.org/manual/categories#meta-logger>.", { metaLoggerCategory: ["logtape", "meta"], dismissLevel: "info" });
|
|
250
|
+
if (levelUsed) {
|
|
251
|
+
meta.warn("The level option is deprecated in favor of lowestLevel option. " +
|
|
252
|
+
"Please update your configuration. See also " +
|
|
253
|
+
"<https://logtape.org/manual/levels#configuring-severity-levels>.");
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Get the current configuration, if any. Otherwise, `null`.
|
|
258
|
+
* @returns The current configuration, if any. Otherwise, `null`.
|
|
259
|
+
*/
|
|
260
|
+
function getConfig() {
|
|
261
|
+
return currentConfig;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Reset the configuration. Mostly for testing purposes.
|
|
265
|
+
*/
|
|
266
|
+
async function reset() {
|
|
267
|
+
await dispose();
|
|
268
|
+
resetInternal();
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Reset the configuration. Mostly for testing purposes. Will not clear async
|
|
272
|
+
* sinks, only use with sync sinks. Use {@link reset} if you have async sinks.
|
|
273
|
+
* @since 0.9.0
|
|
274
|
+
*/
|
|
275
|
+
function resetSync() {
|
|
276
|
+
disposeSync();
|
|
277
|
+
resetInternal();
|
|
278
|
+
}
|
|
279
|
+
function resetInternal() {
|
|
280
|
+
const rootLogger = logger_js_1.LoggerImpl.getLogger([]);
|
|
281
|
+
rootLogger.resetDescendants();
|
|
282
|
+
delete rootLogger.contextLocalStorage;
|
|
283
|
+
strongRefs.clear();
|
|
284
|
+
currentConfig = null;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Dispose of the disposables.
|
|
288
|
+
*/
|
|
289
|
+
async function dispose() {
|
|
290
|
+
disposeSync();
|
|
291
|
+
const promises = [];
|
|
292
|
+
for (const disposable of asyncDisposables) {
|
|
293
|
+
promises.push(disposable[Symbol.asyncDispose]());
|
|
294
|
+
asyncDisposables.delete(disposable);
|
|
295
|
+
}
|
|
296
|
+
await Promise.all(promises);
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Dispose of the sync disposables. Async disposables will be untouched,
|
|
300
|
+
* use {@link dispose} if you have async sinks.
|
|
301
|
+
* @since 0.9.0
|
|
302
|
+
*/
|
|
303
|
+
function disposeSync() {
|
|
304
|
+
for (const disposable of disposables)
|
|
305
|
+
disposable[Symbol.dispose]();
|
|
306
|
+
disposables.clear();
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* A configuration error.
|
|
310
|
+
*/
|
|
311
|
+
class ConfigError extends Error {
|
|
312
|
+
/**
|
|
313
|
+
* Constructs a new configuration error.
|
|
314
|
+
* @param message The error message.
|
|
315
|
+
*/
|
|
316
|
+
constructor(message) {
|
|
317
|
+
super(message);
|
|
318
|
+
this.name = "ConfigureError";
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
exports.ConfigError = ConfigError;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.withContext = withContext;
|
|
4
|
+
const logger_js_1 = require("./logger.js");
|
|
5
|
+
/**
|
|
6
|
+
* Runs a callback with the given implicit context. Every single log record
|
|
7
|
+
* in the callback will have the given context.
|
|
8
|
+
*
|
|
9
|
+
* If no `contextLocalStorage` is configured, this function does nothing and
|
|
10
|
+
* just returns the return value of the callback. It also logs a warning to
|
|
11
|
+
* the `["logtape", "meta"]` logger in this case.
|
|
12
|
+
* @param context The context to inject.
|
|
13
|
+
* @param callback The callback to run.
|
|
14
|
+
* @returns The return value of the callback.
|
|
15
|
+
* @since 0.7.0
|
|
16
|
+
*/
|
|
17
|
+
function withContext(context, callback) {
|
|
18
|
+
const rootLogger = logger_js_1.LoggerImpl.getLogger();
|
|
19
|
+
if (rootLogger.contextLocalStorage == null) {
|
|
20
|
+
logger_js_1.LoggerImpl.getLogger(["logtape", "meta"]).warn("Context-local storage is not configured. " +
|
|
21
|
+
"Specify contextLocalStorage option in the configure() function.");
|
|
22
|
+
return callback();
|
|
23
|
+
}
|
|
24
|
+
const parentContext = rootLogger.contextLocalStorage.getStore() ?? {};
|
|
25
|
+
return rootLogger.contextLocalStorage.run({ ...parentContext, ...context }, callback);
|
|
26
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toFilter = toFilter;
|
|
4
|
+
exports.getLevelFilter = getLevelFilter;
|
|
5
|
+
/**
|
|
6
|
+
* Converts a {@link FilterLike} value to an actual {@link Filter}.
|
|
7
|
+
*
|
|
8
|
+
* @param filter The filter-like value to convert.
|
|
9
|
+
* @returns The actual filter.
|
|
10
|
+
*/
|
|
11
|
+
function toFilter(filter) {
|
|
12
|
+
if (typeof filter === "function")
|
|
13
|
+
return filter;
|
|
14
|
+
return getLevelFilter(filter);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Returns a filter that accepts log records with the specified level.
|
|
18
|
+
*
|
|
19
|
+
* @param level The level to filter by. If `null`, the filter will reject all
|
|
20
|
+
* records.
|
|
21
|
+
* @returns The filter.
|
|
22
|
+
*/
|
|
23
|
+
function getLevelFilter(level) {
|
|
24
|
+
if (level == null)
|
|
25
|
+
return () => false;
|
|
26
|
+
if (level === "fatal") {
|
|
27
|
+
return (record) => record.level === "fatal";
|
|
28
|
+
}
|
|
29
|
+
else if (level === "error") {
|
|
30
|
+
return (record) => record.level === "fatal" || record.level === "error";
|
|
31
|
+
}
|
|
32
|
+
else if (level === "warning") {
|
|
33
|
+
return (record) => record.level === "fatal" ||
|
|
34
|
+
record.level === "error" ||
|
|
35
|
+
record.level === "warning";
|
|
36
|
+
}
|
|
37
|
+
else if (level === "info") {
|
|
38
|
+
return (record) => record.level === "fatal" ||
|
|
39
|
+
record.level === "error" ||
|
|
40
|
+
record.level === "warning" ||
|
|
41
|
+
record.level === "info";
|
|
42
|
+
}
|
|
43
|
+
else if (level === "debug")
|
|
44
|
+
return () => true;
|
|
45
|
+
throw new TypeError(`Invalid log level: ${level}.`);
|
|
46
|
+
}
|