@vercube/logger 0.0.1-alpha.15
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/dist/Common/Logger.d.ts +39 -0
- package/dist/Common/LoggerProvider.d.ts +26 -0
- package/dist/Providers/ConsoleProvider.d.ts +18 -0
- package/dist/Providers/JsonProvider.d.ts +19 -0
- package/dist/Providers/index.cjs +73 -0
- package/dist/Providers/index.d.ts +2 -0
- package/dist/Providers/index.mjs +71 -0
- package/dist/Service/BaseLogger.d.ts +55 -0
- package/dist/Types/LoggerTypes.d.ts +27 -0
- package/dist/Utils/Utils.d.ts +11 -0
- package/dist/index.cjs +175 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.mjs +143 -0
- package/package.json +34 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { LoggerTypes } from "../Types/LoggerTypes.js";
|
|
2
|
+
/**
|
|
3
|
+
* Abstract base class for implementing logging services.
|
|
4
|
+
* Provides a standard interface for logging at different levels.
|
|
5
|
+
* Implementations can customize how logs are processed and stored.
|
|
6
|
+
*/
|
|
7
|
+
export declare abstract class Logger {
|
|
8
|
+
/**
|
|
9
|
+
* Configures the logger with the provided options.
|
|
10
|
+
* This method should be called before using the logger to process logs.
|
|
11
|
+
* @param options - Configuration options for the logger
|
|
12
|
+
* @returns void or Promise<void> if configuration is asynchronous
|
|
13
|
+
*/
|
|
14
|
+
abstract configure(options: LoggerTypes.Options): void;
|
|
15
|
+
/**
|
|
16
|
+
* Logs a message at the specified log level.
|
|
17
|
+
* @param args - Additional parameters to be logged
|
|
18
|
+
* @returns A value determined by the implementing class
|
|
19
|
+
*/
|
|
20
|
+
abstract debug(...args: LoggerTypes.Arg[]): void;
|
|
21
|
+
/**
|
|
22
|
+
* Logs an informational message.
|
|
23
|
+
* @param args - Additional parameters to be logged
|
|
24
|
+
* @returns A value determined by the implementing class
|
|
25
|
+
*/
|
|
26
|
+
abstract info(...args: LoggerTypes.Arg[]): void;
|
|
27
|
+
/**
|
|
28
|
+
* Logs a warning message.
|
|
29
|
+
* @param args - Additional parameters to be logged
|
|
30
|
+
* @returns A value determined by the implementing class
|
|
31
|
+
*/
|
|
32
|
+
abstract warn(...args: LoggerTypes.Arg[]): void;
|
|
33
|
+
/**
|
|
34
|
+
* Logs an error message.
|
|
35
|
+
* @param args - Additional parameters to be logged
|
|
36
|
+
* @returns A value determined by the implementing class
|
|
37
|
+
*/
|
|
38
|
+
abstract error(...args: LoggerTypes.Arg[]): void;
|
|
39
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { LoggerTypes } from "../Types/LoggerTypes.js";
|
|
2
|
+
/**
|
|
3
|
+
* Abstract base class for implementing log provider.
|
|
4
|
+
* Providers are responsible for processing and outputting log messages to various destinations.
|
|
5
|
+
* Each appender can be initialized with custom options and handles message processing according to its implementation.
|
|
6
|
+
*
|
|
7
|
+
* @template T - The type of options used to initialize the appender
|
|
8
|
+
*/
|
|
9
|
+
export declare abstract class LoggerProvider<T = unknown> {
|
|
10
|
+
/**
|
|
11
|
+
* Initializes the appender with the provided options.
|
|
12
|
+
* This method should be called before using the appender to process messages.
|
|
13
|
+
*
|
|
14
|
+
* @param options - Configuration options for the appender
|
|
15
|
+
* @returns void or Promise<void> if initialization is asynchronous
|
|
16
|
+
*/
|
|
17
|
+
abstract initialize(options: T): void | Promise<void>;
|
|
18
|
+
/**
|
|
19
|
+
* Processes a log message according to the appender's implementation.
|
|
20
|
+
* This method handles the actual logging logic specific to each appender type.
|
|
21
|
+
*
|
|
22
|
+
* @param message - The log message to process, containing level, tag, and other metadata
|
|
23
|
+
* @returns void or Promise<void> if processing is asynchronous
|
|
24
|
+
*/
|
|
25
|
+
abstract processMessage(message: LoggerTypes.Message): void | Promise<void>;
|
|
26
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { LoggerProvider } from "../Common/LoggerProvider.js";
|
|
2
|
+
import { LoggerTypes } from "../Types/LoggerTypes.js";
|
|
3
|
+
/**
|
|
4
|
+
* ConsoleProvider class for logging messages to the console.
|
|
5
|
+
*/
|
|
6
|
+
export declare class ConsoleProvider extends LoggerProvider {
|
|
7
|
+
/**
|
|
8
|
+
* Initializes the appender with the provided options.
|
|
9
|
+
* This method should be called before using the appender to process messages.
|
|
10
|
+
*/
|
|
11
|
+
initialize(): void;
|
|
12
|
+
/**
|
|
13
|
+
* Prints a log message according to the specified format and level.
|
|
14
|
+
* @param message - The log message object containing level, tag, and arguments
|
|
15
|
+
* @protected
|
|
16
|
+
*/
|
|
17
|
+
processMessage(message: LoggerTypes.Message): void;
|
|
18
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { LoggerProvider } from "../Common/LoggerProvider.js";
|
|
2
|
+
import { LoggerTypes } from "../Types/LoggerTypes.js";
|
|
3
|
+
/**
|
|
4
|
+
* A logger implementation that formats log messages as JSON.
|
|
5
|
+
*/
|
|
6
|
+
export declare class JSONProvider extends LoggerProvider {
|
|
7
|
+
/**
|
|
8
|
+
* Initializes the appender with the provided options.
|
|
9
|
+
* This method should be called before using the appender to process messages.
|
|
10
|
+
*
|
|
11
|
+
*/
|
|
12
|
+
initialize(): void;
|
|
13
|
+
/**
|
|
14
|
+
* Prints a log message according to the specified format and level.
|
|
15
|
+
* @param message - The log message object containing level, tag, and arguments
|
|
16
|
+
* @protected
|
|
17
|
+
*/
|
|
18
|
+
processMessage(message: LoggerTypes.Message): void;
|
|
19
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
//#region packages/logger/src/Common/LoggerProvider.ts
|
|
4
|
+
var LoggerProvider = class {};
|
|
5
|
+
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region packages/logger/src/Providers/JsonProvider.ts
|
|
8
|
+
var JSONProvider = class extends LoggerProvider {
|
|
9
|
+
/**
|
|
10
|
+
* Initializes the appender with the provided options.
|
|
11
|
+
* This method should be called before using the appender to process messages.
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
initialize() {}
|
|
15
|
+
/**
|
|
16
|
+
* Prints a log message according to the specified format and level.
|
|
17
|
+
* @param message - The log message object containing level, tag, and arguments
|
|
18
|
+
* @protected
|
|
19
|
+
*/
|
|
20
|
+
processMessage(message) {
|
|
21
|
+
console.log(JSON.stringify({
|
|
22
|
+
type: message?.type ?? "application_log",
|
|
23
|
+
...message
|
|
24
|
+
}));
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region packages/logger/src/Utils/Utils.ts
|
|
30
|
+
const isColorAllowed = () => !process.env.NO_COLOR;
|
|
31
|
+
const colorIfAllowed = (colorFn) => {
|
|
32
|
+
const wrappedFn = (text) => {
|
|
33
|
+
return isColorAllowed() ? colorFn(text) : text;
|
|
34
|
+
};
|
|
35
|
+
return wrappedFn;
|
|
36
|
+
};
|
|
37
|
+
const colors = {
|
|
38
|
+
bold: colorIfAllowed((text) => `\x1B[1m${text}\x1B[0m`),
|
|
39
|
+
green: colorIfAllowed((text) => `\x1B[32m${text}\x1B[39m`),
|
|
40
|
+
yellow: colorIfAllowed((text) => `\x1B[33m${text}\x1B[39m`),
|
|
41
|
+
red: colorIfAllowed((text) => `\x1B[31m${text}\x1B[39m`),
|
|
42
|
+
magentaBright: colorIfAllowed((text) => `\x1B[95m${text}\x1B[39m`),
|
|
43
|
+
cyanBright: colorIfAllowed((text) => `\x1B[96m${text}\x1B[39m`)
|
|
44
|
+
};
|
|
45
|
+
const LOG_LEVEL_COLORS = {
|
|
46
|
+
debug: colors.green,
|
|
47
|
+
info: colors.bold,
|
|
48
|
+
warn: colors.yellow,
|
|
49
|
+
error: colors.red
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region packages/logger/src/Providers/ConsoleProvider.ts
|
|
54
|
+
var ConsoleProvider = class extends LoggerProvider {
|
|
55
|
+
/**
|
|
56
|
+
* Initializes the appender with the provided options.
|
|
57
|
+
* This method should be called before using the appender to process messages.
|
|
58
|
+
*/
|
|
59
|
+
initialize() {}
|
|
60
|
+
/**
|
|
61
|
+
* Prints a log message according to the specified format and level.
|
|
62
|
+
* @param message - The log message object containing level, tag, and arguments
|
|
63
|
+
* @protected
|
|
64
|
+
*/
|
|
65
|
+
processMessage(message) {
|
|
66
|
+
const date = message.timestamp ? new Date(message.timestamp) : new Date();
|
|
67
|
+
console[message.level](`%s%s${message?.tag ? "%s" : ""} %s`, LOG_LEVEL_COLORS[message.level](`[${date.toISOString().split("T")[1]?.replace("Z", "")}]`), LOG_LEVEL_COLORS[message.level](`[${message.level.toUpperCase().padEnd(5, " ")}]`), message?.tag ? LOG_LEVEL_COLORS[message.level](`[${message.tag}]`) : "", ...message.args);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
//#endregion
|
|
72
|
+
exports.ConsoleProvider = ConsoleProvider
|
|
73
|
+
exports.JSONProvider = JSONProvider
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
|
|
2
|
+
//#region packages/logger/src/Common/LoggerProvider.ts
|
|
3
|
+
var LoggerProvider = class {};
|
|
4
|
+
|
|
5
|
+
//#endregion
|
|
6
|
+
//#region packages/logger/src/Providers/JsonProvider.ts
|
|
7
|
+
var JSONProvider = class extends LoggerProvider {
|
|
8
|
+
/**
|
|
9
|
+
* Initializes the appender with the provided options.
|
|
10
|
+
* This method should be called before using the appender to process messages.
|
|
11
|
+
*
|
|
12
|
+
*/
|
|
13
|
+
initialize() {}
|
|
14
|
+
/**
|
|
15
|
+
* Prints a log message according to the specified format and level.
|
|
16
|
+
* @param message - The log message object containing level, tag, and arguments
|
|
17
|
+
* @protected
|
|
18
|
+
*/
|
|
19
|
+
processMessage(message) {
|
|
20
|
+
console.log(JSON.stringify({
|
|
21
|
+
type: message?.type ?? "application_log",
|
|
22
|
+
...message
|
|
23
|
+
}));
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
//#region packages/logger/src/Utils/Utils.ts
|
|
29
|
+
const isColorAllowed = () => !process.env.NO_COLOR;
|
|
30
|
+
const colorIfAllowed = (colorFn) => {
|
|
31
|
+
const wrappedFn = (text) => {
|
|
32
|
+
return isColorAllowed() ? colorFn(text) : text;
|
|
33
|
+
};
|
|
34
|
+
return wrappedFn;
|
|
35
|
+
};
|
|
36
|
+
const colors = {
|
|
37
|
+
bold: colorIfAllowed((text) => `\x1B[1m${text}\x1B[0m`),
|
|
38
|
+
green: colorIfAllowed((text) => `\x1B[32m${text}\x1B[39m`),
|
|
39
|
+
yellow: colorIfAllowed((text) => `\x1B[33m${text}\x1B[39m`),
|
|
40
|
+
red: colorIfAllowed((text) => `\x1B[31m${text}\x1B[39m`),
|
|
41
|
+
magentaBright: colorIfAllowed((text) => `\x1B[95m${text}\x1B[39m`),
|
|
42
|
+
cyanBright: colorIfAllowed((text) => `\x1B[96m${text}\x1B[39m`)
|
|
43
|
+
};
|
|
44
|
+
const LOG_LEVEL_COLORS = {
|
|
45
|
+
debug: colors.green,
|
|
46
|
+
info: colors.bold,
|
|
47
|
+
warn: colors.yellow,
|
|
48
|
+
error: colors.red
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region packages/logger/src/Providers/ConsoleProvider.ts
|
|
53
|
+
var ConsoleProvider = class extends LoggerProvider {
|
|
54
|
+
/**
|
|
55
|
+
* Initializes the appender with the provided options.
|
|
56
|
+
* This method should be called before using the appender to process messages.
|
|
57
|
+
*/
|
|
58
|
+
initialize() {}
|
|
59
|
+
/**
|
|
60
|
+
* Prints a log message according to the specified format and level.
|
|
61
|
+
* @param message - The log message object containing level, tag, and arguments
|
|
62
|
+
* @protected
|
|
63
|
+
*/
|
|
64
|
+
processMessage(message) {
|
|
65
|
+
const date = message.timestamp ? new Date(message.timestamp) : new Date();
|
|
66
|
+
console[message.level](`%s%s${message?.tag ? "%s" : ""} %s`, LOG_LEVEL_COLORS[message.level](`[${date.toISOString().split("T")[1]?.replace("Z", "")}]`), LOG_LEVEL_COLORS[message.level](`[${message.level.toUpperCase().padEnd(5, " ")}]`), message?.tag ? LOG_LEVEL_COLORS[message.level](`[${message.tag}]`) : "", ...message.args);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
//#endregion
|
|
71
|
+
export { ConsoleProvider, JSONProvider };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { LoggerTypes } from "../Types/LoggerTypes.js";
|
|
2
|
+
import type { Logger } from "../Common/Logger.js";
|
|
3
|
+
export declare class BaseLogger implements Logger {
|
|
4
|
+
private gContainer;
|
|
5
|
+
/**
|
|
6
|
+
* Hold the active log levels for the logger.
|
|
7
|
+
* Default: debug
|
|
8
|
+
*/
|
|
9
|
+
private fLogLevel;
|
|
10
|
+
/**
|
|
11
|
+
* Hold providers
|
|
12
|
+
*/
|
|
13
|
+
private fProviders;
|
|
14
|
+
/**
|
|
15
|
+
* Hold providers level
|
|
16
|
+
*/
|
|
17
|
+
private fProvidersLevel;
|
|
18
|
+
/**
|
|
19
|
+
* Configure logger
|
|
20
|
+
* @param options
|
|
21
|
+
*/
|
|
22
|
+
configure(options: LoggerTypes.Options): void;
|
|
23
|
+
/**
|
|
24
|
+
* Logs an informational message.
|
|
25
|
+
* @param args - Additional parameters to be logged
|
|
26
|
+
* @returns A value determined by the implementing class
|
|
27
|
+
*/
|
|
28
|
+
debug(...args: LoggerTypes.Arg[]): void;
|
|
29
|
+
/**
|
|
30
|
+
* Logs an informational message.
|
|
31
|
+
* @param args - Additional parameters to be logged
|
|
32
|
+
* @returns A value determined by the implementing class
|
|
33
|
+
*/
|
|
34
|
+
info(...args: LoggerTypes.Arg[]): void;
|
|
35
|
+
/**
|
|
36
|
+
* Logs a warning message.
|
|
37
|
+
* @param args - Additional parameters to be logged
|
|
38
|
+
* @returns A value determined by the implementing class
|
|
39
|
+
*/
|
|
40
|
+
warn(...args: LoggerTypes.Arg[]): void;
|
|
41
|
+
/**
|
|
42
|
+
* Logs an error message.
|
|
43
|
+
* @param args - Additional parameters to be logged
|
|
44
|
+
* @returns A value determined by the implementing class
|
|
45
|
+
*/
|
|
46
|
+
error(...args: LoggerTypes.Arg[]): void;
|
|
47
|
+
/**
|
|
48
|
+
* Prints a log message according to the specified format and level.
|
|
49
|
+
* This is an abstract method that should be implemented by subclasses.
|
|
50
|
+
* @param message - The log message object containing level, tag, and arguments
|
|
51
|
+
* @throws {Error} When called directly on BaseLogger (must be implemented by subclass)
|
|
52
|
+
* @protected
|
|
53
|
+
*/
|
|
54
|
+
protected printMessage(message: LoggerTypes.Message): void;
|
|
55
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { IOC } from "@vercube/di";
|
|
2
|
+
import type { LoggerProvider } from "../Common/LoggerProvider.js";
|
|
3
|
+
export declare namespace LoggerTypes {
|
|
4
|
+
type Level = "debug" | "info" | "warn" | "error";
|
|
5
|
+
type Arg = any;
|
|
6
|
+
interface Message {
|
|
7
|
+
level: Level;
|
|
8
|
+
args: Arg[];
|
|
9
|
+
tag?: string;
|
|
10
|
+
pid?: number;
|
|
11
|
+
type?: "access_log" | "application_log";
|
|
12
|
+
timestamp?: number;
|
|
13
|
+
}
|
|
14
|
+
type LogProviderOptions<T extends IOC.Newable<LoggerProvider>> = Parameters<InstanceType<T>["initialize"]>[0] & {
|
|
15
|
+
logLevel?: Level
|
|
16
|
+
};
|
|
17
|
+
interface LogAppender<T extends IOC.Newable<LoggerProvider>> {
|
|
18
|
+
name: string;
|
|
19
|
+
provider: T;
|
|
20
|
+
logLevel?: Level;
|
|
21
|
+
options?: LogProviderOptions<T>;
|
|
22
|
+
}
|
|
23
|
+
interface Options {
|
|
24
|
+
logLevel?: Level;
|
|
25
|
+
providers?: LogAppender<any>[];
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { LoggerTypes } from "../Types/LoggerTypes.js";
|
|
2
|
+
type ColorTextFn = (text: string) => string;
|
|
3
|
+
/**
|
|
4
|
+
* Checks if target level is enabled.
|
|
5
|
+
* @param targetLevel target level
|
|
6
|
+
* @param logLevels array of enabled log levels
|
|
7
|
+
*/
|
|
8
|
+
export declare function isLogLevelEnabled(targetLevel: LoggerTypes.Level, currentLevel: LoggerTypes.Level): boolean;
|
|
9
|
+
export declare const colors: Record<string, ColorTextFn>;
|
|
10
|
+
export declare const LOG_LEVEL_COLORS: Record<LoggerTypes.Level, ColorTextFn>;
|
|
11
|
+
export {};
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __commonJS = (cb, mod) => function() {
|
|
9
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
13
|
+
key = keys[i];
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
15
|
+
get: ((k) => from[k]).bind(null, key),
|
|
16
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
22
|
+
value: mod,
|
|
23
|
+
enumerable: true
|
|
24
|
+
}) : target, mod));
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
const __vercube_di = __toESM(require("@vercube/di"));
|
|
28
|
+
|
|
29
|
+
//#region packages/logger/src/Common/Logger.ts
|
|
30
|
+
var Logger = class {};
|
|
31
|
+
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region packages/logger/src/Utils/Utils.ts
|
|
34
|
+
const LOG_LEVEL_VALUES = {
|
|
35
|
+
debug: 1,
|
|
36
|
+
info: 2,
|
|
37
|
+
warn: 3,
|
|
38
|
+
error: 4
|
|
39
|
+
};
|
|
40
|
+
function isLogLevelEnabled(targetLevel, currentLevel) {
|
|
41
|
+
return LOG_LEVEL_VALUES[targetLevel] >= LOG_LEVEL_VALUES[currentLevel];
|
|
42
|
+
}
|
|
43
|
+
const isColorAllowed = () => !process.env.NO_COLOR;
|
|
44
|
+
const colorIfAllowed = (colorFn) => {
|
|
45
|
+
const wrappedFn = (text) => {
|
|
46
|
+
return isColorAllowed() ? colorFn(text) : text;
|
|
47
|
+
};
|
|
48
|
+
return wrappedFn;
|
|
49
|
+
};
|
|
50
|
+
const colors = {
|
|
51
|
+
bold: colorIfAllowed((text) => `\x1B[1m${text}\x1B[0m`),
|
|
52
|
+
green: colorIfAllowed((text) => `\x1B[32m${text}\x1B[39m`),
|
|
53
|
+
yellow: colorIfAllowed((text) => `\x1B[33m${text}\x1B[39m`),
|
|
54
|
+
red: colorIfAllowed((text) => `\x1B[31m${text}\x1B[39m`),
|
|
55
|
+
magentaBright: colorIfAllowed((text) => `\x1B[95m${text}\x1B[39m`),
|
|
56
|
+
cyanBright: colorIfAllowed((text) => `\x1B[96m${text}\x1B[39m`)
|
|
57
|
+
};
|
|
58
|
+
const LOG_LEVEL_COLORS = {
|
|
59
|
+
debug: colors.green,
|
|
60
|
+
info: colors.bold,
|
|
61
|
+
warn: colors.yellow,
|
|
62
|
+
error: colors.red
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
//#endregion
|
|
66
|
+
//#region node_modules/.pnpm/@oxc-project+runtime@0.62.0/node_modules/@oxc-project/runtime/src/helpers/decorate.js
|
|
67
|
+
var require_decorate = __commonJS({ "node_modules/.pnpm/@oxc-project+runtime@0.62.0/node_modules/@oxc-project/runtime/src/helpers/decorate.js"(exports, module) {
|
|
68
|
+
function __decorate(decorators, target, key, desc) {
|
|
69
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
70
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
71
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
72
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
73
|
+
}
|
|
74
|
+
module.exports = __decorate, module.exports.__esModule = true, module.exports["default"] = module.exports;
|
|
75
|
+
} });
|
|
76
|
+
var import_decorate = __toESM(require_decorate(), 1);
|
|
77
|
+
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region packages/logger/src/Service/BaseLogger.ts
|
|
80
|
+
var BaseLogger = class {
|
|
81
|
+
gContainer;
|
|
82
|
+
/**
|
|
83
|
+
* Hold the active log levels for the logger.
|
|
84
|
+
* Default: debug
|
|
85
|
+
*/
|
|
86
|
+
fLogLevel = "debug";
|
|
87
|
+
/**
|
|
88
|
+
* Hold providers
|
|
89
|
+
*/
|
|
90
|
+
fProviders = new Map();
|
|
91
|
+
/**
|
|
92
|
+
* Hold providers level
|
|
93
|
+
*/
|
|
94
|
+
fProvidersLevel = new Map();
|
|
95
|
+
/**
|
|
96
|
+
* Configure logger
|
|
97
|
+
* @param options
|
|
98
|
+
*/
|
|
99
|
+
configure(options) {
|
|
100
|
+
this.fLogLevel = options?.logLevel ?? "debug";
|
|
101
|
+
if (!options?.providers?.length) return;
|
|
102
|
+
this.fProviders.clear();
|
|
103
|
+
for (const logger of options?.providers ?? []) try {
|
|
104
|
+
const provider = this.gContainer.resolve(logger.provider);
|
|
105
|
+
provider.initialize(logger.options);
|
|
106
|
+
this.fProviders.set(logger.name, provider);
|
|
107
|
+
this.fProvidersLevel.set(logger.name, logger.logLevel ?? this.fLogLevel);
|
|
108
|
+
} catch (error) {
|
|
109
|
+
console.error(`Failed to initialize logger provider: ${logger.provider.name}`, error);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Logs an informational message.
|
|
114
|
+
* @param args - Additional parameters to be logged
|
|
115
|
+
* @returns A value determined by the implementing class
|
|
116
|
+
*/
|
|
117
|
+
debug(...args) {
|
|
118
|
+
this.printMessage({
|
|
119
|
+
level: "debug",
|
|
120
|
+
args
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Logs an informational message.
|
|
125
|
+
* @param args - Additional parameters to be logged
|
|
126
|
+
* @returns A value determined by the implementing class
|
|
127
|
+
*/
|
|
128
|
+
info(...args) {
|
|
129
|
+
this.printMessage({
|
|
130
|
+
level: "info",
|
|
131
|
+
args
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Logs a warning message.
|
|
136
|
+
* @param args - Additional parameters to be logged
|
|
137
|
+
* @returns A value determined by the implementing class
|
|
138
|
+
*/
|
|
139
|
+
warn(...args) {
|
|
140
|
+
this.printMessage({
|
|
141
|
+
level: "warn",
|
|
142
|
+
args
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Logs an error message.
|
|
147
|
+
* @param args - Additional parameters to be logged
|
|
148
|
+
* @returns A value determined by the implementing class
|
|
149
|
+
*/
|
|
150
|
+
error(...args) {
|
|
151
|
+
this.printMessage({
|
|
152
|
+
level: "error",
|
|
153
|
+
args
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Prints a log message according to the specified format and level.
|
|
158
|
+
* This is an abstract method that should be implemented by subclasses.
|
|
159
|
+
* @param message - The log message object containing level, tag, and arguments
|
|
160
|
+
* @throws {Error} When called directly on BaseLogger (must be implemented by subclass)
|
|
161
|
+
* @protected
|
|
162
|
+
*/
|
|
163
|
+
printMessage(message) {
|
|
164
|
+
const providersToProcess = [...this.fProviders.entries()].filter(([name]) => {
|
|
165
|
+
return isLogLevelEnabled(message.level, this.fProvidersLevel.get(name) ?? this.fLogLevel);
|
|
166
|
+
});
|
|
167
|
+
for (const [, provider] of providersToProcess) provider.processMessage(message);
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
(0, import_decorate.default)([(0, __vercube_di.Inject)(__vercube_di.Container)], BaseLogger.prototype, "gContainer", void 0);
|
|
171
|
+
|
|
172
|
+
//#endregion
|
|
173
|
+
exports.BaseLogger = BaseLogger
|
|
174
|
+
exports.Logger = Logger
|
|
175
|
+
exports.isLogLevelEnabled = isLogLevelEnabled
|
package/dist/index.d.ts
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { Container, Inject } from "@vercube/di";
|
|
2
|
+
|
|
3
|
+
//#region packages/logger/src/Common/Logger.ts
|
|
4
|
+
var Logger = class {};
|
|
5
|
+
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region packages/logger/src/Utils/Utils.ts
|
|
8
|
+
const LOG_LEVEL_VALUES = {
|
|
9
|
+
debug: 1,
|
|
10
|
+
info: 2,
|
|
11
|
+
warn: 3,
|
|
12
|
+
error: 4
|
|
13
|
+
};
|
|
14
|
+
function isLogLevelEnabled(targetLevel, currentLevel) {
|
|
15
|
+
return LOG_LEVEL_VALUES[targetLevel] >= LOG_LEVEL_VALUES[currentLevel];
|
|
16
|
+
}
|
|
17
|
+
const isColorAllowed = () => !process.env.NO_COLOR;
|
|
18
|
+
const colorIfAllowed = (colorFn) => {
|
|
19
|
+
const wrappedFn = (text) => {
|
|
20
|
+
return isColorAllowed() ? colorFn(text) : text;
|
|
21
|
+
};
|
|
22
|
+
return wrappedFn;
|
|
23
|
+
};
|
|
24
|
+
const colors = {
|
|
25
|
+
bold: colorIfAllowed((text) => `\x1B[1m${text}\x1B[0m`),
|
|
26
|
+
green: colorIfAllowed((text) => `\x1B[32m${text}\x1B[39m`),
|
|
27
|
+
yellow: colorIfAllowed((text) => `\x1B[33m${text}\x1B[39m`),
|
|
28
|
+
red: colorIfAllowed((text) => `\x1B[31m${text}\x1B[39m`),
|
|
29
|
+
magentaBright: colorIfAllowed((text) => `\x1B[95m${text}\x1B[39m`),
|
|
30
|
+
cyanBright: colorIfAllowed((text) => `\x1B[96m${text}\x1B[39m`)
|
|
31
|
+
};
|
|
32
|
+
const LOG_LEVEL_COLORS = {
|
|
33
|
+
debug: colors.green,
|
|
34
|
+
info: colors.bold,
|
|
35
|
+
warn: colors.yellow,
|
|
36
|
+
error: colors.red
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region node_modules/.pnpm/@oxc-project+runtime@0.62.0/node_modules/@oxc-project/runtime/src/helpers/esm/decorate.js
|
|
41
|
+
function __decorate(decorators, target, key, desc) {
|
|
42
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
43
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
44
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
45
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
//#endregion
|
|
49
|
+
//#region packages/logger/src/Service/BaseLogger.ts
|
|
50
|
+
var BaseLogger = class {
|
|
51
|
+
gContainer;
|
|
52
|
+
/**
|
|
53
|
+
* Hold the active log levels for the logger.
|
|
54
|
+
* Default: debug
|
|
55
|
+
*/
|
|
56
|
+
fLogLevel = "debug";
|
|
57
|
+
/**
|
|
58
|
+
* Hold providers
|
|
59
|
+
*/
|
|
60
|
+
fProviders = new Map();
|
|
61
|
+
/**
|
|
62
|
+
* Hold providers level
|
|
63
|
+
*/
|
|
64
|
+
fProvidersLevel = new Map();
|
|
65
|
+
/**
|
|
66
|
+
* Configure logger
|
|
67
|
+
* @param options
|
|
68
|
+
*/
|
|
69
|
+
configure(options) {
|
|
70
|
+
this.fLogLevel = options?.logLevel ?? "debug";
|
|
71
|
+
if (!options?.providers?.length) return;
|
|
72
|
+
this.fProviders.clear();
|
|
73
|
+
for (const logger of options?.providers ?? []) try {
|
|
74
|
+
const provider = this.gContainer.resolve(logger.provider);
|
|
75
|
+
provider.initialize(logger.options);
|
|
76
|
+
this.fProviders.set(logger.name, provider);
|
|
77
|
+
this.fProvidersLevel.set(logger.name, logger.logLevel ?? this.fLogLevel);
|
|
78
|
+
} catch (error) {
|
|
79
|
+
console.error(`Failed to initialize logger provider: ${logger.provider.name}`, error);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Logs an informational message.
|
|
84
|
+
* @param args - Additional parameters to be logged
|
|
85
|
+
* @returns A value determined by the implementing class
|
|
86
|
+
*/
|
|
87
|
+
debug(...args) {
|
|
88
|
+
this.printMessage({
|
|
89
|
+
level: "debug",
|
|
90
|
+
args
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Logs an informational message.
|
|
95
|
+
* @param args - Additional parameters to be logged
|
|
96
|
+
* @returns A value determined by the implementing class
|
|
97
|
+
*/
|
|
98
|
+
info(...args) {
|
|
99
|
+
this.printMessage({
|
|
100
|
+
level: "info",
|
|
101
|
+
args
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Logs a warning message.
|
|
106
|
+
* @param args - Additional parameters to be logged
|
|
107
|
+
* @returns A value determined by the implementing class
|
|
108
|
+
*/
|
|
109
|
+
warn(...args) {
|
|
110
|
+
this.printMessage({
|
|
111
|
+
level: "warn",
|
|
112
|
+
args
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Logs an error message.
|
|
117
|
+
* @param args - Additional parameters to be logged
|
|
118
|
+
* @returns A value determined by the implementing class
|
|
119
|
+
*/
|
|
120
|
+
error(...args) {
|
|
121
|
+
this.printMessage({
|
|
122
|
+
level: "error",
|
|
123
|
+
args
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Prints a log message according to the specified format and level.
|
|
128
|
+
* This is an abstract method that should be implemented by subclasses.
|
|
129
|
+
* @param message - The log message object containing level, tag, and arguments
|
|
130
|
+
* @throws {Error} When called directly on BaseLogger (must be implemented by subclass)
|
|
131
|
+
* @protected
|
|
132
|
+
*/
|
|
133
|
+
printMessage(message) {
|
|
134
|
+
const providersToProcess = [...this.fProviders.entries()].filter(([name]) => {
|
|
135
|
+
return isLogLevelEnabled(message.level, this.fProvidersLevel.get(name) ?? this.fLogLevel);
|
|
136
|
+
});
|
|
137
|
+
for (const [, provider] of providersToProcess) provider.processMessage(message);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
__decorate([Inject(Container)], BaseLogger.prototype, "gContainer", void 0);
|
|
141
|
+
|
|
142
|
+
//#endregion
|
|
143
|
+
export { BaseLogger, Logger, isLogLevelEnabled };
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vercube/logger",
|
|
3
|
+
"version": "0.0.1-alpha.15",
|
|
4
|
+
"description": "Logger module for Vercube framework",
|
|
5
|
+
"repository": "@vercube/logger",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"type": "module",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.mjs",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
},
|
|
15
|
+
"./providers": {
|
|
16
|
+
"types": "./dist/Providers/index.d.ts",
|
|
17
|
+
"import": "./dist/Providers/index.mjs",
|
|
18
|
+
"require": "./dist/Providers/index.cjs"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"main": "./dist/index.cjs",
|
|
22
|
+
"module": "./dist/index.mjs",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@vercube/di": "0.0.1-alpha.15"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
}
|
|
34
|
+
}
|