@elgato/utils 0.0.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.
- package/LICENSE +21 -0
- package/README.md +210 -0
- package/dist/disposable.d.ts +19 -0
- package/dist/disposable.js +21 -0
- package/dist/enumerable.d.ts +141 -0
- package/dist/enumerable.js +302 -0
- package/dist/event-emitter.d.ts +124 -0
- package/dist/event-emitter.js +170 -0
- package/dist/i18n/index.d.ts +2 -0
- package/dist/i18n/index.js +2 -0
- package/dist/i18n/language.d.ts +14 -0
- package/dist/i18n/language.js +4 -0
- package/dist/i18n/provider.d.ts +42 -0
- package/dist/i18n/provider.js +76 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +10 -0
- package/dist/json.d.ts +14 -0
- package/dist/json.js +1 -0
- package/dist/lazy.d.ts +16 -0
- package/dist/lazy.js +30 -0
- package/dist/logging/console-target.d.ts +10 -0
- package/dist/logging/console-target.js +20 -0
- package/dist/logging/file-target.d.ts +59 -0
- package/dist/logging/file-target.js +101 -0
- package/dist/logging/format.d.ts +23 -0
- package/dist/logging/format.js +47 -0
- package/dist/logging/index.d.ts +6 -0
- package/dist/logging/index.js +6 -0
- package/dist/logging/level.d.ts +30 -0
- package/dist/logging/level.js +21 -0
- package/dist/logging/logger.d.ts +87 -0
- package/dist/logging/logger.js +120 -0
- package/dist/logging/target.d.ts +32 -0
- package/dist/logging/target.js +1 -0
- package/dist/objects.d.ts +19 -0
- package/dist/objects.js +33 -0
- package/dist/parsers.d.ts +12 -0
- package/dist/parsers.js +26 -0
- package/dist/promises.d.ts +24 -0
- package/dist/promises.js +13 -0
- package/dist/rpc/client.d.ts +56 -0
- package/dist/rpc/client.js +144 -0
- package/dist/rpc/gateway.d.ts +28 -0
- package/dist/rpc/gateway.js +38 -0
- package/dist/rpc/index.d.ts +9 -0
- package/dist/rpc/index.js +9 -0
- package/dist/rpc/json-rpc/error.d.ts +52 -0
- package/dist/rpc/json-rpc/error.js +34 -0
- package/dist/rpc/json-rpc/request.d.ts +28 -0
- package/dist/rpc/json-rpc/request.js +13 -0
- package/dist/rpc/json-rpc/response.d.ts +53 -0
- package/dist/rpc/json-rpc/response.js +22 -0
- package/dist/rpc/proxy.d.ts +7 -0
- package/dist/rpc/proxy.js +1 -0
- package/dist/rpc/request.d.ts +22 -0
- package/dist/rpc/request.js +1 -0
- package/dist/rpc/response.d.ts +58 -0
- package/dist/rpc/response.js +69 -0
- package/dist/rpc/server.d.ts +34 -0
- package/dist/rpc/server.js +87 -0
- package/dist/timers.d.ts +7 -0
- package/dist/timers.js +22 -0
- package/dist/types.d.ts +20 -0
- package/dist/types.js +1 -0
- package/package.json +65 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { freeze, get } from "../objects.js";
|
|
2
|
+
import { defaultLanguage } from "./language.js";
|
|
3
|
+
/**
|
|
4
|
+
* Internalization provider, responsible for managing localizations and translating resources.
|
|
5
|
+
*/
|
|
6
|
+
export class I18nProvider {
|
|
7
|
+
/**
|
|
8
|
+
* Map of localized resources, indexed by their language.
|
|
9
|
+
*/
|
|
10
|
+
#translations = new Map();
|
|
11
|
+
/**
|
|
12
|
+
* Default language of the provider.
|
|
13
|
+
*/
|
|
14
|
+
#language;
|
|
15
|
+
/**
|
|
16
|
+
* Function responsible for providing localized resources for a given language.
|
|
17
|
+
*/
|
|
18
|
+
#readTranslations;
|
|
19
|
+
/**
|
|
20
|
+
* Initializes a new instance of the {@link I18nProvider} class.
|
|
21
|
+
* @param language The default language to be used when retrieving translations for a given key.
|
|
22
|
+
* @param readTranslations Function responsible for providing localized resources for a given language.
|
|
23
|
+
*/
|
|
24
|
+
constructor(language, readTranslations) {
|
|
25
|
+
this.#language = language;
|
|
26
|
+
this.#readTranslations = readTranslations;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Translates the specified {@link key}, as defined within the resources for the {@link language}.
|
|
30
|
+
* When the key is not found, the default language is checked. Alias of {@link I18nProvider.translate}.
|
|
31
|
+
* @param key Key of the translation.
|
|
32
|
+
* @param language Optional language to get the translation for; otherwise the default language.
|
|
33
|
+
* @returns The translation; otherwise the key.
|
|
34
|
+
*/
|
|
35
|
+
t(key, language = this.#language) {
|
|
36
|
+
return this.translate(key, language);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Translates the specified {@link key}, as defined within the resources for the {@link language}.
|
|
40
|
+
* When the key is not found, the default language is checked.
|
|
41
|
+
* @param key Key of the translation.
|
|
42
|
+
* @param language Optional language to get the translation for; otherwise the default language.
|
|
43
|
+
* @returns The translation; otherwise the key.
|
|
44
|
+
*/
|
|
45
|
+
translate(key, language = this.#language) {
|
|
46
|
+
// Determine the languages to search for.
|
|
47
|
+
const languages = new Set([
|
|
48
|
+
language,
|
|
49
|
+
language.replaceAll("_", "-").split("-").at(0),
|
|
50
|
+
defaultLanguage,
|
|
51
|
+
]);
|
|
52
|
+
// Attempt to find the resource for the languages.
|
|
53
|
+
for (const language of languages) {
|
|
54
|
+
const resource = get(this.getTranslations(language), key);
|
|
55
|
+
if (resource) {
|
|
56
|
+
return resource.toString();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// Otherwise fallback to the key.
|
|
60
|
+
return key;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Gets the translations for the specified language.
|
|
64
|
+
* @param language Language whose translations are being retrieved.
|
|
65
|
+
* @returns The translations; otherwise `null`.
|
|
66
|
+
*/
|
|
67
|
+
getTranslations(language) {
|
|
68
|
+
let translations = this.#translations.get(language);
|
|
69
|
+
if (translations === undefined) {
|
|
70
|
+
translations = this.#readTranslations(language);
|
|
71
|
+
freeze(translations);
|
|
72
|
+
this.#translations.set(language, translations);
|
|
73
|
+
}
|
|
74
|
+
return translations;
|
|
75
|
+
}
|
|
76
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./disposable.js";
|
|
2
|
+
export * from "./enumerable.js";
|
|
3
|
+
export * from "./event-emitter.js";
|
|
4
|
+
export * from "./json.js";
|
|
5
|
+
export * from "./lazy.js";
|
|
6
|
+
export * from "./objects.js";
|
|
7
|
+
export * from "./parsers.js";
|
|
8
|
+
export * from "./promises.js";
|
|
9
|
+
export * from "./timers.js";
|
|
10
|
+
export * from "./types.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./disposable.js";
|
|
2
|
+
export * from "./enumerable.js";
|
|
3
|
+
export * from "./event-emitter.js";
|
|
4
|
+
export * from "./json.js";
|
|
5
|
+
export * from "./lazy.js";
|
|
6
|
+
export * from "./objects.js";
|
|
7
|
+
export * from "./parsers.js";
|
|
8
|
+
export * from "./promises.js";
|
|
9
|
+
export * from "./timers.js";
|
|
10
|
+
export * from "./types.js";
|
package/dist/json.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON object.
|
|
3
|
+
*/
|
|
4
|
+
export type JsonObject = {
|
|
5
|
+
[key: string]: JsonValue;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* JSON primitive value.
|
|
9
|
+
*/
|
|
10
|
+
export type JsonPrimitive = boolean | number | string | null | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* JSON value.
|
|
13
|
+
*/
|
|
14
|
+
export type JsonValue = JsonObject | JsonPrimitive | JsonValue[];
|
package/dist/json.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/lazy.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provides a wrapper around a value that is lazily instantiated.
|
|
3
|
+
*/
|
|
4
|
+
export declare class Lazy<T> {
|
|
5
|
+
#private;
|
|
6
|
+
/**
|
|
7
|
+
* Initializes a new instance of the {@link Lazy} class.
|
|
8
|
+
* @param valueFactory The factory responsible for instantiating the value.
|
|
9
|
+
*/
|
|
10
|
+
constructor(valueFactory: () => T);
|
|
11
|
+
/**
|
|
12
|
+
* Gets the value.
|
|
13
|
+
* @returns The value.
|
|
14
|
+
*/
|
|
15
|
+
get value(): T;
|
|
16
|
+
}
|
package/dist/lazy.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provides a wrapper around a value that is lazily instantiated.
|
|
3
|
+
*/
|
|
4
|
+
export class Lazy {
|
|
5
|
+
/**
|
|
6
|
+
* Private backing field for {@link Lazy.value}.
|
|
7
|
+
*/
|
|
8
|
+
#value = undefined;
|
|
9
|
+
/**
|
|
10
|
+
* Factory responsible for instantiating the value.
|
|
11
|
+
*/
|
|
12
|
+
#valueFactory;
|
|
13
|
+
/**
|
|
14
|
+
* Initializes a new instance of the {@link Lazy} class.
|
|
15
|
+
* @param valueFactory The factory responsible for instantiating the value.
|
|
16
|
+
*/
|
|
17
|
+
constructor(valueFactory) {
|
|
18
|
+
this.#valueFactory = valueFactory;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Gets the value.
|
|
22
|
+
* @returns The value.
|
|
23
|
+
*/
|
|
24
|
+
get value() {
|
|
25
|
+
if (this.#value === undefined) {
|
|
26
|
+
this.#value = this.#valueFactory();
|
|
27
|
+
}
|
|
28
|
+
return this.#value;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provides a {@link LogTarget} that logs to the console.
|
|
3
|
+
*/
|
|
4
|
+
export class ConsoleTarget {
|
|
5
|
+
/**
|
|
6
|
+
* @inheritdoc
|
|
7
|
+
*/
|
|
8
|
+
write(entry) {
|
|
9
|
+
switch (entry.level) {
|
|
10
|
+
case "error":
|
|
11
|
+
console.error(...entry.data);
|
|
12
|
+
break;
|
|
13
|
+
case "warn":
|
|
14
|
+
console.warn(...entry.data);
|
|
15
|
+
break;
|
|
16
|
+
default:
|
|
17
|
+
console.log(...entry.data);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { LogEntryFormatter } from "./format.js";
|
|
2
|
+
import type { LogEntry, LogTarget } from "./target.js";
|
|
3
|
+
/**
|
|
4
|
+
* Provides a {@link LogTarget} capable of logging to a local file system.
|
|
5
|
+
*/
|
|
6
|
+
export declare class FileTarget implements LogTarget {
|
|
7
|
+
#private;
|
|
8
|
+
/**
|
|
9
|
+
* Initializes a new instance of the {@link FileTarget} class.
|
|
10
|
+
* @param options Options that defines how logs should be written to the local file system.
|
|
11
|
+
*/
|
|
12
|
+
constructor(options: FileTargetOptions);
|
|
13
|
+
/**
|
|
14
|
+
* @inheritdoc
|
|
15
|
+
*/
|
|
16
|
+
write(entry: LogEntry): void;
|
|
17
|
+
/**
|
|
18
|
+
* Gets the file path to an indexed log file.
|
|
19
|
+
* @param index Optional index of the log file to be included as part of the file name.
|
|
20
|
+
* @returns File path that represents the indexed log file.
|
|
21
|
+
*/
|
|
22
|
+
private getLogFilePath;
|
|
23
|
+
/**
|
|
24
|
+
* Gets the log files associated with this file target, including past and present.
|
|
25
|
+
* @returns Log file entries.
|
|
26
|
+
*/
|
|
27
|
+
private getLogFiles;
|
|
28
|
+
/**
|
|
29
|
+
* Re-indexes the existing log files associated with this file target, removing old log files whose index exceeds the {@link FileTargetOptions.maxFileCount}, and renaming the
|
|
30
|
+
* remaining log files, leaving index "0" free for a new log file.
|
|
31
|
+
*/
|
|
32
|
+
private reIndex;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Options that determine the behavior of a {@link FileTarget}.
|
|
36
|
+
*/
|
|
37
|
+
export type FileTargetOptions = {
|
|
38
|
+
/**
|
|
39
|
+
* Destination folder where log files will be written.
|
|
40
|
+
*/
|
|
41
|
+
dest: string;
|
|
42
|
+
/**
|
|
43
|
+
* Name of the log file. The filename extension will be `.log`, and the file will be indexed, e.g. when {@link FileTargetOptions.fileName} is `com.elgato.test`, the resulting output
|
|
44
|
+
* filename will be `com.elgato.test.1.log`.
|
|
45
|
+
*/
|
|
46
|
+
fileName: string;
|
|
47
|
+
/**
|
|
48
|
+
* Formatter responsible for formatting the log entry.
|
|
49
|
+
*/
|
|
50
|
+
format: LogEntryFormatter;
|
|
51
|
+
/**
|
|
52
|
+
* Maximum number of files that can be created as part of the target before old logs should be truncated and removed.
|
|
53
|
+
*/
|
|
54
|
+
maxFileCount: number;
|
|
55
|
+
/**
|
|
56
|
+
* Maximum size of a log file; when exceeded, file rotation occurs and older log files are removed based on {@link FileTargetOptions.maxFileCount}.
|
|
57
|
+
*/
|
|
58
|
+
maxSize: number;
|
|
59
|
+
};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
/**
|
|
4
|
+
* Provides a {@link LogTarget} capable of logging to a local file system.
|
|
5
|
+
*/
|
|
6
|
+
export class FileTarget {
|
|
7
|
+
/**
|
|
8
|
+
* File path where logs will be written.
|
|
9
|
+
*/
|
|
10
|
+
#filePath;
|
|
11
|
+
/**
|
|
12
|
+
* Options that defines how logs should be written to the local file system.
|
|
13
|
+
*/
|
|
14
|
+
#options;
|
|
15
|
+
/**
|
|
16
|
+
* Current size of the logs that have been written to the {@link FileTarget.#filePath}.
|
|
17
|
+
*/
|
|
18
|
+
#size = 0;
|
|
19
|
+
/**
|
|
20
|
+
* Initializes a new instance of the {@link FileTarget} class.
|
|
21
|
+
* @param options Options that defines how logs should be written to the local file system.
|
|
22
|
+
*/
|
|
23
|
+
constructor(options) {
|
|
24
|
+
this.#options = options;
|
|
25
|
+
this.#filePath = this.getLogFilePath();
|
|
26
|
+
this.reIndex();
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* @inheritdoc
|
|
30
|
+
*/
|
|
31
|
+
write(entry) {
|
|
32
|
+
const fd = fs.openSync(this.#filePath, "a");
|
|
33
|
+
try {
|
|
34
|
+
const msg = this.#options.format(entry);
|
|
35
|
+
fs.writeSync(fd, msg + "\n");
|
|
36
|
+
this.#size += msg.length;
|
|
37
|
+
}
|
|
38
|
+
finally {
|
|
39
|
+
fs.closeSync(fd);
|
|
40
|
+
}
|
|
41
|
+
if (this.#size >= this.#options.maxSize) {
|
|
42
|
+
this.reIndex();
|
|
43
|
+
this.#size = 0;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Gets the file path to an indexed log file.
|
|
48
|
+
* @param index Optional index of the log file to be included as part of the file name.
|
|
49
|
+
* @returns File path that represents the indexed log file.
|
|
50
|
+
*/
|
|
51
|
+
getLogFilePath(index = 0) {
|
|
52
|
+
return path.join(this.#options.dest, `${this.#options.fileName}.${index}.log`);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Gets the log files associated with this file target, including past and present.
|
|
56
|
+
* @returns Log file entries.
|
|
57
|
+
*/
|
|
58
|
+
getLogFiles() {
|
|
59
|
+
const regex = /^\.(\d+)\.log$/;
|
|
60
|
+
return fs
|
|
61
|
+
.readdirSync(this.#options.dest, { withFileTypes: true })
|
|
62
|
+
.reduce((prev, entry) => {
|
|
63
|
+
if (entry.isDirectory() || entry.name.indexOf(this.#options.fileName) < 0) {
|
|
64
|
+
return prev;
|
|
65
|
+
}
|
|
66
|
+
const match = entry.name.substring(this.#options.fileName.length).match(regex);
|
|
67
|
+
if (match?.length !== 2) {
|
|
68
|
+
return prev;
|
|
69
|
+
}
|
|
70
|
+
prev.push({
|
|
71
|
+
path: path.join(this.#options.dest, entry.name),
|
|
72
|
+
index: parseInt(match[1]),
|
|
73
|
+
});
|
|
74
|
+
return prev;
|
|
75
|
+
}, [])
|
|
76
|
+
.sort(({ index: a }, { index: b }) => {
|
|
77
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Re-indexes the existing log files associated with this file target, removing old log files whose index exceeds the {@link FileTargetOptions.maxFileCount}, and renaming the
|
|
82
|
+
* remaining log files, leaving index "0" free for a new log file.
|
|
83
|
+
*/
|
|
84
|
+
reIndex() {
|
|
85
|
+
// When the destination directory is new, create it, and return.
|
|
86
|
+
if (!fs.existsSync(this.#options.dest)) {
|
|
87
|
+
fs.mkdirSync(this.#options.dest);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const logFiles = this.getLogFiles();
|
|
91
|
+
for (let i = logFiles.length - 1; i >= 0; i--) {
|
|
92
|
+
const log = logFiles[i];
|
|
93
|
+
if (i >= this.#options.maxFileCount - 1) {
|
|
94
|
+
fs.rmSync(log.path);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
fs.renameSync(log.path, this.getLogFilePath(i + 1));
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { LogEntry } from "./target.js";
|
|
2
|
+
/**
|
|
3
|
+
* Formats the specified entry.
|
|
4
|
+
* @param entry Log entry to format.
|
|
5
|
+
* @returns The formatted log entry as a `string`.
|
|
6
|
+
*/
|
|
7
|
+
export type LogEntryFormatter = (entry: LogEntry) => string;
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new string log entry formatter.
|
|
10
|
+
* @param opts Options that defines the type for the formatter.
|
|
11
|
+
* @returns The string {@link LogEntryFormatter}.
|
|
12
|
+
*/
|
|
13
|
+
export declare function stringFormatter(opts?: StringFormatOptions): LogEntryFormatter;
|
|
14
|
+
/**
|
|
15
|
+
* Options associated with {@link stringFormatter}.
|
|
16
|
+
*/
|
|
17
|
+
type StringFormatOptions = {
|
|
18
|
+
/**
|
|
19
|
+
* Indicates whether the entry should only log the {@link LogEntry.data}.
|
|
20
|
+
*/
|
|
21
|
+
dataOnly?: boolean;
|
|
22
|
+
};
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// Remove any dependencies on node.
|
|
2
|
+
const EOL = "\n";
|
|
3
|
+
/**
|
|
4
|
+
* Creates a new string log entry formatter.
|
|
5
|
+
* @param opts Options that defines the type for the formatter.
|
|
6
|
+
* @returns The string {@link LogEntryFormatter}.
|
|
7
|
+
*/
|
|
8
|
+
export function stringFormatter(opts) {
|
|
9
|
+
if (opts?.dataOnly) {
|
|
10
|
+
return ({ data }) => `${reduce(data)}`;
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
return (entry) => {
|
|
14
|
+
const { data, level, scope } = entry;
|
|
15
|
+
let prefix = `${new Date().toISOString()} ${level.toUpperCase().padEnd(5)} `;
|
|
16
|
+
if (scope) {
|
|
17
|
+
prefix += `${scope}: `;
|
|
18
|
+
}
|
|
19
|
+
return `${prefix}${reduce(data)}`;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Stringifies the provided data parameters that make up the log entry.
|
|
25
|
+
* @param data Data parameters.
|
|
26
|
+
* @returns The data represented as a single `string`.
|
|
27
|
+
*/
|
|
28
|
+
function reduce(data) {
|
|
29
|
+
let result = "";
|
|
30
|
+
let previousWasError = false;
|
|
31
|
+
for (const value of data) {
|
|
32
|
+
// When the value is an error, write the stack.
|
|
33
|
+
if (typeof value === "object" && value instanceof Error) {
|
|
34
|
+
result += `${EOL}${value.stack}`;
|
|
35
|
+
previousWasError = true;
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
// When the previous was an error, write a new line.
|
|
39
|
+
if (previousWasError) {
|
|
40
|
+
result += EOL;
|
|
41
|
+
previousWasError = false;
|
|
42
|
+
}
|
|
43
|
+
result += typeof value === "object" ? JSON.stringify(value) : value;
|
|
44
|
+
result += " ";
|
|
45
|
+
}
|
|
46
|
+
return result.trimEnd();
|
|
47
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Levels of logging.
|
|
3
|
+
*/
|
|
4
|
+
export type LogLevel =
|
|
5
|
+
/**
|
|
6
|
+
* Error message used to indicate an error was thrown, or something critically went wrong.
|
|
7
|
+
*/
|
|
8
|
+
"error"
|
|
9
|
+
/**
|
|
10
|
+
* Warning message used to indicate something went wrong, but the application is able to recover.
|
|
11
|
+
*/
|
|
12
|
+
| "warn"
|
|
13
|
+
/**
|
|
14
|
+
* Information message for general usage.
|
|
15
|
+
*/
|
|
16
|
+
| "info"
|
|
17
|
+
/**
|
|
18
|
+
* Debug message used to detail information useful for profiling the applications runtime.
|
|
19
|
+
*/
|
|
20
|
+
| "debug"
|
|
21
|
+
/**
|
|
22
|
+
* Trace message used to monitor low-level information such as method calls, performance tracking, etc.
|
|
23
|
+
*/
|
|
24
|
+
| "trace";
|
|
25
|
+
/**
|
|
26
|
+
* Gets the priority of the specified log level as a number; low numbers signify a higher priority.
|
|
27
|
+
* @param level Log level.
|
|
28
|
+
* @returns The priority as a number.
|
|
29
|
+
*/
|
|
30
|
+
export declare function defcon(level: LogLevel): number;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/sort-type-constituents */
|
|
2
|
+
/**
|
|
3
|
+
* Gets the priority of the specified log level as a number; low numbers signify a higher priority.
|
|
4
|
+
* @param level Log level.
|
|
5
|
+
* @returns The priority as a number.
|
|
6
|
+
*/
|
|
7
|
+
export function defcon(level) {
|
|
8
|
+
switch (level) {
|
|
9
|
+
case "error":
|
|
10
|
+
return 0;
|
|
11
|
+
case "warn":
|
|
12
|
+
return 1;
|
|
13
|
+
case "info":
|
|
14
|
+
return 2;
|
|
15
|
+
case "debug":
|
|
16
|
+
return 3;
|
|
17
|
+
case "trace":
|
|
18
|
+
default:
|
|
19
|
+
return 4;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { type LogLevel } from "./level.js";
|
|
2
|
+
import type { LogEntry, LogEntryData, LogTarget } from "./target.js";
|
|
3
|
+
/**
|
|
4
|
+
* Logger capable of forwarding messages to a {@link LogTarget}.
|
|
5
|
+
*/
|
|
6
|
+
export declare class Logger {
|
|
7
|
+
#private;
|
|
8
|
+
/**
|
|
9
|
+
* Initializes a new instance of the {@link Logger} class.
|
|
10
|
+
* @param opts Options that define the loggers behavior.
|
|
11
|
+
*/
|
|
12
|
+
constructor(opts: LoggerOptions);
|
|
13
|
+
/**
|
|
14
|
+
* Gets the {@link LogLevel}.
|
|
15
|
+
* @returns The {@link LogLevel}.
|
|
16
|
+
*/
|
|
17
|
+
get level(): LogLevel;
|
|
18
|
+
/**
|
|
19
|
+
* Creates a scoped logger with the given {@link scope}; logs created by scoped-loggers include their scope to enable their source to be easily identified.
|
|
20
|
+
* @param scope Value that represents the scope of the new logger.
|
|
21
|
+
* @returns The scoped logger, or this instance when {@link scope} is not defined.
|
|
22
|
+
*/
|
|
23
|
+
createScope(scope: string): Logger | this;
|
|
24
|
+
/**
|
|
25
|
+
* Writes the arguments as a debug log entry.
|
|
26
|
+
* @param data Message or data to log.
|
|
27
|
+
* @returns This instance for chaining.
|
|
28
|
+
*/
|
|
29
|
+
debug(...data: LogEntryData): this;
|
|
30
|
+
/**
|
|
31
|
+
* Writes the arguments as error log entry.
|
|
32
|
+
* @param data Message or data to log.
|
|
33
|
+
* @returns This instance for chaining.
|
|
34
|
+
*/
|
|
35
|
+
error(...data: LogEntryData): this;
|
|
36
|
+
/**
|
|
37
|
+
* Writes the arguments as an info log entry.
|
|
38
|
+
* @param data Message or data to log.
|
|
39
|
+
* @returns This instance for chaining.
|
|
40
|
+
*/
|
|
41
|
+
info(...data: LogEntryData): this;
|
|
42
|
+
/**
|
|
43
|
+
* Sets the log-level that determines which logs should be written. The specified level will be inherited by all scoped loggers unless they have log-level explicitly defined.
|
|
44
|
+
* @param level The log-level that determines which logs should be written; when `undefined`, the level will be inherited from the parent logger, or default to the environment level.
|
|
45
|
+
* @returns This instance for chaining.
|
|
46
|
+
*/
|
|
47
|
+
setLevel(level?: LogLevel): this;
|
|
48
|
+
/**
|
|
49
|
+
* Writes the arguments as a trace log entry.
|
|
50
|
+
* @param data Message or data to log.
|
|
51
|
+
* @returns This instance for chaining.
|
|
52
|
+
*/
|
|
53
|
+
trace(...data: LogEntryData): this;
|
|
54
|
+
/**
|
|
55
|
+
* Writes the arguments as a warning log entry.
|
|
56
|
+
* @param data Message or data to log.
|
|
57
|
+
* @returns This instance for chaining.
|
|
58
|
+
*/
|
|
59
|
+
warn(...data: LogEntryData): this;
|
|
60
|
+
/**
|
|
61
|
+
* Writes the log entry.
|
|
62
|
+
* @param entry Log entry to write.
|
|
63
|
+
* @returns This instance for chaining.
|
|
64
|
+
*/
|
|
65
|
+
write(entry: LogEntry): this;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Options that define the logger's behavior.
|
|
69
|
+
*/
|
|
70
|
+
export type LoggerOptions = {
|
|
71
|
+
/**
|
|
72
|
+
* Determines the minimum level of logs that can be written.
|
|
73
|
+
*/
|
|
74
|
+
level: LogLevel | (() => LogLevel);
|
|
75
|
+
/**
|
|
76
|
+
* Minimum level the logger can be set to.
|
|
77
|
+
*/
|
|
78
|
+
minimumLevel?: LogLevel;
|
|
79
|
+
/**
|
|
80
|
+
* Optional value that defines the scope of the logger.
|
|
81
|
+
*/
|
|
82
|
+
scope?: string;
|
|
83
|
+
/**
|
|
84
|
+
* Log targets where logs will be written to.
|
|
85
|
+
*/
|
|
86
|
+
targets: LogTarget[];
|
|
87
|
+
};
|