@vvlad1973/simple-logger 2.1.7 → 2.1.8

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.
@@ -1,9 +1,9 @@
1
- export declare const LoggerLevels: {
2
- readonly TRACE: "trace";
3
- readonly DEBUG: "debug";
4
- readonly INFO: "info";
5
- readonly WARN: "warn";
6
- readonly ERROR: "error";
7
- readonly FATAL: "fatal";
8
- readonly SILENT: "silent";
9
- };
1
+ export declare const LoggerLevels: {
2
+ readonly TRACE: "trace";
3
+ readonly DEBUG: "debug";
4
+ readonly INFO: "info";
5
+ readonly WARN: "warn";
6
+ readonly ERROR: "error";
7
+ readonly FATAL: "fatal";
8
+ readonly SILENT: "silent";
9
+ };
@@ -1,10 +1,10 @@
1
- export const LoggerLevels = {
2
- TRACE: 'trace',
3
- DEBUG: 'debug',
4
- INFO: 'info',
5
- WARN: 'warn',
6
- ERROR: 'error',
7
- FATAL: 'fatal',
8
- SILENT: 'silent',
9
- };
1
+ export const LoggerLevels = {
2
+ TRACE: 'trace',
3
+ DEBUG: 'debug',
4
+ INFO: 'info',
5
+ WARN: 'warn',
6
+ ERROR: 'error',
7
+ FATAL: 'fatal',
8
+ SILENT: 'silent',
9
+ };
10
10
  //# sourceMappingURL=constants.js.map
@@ -1,31 +1,31 @@
1
- import { LoggerLevel, PreparedLogCall } from '../types/simple-logger.types.js';
2
- /**
3
- * Extracts the message and rest of the arguments from the provided array.
4
- *
5
- * If the first argument is an object and the second argument is a string, the second argument is considered the message.
6
- * If the first argument is a string, it is considered the message.
7
- * Otherwise, an empty message is returned.
8
- *
9
- * @param {unknown[]} args - The array of arguments to extract the message from.
10
- * @return {{ msg: string; rest: unknown[] }} An object containing the extracted message and the rest of the arguments.
11
- */
12
- export declare function extractMessage(args: unknown[]): {
13
- msg: string;
14
- rest: unknown[];
15
- };
16
- /**
17
- * Extracts the context object from the provided array of arguments.
18
- *
19
- * @param {unknown[]} args - The array of arguments to extract the context from.
20
- * @return {Record<string, unknown>} The extracted context object, or an empty object if no context is found.
21
- */
22
- export declare function extractContext(args: unknown[]): Record<string, unknown>;
23
- /**
24
- * Returns a string with the provided label colored according to the specified logging level.
25
- *
26
- * @param {LoggerLevel} level - The logging level to determine the color from.
27
- * @param {string} label - The label to be colored.
28
- * @return {string} The colored label.
29
- */
30
- export declare function colorizeLevel(level: LoggerLevel, label: string): string;
31
- export declare function prepareLogCall(args: unknown[]): PreparedLogCall;
1
+ import { LoggerLevel, PreparedLogCall } from '../types/simple-logger.types.js';
2
+ /**
3
+ * Extracts the message and rest of the arguments from the provided array.
4
+ *
5
+ * If the first argument is an object and the second argument is a string, the second argument is considered the message.
6
+ * If the first argument is a string, it is considered the message.
7
+ * Otherwise, an empty message is returned.
8
+ *
9
+ * @param {unknown[]} args - The array of arguments to extract the message from.
10
+ * @return {{ msg: string; rest: unknown[] }} An object containing the extracted message and the rest of the arguments.
11
+ */
12
+ export declare function extractMessage(args: unknown[]): {
13
+ msg: string;
14
+ rest: unknown[];
15
+ };
16
+ /**
17
+ * Extracts the context object from the provided array of arguments.
18
+ *
19
+ * @param {unknown[]} args - The array of arguments to extract the context from.
20
+ * @return {Record<string, unknown>} The extracted context object, or an empty object if no context is found.
21
+ */
22
+ export declare function extractContext(args: unknown[]): Record<string, unknown>;
23
+ /**
24
+ * Returns a string with the provided label colored according to the specified logging level.
25
+ *
26
+ * @param {LoggerLevel} level - The logging level to determine the color from.
27
+ * @param {string} label - The label to be colored.
28
+ * @return {string} The colored label.
29
+ */
30
+ export declare function colorizeLevel(level: LoggerLevel, label: string): string;
31
+ export declare function prepareLogCall(args: unknown[]): PreparedLogCall;
@@ -1,81 +1,81 @@
1
- import chalk from 'chalk';
2
- import { LoggerLevels } from '../constants/constants.js';
3
- /**
4
- * Extracts the message and rest of the arguments from the provided array.
5
- *
6
- * If the first argument is an object and the second argument is a string, the second argument is considered the message.
7
- * If the first argument is a string, it is considered the message.
8
- * Otherwise, an empty message is returned.
9
- *
10
- * @param {unknown[]} args - The array of arguments to extract the message from.
11
- * @return {{ msg: string; rest: unknown[] }} An object containing the extracted message and the rest of the arguments.
12
- */
13
- export function extractMessage(args) {
14
- const [first, second, ...rest] = args;
15
- if (typeof first === 'object' &&
16
- first !== null &&
17
- typeof second === 'string') {
18
- return { msg: second, rest };
19
- }
20
- if (typeof first === 'string') {
21
- return {
22
- msg: first,
23
- rest: [second, ...rest].filter((value) => value !== undefined),
24
- };
25
- }
26
- return { msg: '', rest: [] };
27
- }
28
- /**
29
- * Extracts the context object from the provided array of arguments.
30
- *
31
- * @param {unknown[]} args - The array of arguments to extract the context from.
32
- * @return {Record<string, unknown>} The extracted context object, or an empty object if no context is found.
33
- */
34
- export function extractContext(args) {
35
- const [first] = args;
36
- if (typeof first === 'object' && first !== null && !Array.isArray(first)) {
37
- return first;
38
- }
39
- return {};
40
- }
41
- /**
42
- * Returns a string with the provided label colored according to the specified logging level.
43
- *
44
- * @param {LoggerLevel} level - The logging level to determine the color from.
45
- * @param {string} label - The label to be colored.
46
- * @return {string} The colored label.
47
- */
48
- export function colorizeLevel(level, label) {
49
- switch (level) {
50
- case LoggerLevels.DEBUG:
51
- case LoggerLevels.TRACE:
52
- return chalk.gray(label);
53
- case LoggerLevels.INFO:
54
- return chalk.green(label);
55
- case LoggerLevels.WARN:
56
- return chalk.yellow(label);
57
- case LoggerLevels.ERROR:
58
- case LoggerLevels.FATAL:
59
- return chalk.red(label);
60
- default:
61
- return label;
62
- }
63
- /**
64
- * Prepares the log call by rearranging the provided arguments into a standardized format.
65
- *
66
- * @param {unknown[]} args - The array of arguments to prepare for the log call.
67
- * @return {PreparedLogCall} The prepared log call arguments, or null if the input is invalid.
68
- */
69
- }
70
- export function prepareLogCall(args) {
71
- if (typeof args[0] === 'string') {
72
- return [args[0], ...args.slice(1)];
73
- }
74
- if (typeof args[0] === 'object' && args[0] !== null) {
75
- const msg = typeof args[1] === 'string' ? args[1] : undefined;
76
- const rest = args.slice(msg ? 2 : 1);
77
- return [args[0], msg, ...rest];
78
- }
79
- return null;
80
- }
1
+ import chalk from 'chalk';
2
+ import { LoggerLevels } from '../constants/constants.js';
3
+ /**
4
+ * Extracts the message and rest of the arguments from the provided array.
5
+ *
6
+ * If the first argument is an object and the second argument is a string, the second argument is considered the message.
7
+ * If the first argument is a string, it is considered the message.
8
+ * Otherwise, an empty message is returned.
9
+ *
10
+ * @param {unknown[]} args - The array of arguments to extract the message from.
11
+ * @return {{ msg: string; rest: unknown[] }} An object containing the extracted message and the rest of the arguments.
12
+ */
13
+ export function extractMessage(args) {
14
+ const [first, second, ...rest] = args;
15
+ if (typeof first === 'object' &&
16
+ first !== null &&
17
+ typeof second === 'string') {
18
+ return { msg: second, rest };
19
+ }
20
+ if (typeof first === 'string') {
21
+ return {
22
+ msg: first,
23
+ rest: [second, ...rest].filter((value) => value !== undefined),
24
+ };
25
+ }
26
+ return { msg: '', rest: [] };
27
+ }
28
+ /**
29
+ * Extracts the context object from the provided array of arguments.
30
+ *
31
+ * @param {unknown[]} args - The array of arguments to extract the context from.
32
+ * @return {Record<string, unknown>} The extracted context object, or an empty object if no context is found.
33
+ */
34
+ export function extractContext(args) {
35
+ const [first] = args;
36
+ if (typeof first === 'object' && first !== null && !Array.isArray(first)) {
37
+ return first;
38
+ }
39
+ return {};
40
+ }
41
+ /**
42
+ * Returns a string with the provided label colored according to the specified logging level.
43
+ *
44
+ * @param {LoggerLevel} level - The logging level to determine the color from.
45
+ * @param {string} label - The label to be colored.
46
+ * @return {string} The colored label.
47
+ */
48
+ export function colorizeLevel(level, label) {
49
+ switch (level) {
50
+ case LoggerLevels.DEBUG:
51
+ case LoggerLevels.TRACE:
52
+ return chalk.gray(label);
53
+ case LoggerLevels.INFO:
54
+ return chalk.green(label);
55
+ case LoggerLevels.WARN:
56
+ return chalk.yellow(label);
57
+ case LoggerLevels.ERROR:
58
+ case LoggerLevels.FATAL:
59
+ return chalk.red(label);
60
+ default:
61
+ return label;
62
+ }
63
+ /**
64
+ * Prepares the log call by rearranging the provided arguments into a standardized format.
65
+ *
66
+ * @param {unknown[]} args - The array of arguments to prepare for the log call.
67
+ * @return {PreparedLogCall} The prepared log call arguments, or null if the input is invalid.
68
+ */
69
+ }
70
+ export function prepareLogCall(args) {
71
+ if (typeof args[0] === 'string') {
72
+ return [args[0], ...args.slice(1)];
73
+ }
74
+ if (typeof args[0] === 'object' && args[0] !== null) {
75
+ const msg = typeof args[1] === 'string' ? args[1] : undefined;
76
+ const rest = args.slice(msg ? 2 : 1);
77
+ return [args[0], msg, ...rest];
78
+ }
79
+ return null;
80
+ }
81
81
  //# sourceMappingURL=helpers.js.map
@@ -1,17 +1,17 @@
1
- import { ExternalLogger } from '../types/simple-logger.types.js';
2
- /**
3
- * Checks if the provided logger is a valid ExternalLogger instance.
4
- *
5
- * @param {unknown} logger - The logger to validate.
6
- * @return {logger is ExternalLogger} True if the logger is a valid ExternalLogger, false otherwise.
7
- */
8
- export declare function isValidLogger(logger: unknown): logger is ExternalLogger;
9
- /**
10
- * Checks if the provided logger is an ExternalLogger instance with a child method.
11
- *
12
- * @param {unknown} logger - The logger to check.
13
- * @return {logger is ExternalLogger & { child: Function }} True if the logger is an ExternalLogger with a child method, false otherwise.
14
- */
15
- export declare function hasChild(logger: unknown): logger is ExternalLogger & {
16
- child: Function;
17
- };
1
+ import { ExternalLogger } from '../types/simple-logger.types.js';
2
+ /**
3
+ * Checks if the provided logger is a valid ExternalLogger instance.
4
+ *
5
+ * @param {unknown} logger - The logger to validate.
6
+ * @return {logger is ExternalLogger} True if the logger is a valid ExternalLogger, false otherwise.
7
+ */
8
+ export declare function isValidLogger(logger: unknown): logger is ExternalLogger;
9
+ /**
10
+ * Checks if the provided logger is an ExternalLogger instance with a child method.
11
+ *
12
+ * @param {unknown} logger - The logger to check.
13
+ * @return {logger is ExternalLogger & { child: Function }} True if the logger is an ExternalLogger with a child method, false otherwise.
14
+ */
15
+ export declare function hasChild(logger: unknown): logger is ExternalLogger & {
16
+ child: Function;
17
+ };
@@ -1,27 +1,27 @@
1
- import { LoggerLevels } from '../constants/constants.js';
2
- /**
3
- * Checks if the provided logger is a valid ExternalLogger instance.
4
- *
5
- * @param {unknown} logger - The logger to validate.
6
- * @return {logger is ExternalLogger} True if the logger is a valid ExternalLogger, false otherwise.
7
- */
8
- export function isValidLogger(logger) {
9
- const levels = Object.values(LoggerLevels);
10
- if (typeof logger !== 'object' || logger === null) {
11
- return false;
12
- }
13
- const candidate = logger;
14
- return levels.some((level) => typeof candidate[level] === 'function');
15
- }
16
- /**
17
- * Checks if the provided logger is an ExternalLogger instance with a child method.
18
- *
19
- * @param {unknown} logger - The logger to check.
20
- * @return {logger is ExternalLogger & { child: Function }} True if the logger is an ExternalLogger with a child method, false otherwise.
21
- */
22
- export function hasChild(logger) {
23
- return (typeof logger === 'object' &&
24
- logger !== null &&
25
- typeof logger.child === 'function');
26
- }
1
+ import { LoggerLevels } from '../constants/constants.js';
2
+ /**
3
+ * Checks if the provided logger is a valid ExternalLogger instance.
4
+ *
5
+ * @param {unknown} logger - The logger to validate.
6
+ * @return {logger is ExternalLogger} True if the logger is a valid ExternalLogger, false otherwise.
7
+ */
8
+ export function isValidLogger(logger) {
9
+ const levels = Object.values(LoggerLevels);
10
+ if (typeof logger !== 'object' || logger === null) {
11
+ return false;
12
+ }
13
+ const candidate = logger;
14
+ return levels.some((level) => typeof candidate[level] === 'function');
15
+ }
16
+ /**
17
+ * Checks if the provided logger is an ExternalLogger instance with a child method.
18
+ *
19
+ * @param {unknown} logger - The logger to check.
20
+ * @return {logger is ExternalLogger & { child: Function }} True if the logger is an ExternalLogger with a child method, false otherwise.
21
+ */
22
+ export function hasChild(logger) {
23
+ return (typeof logger === 'object' &&
24
+ logger !== null &&
25
+ typeof logger.child === 'function');
26
+ }
27
27
  //# sourceMappingURL=validators.js.map
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import SimpleLogger from './classes/simple-logger.js';
2
- export default SimpleLogger;
3
- export { SimpleLogger };
4
- export * from './types/simple-logger.types.js';
5
- export * from './constants/constants.js';
1
+ import SimpleLogger from './classes/simple-logger.js';
2
+ export default SimpleLogger;
3
+ export { SimpleLogger };
4
+ export * from './types/simple-logger.types.js';
5
+ export * from './constants/constants.js';
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
- import SimpleLogger from './classes/simple-logger.js';
2
- export default SimpleLogger;
3
- export { SimpleLogger };
4
- export * from './types/simple-logger.types.js';
5
- export * from './constants/constants.js';
1
+ import SimpleLogger from './classes/simple-logger.js';
2
+ export default SimpleLogger;
3
+ export { SimpleLogger };
4
+ export * from './types/simple-logger.types.js';
5
+ export * from './constants/constants.js';
6
6
  //# sourceMappingURL=index.js.map
@@ -1,80 +1,80 @@
1
- /**
2
- * A module for simple logging with various logging levels.
3
- * @module SimpleLogger
4
- */
5
- export type ExternalLogger = {
6
- trace?: LogFn;
7
- debug?: LogFn;
8
- info?: LogFn;
9
- warn?: LogFn;
10
- error?: LogFn;
11
- fatal?: LogFn;
12
- silent?: LogFn;
13
- level?: string;
14
- };
15
- interface LogFn {
16
- <T extends object>(obj: T, msg?: string, ...args: any[]): void;
17
- (obj: unknown, msg?: string, ...args: any[]): void;
18
- (msg: string, ...args: any[]): void;
19
- }
20
- declare const loggerLevels: readonly ["trace", "debug", "info", "warn", "error", "fatal", "silent"];
21
- export type LoggerLevel = (typeof loggerLevels)[number];
22
- /**
23
- * A simple logger class with various logging levels.
24
- * @class
25
- * @param {string} [level='info'] - Initial logging level.
26
- * @param {ExternalLogger | undefined | null} [externalLogger=null] - Optional external logger to use.
27
- */
28
- export declare class SimpleLogger {
29
- #private;
30
- isExternal: boolean;
31
- constructor(level?: LoggerLevel, externalLogger?: ExternalLogger);
32
- trace: LogFn;
33
- debug: LogFn;
34
- info: LogFn;
35
- warn: LogFn;
36
- error: LogFn;
37
- fatal: LogFn;
38
- silent: LogFn;
39
- /**
40
- * Updates the logging methods based on the current logging level.
41
- * @private
42
- */
43
- private updateLoggingMethods;
44
- /**
45
- * Checks if the provided logger is valid.
46
- * @private
47
- * @param {any} logger - The logger to check.
48
- * @returns {boolean} - True if the logger is valid, otherwise false.
49
- */
50
- private isValidLogger;
51
- /**
52
- * Gets the external logger method for a specific level.
53
- * @param {string} method - The logger method to get.
54
- * @param {string} [fallbackMethod] - The fallback logger method to get.
55
- * @returns {LoggerMethod} - The logger method or a no-op function.
56
- * @private
57
- */
58
- private getExternalLoggerMethod;
59
- /**
60
- * Sets the logging level.
61
- * @param {string} level - The logging level to set.
62
- */
63
- setLevel(level: string): void;
64
- /**
65
- * Sets an external logger to be used.
66
- * @param {ExternalLogger | undefined | null} logger - The external logger to set.
67
- */
68
- setExternalLogger(logger: ExternalLogger | undefined | null, level?: LoggerLevel): void;
69
- /**
70
- * Logs the start of a function.
71
- * @param {string} [functionName] - Optional function name to log.
72
- */
73
- logFunctionStart(functionName?: string): void;
74
- /**
75
- * Logs the end of a function.
76
- * @param {string} [functionName] - Optional function name to log.
77
- */
78
- logFunctionEnd(functionName?: string): void;
79
- }
80
- export default SimpleLogger;
1
+ /**
2
+ * A module for simple logging with various logging levels.
3
+ * @module SimpleLogger
4
+ */
5
+ export type ExternalLogger = {
6
+ trace?: LogFn;
7
+ debug?: LogFn;
8
+ info?: LogFn;
9
+ warn?: LogFn;
10
+ error?: LogFn;
11
+ fatal?: LogFn;
12
+ silent?: LogFn;
13
+ level?: string;
14
+ };
15
+ interface LogFn {
16
+ <T extends object>(obj: T, msg?: string, ...args: any[]): void;
17
+ (obj: unknown, msg?: string, ...args: any[]): void;
18
+ (msg: string, ...args: any[]): void;
19
+ }
20
+ declare const loggerLevels: readonly ["trace", "debug", "info", "warn", "error", "fatal", "silent"];
21
+ export type LoggerLevel = (typeof loggerLevels)[number];
22
+ /**
23
+ * A simple logger class with various logging levels.
24
+ * @class
25
+ * @param {string} [level='info'] - Initial logging level.
26
+ * @param {ExternalLogger | undefined | null} [externalLogger=null] - Optional external logger to use.
27
+ */
28
+ export declare class SimpleLogger {
29
+ #private;
30
+ isExternal: boolean;
31
+ constructor(level?: LoggerLevel, externalLogger?: ExternalLogger);
32
+ trace: LogFn;
33
+ debug: LogFn;
34
+ info: LogFn;
35
+ warn: LogFn;
36
+ error: LogFn;
37
+ fatal: LogFn;
38
+ silent: LogFn;
39
+ /**
40
+ * Updates the logging methods based on the current logging level.
41
+ * @private
42
+ */
43
+ private updateLoggingMethods;
44
+ /**
45
+ * Checks if the provided logger is valid.
46
+ * @private
47
+ * @param {any} logger - The logger to check.
48
+ * @returns {boolean} - True if the logger is valid, otherwise false.
49
+ */
50
+ private isValidLogger;
51
+ /**
52
+ * Gets the external logger method for a specific level.
53
+ * @param {string} method - The logger method to get.
54
+ * @param {string} [fallbackMethod] - The fallback logger method to get.
55
+ * @returns {LoggerMethod} - The logger method or a no-op function.
56
+ * @private
57
+ */
58
+ private getExternalLoggerMethod;
59
+ /**
60
+ * Sets the logging level.
61
+ * @param {string} level - The logging level to set.
62
+ */
63
+ setLevel(level: string): void;
64
+ /**
65
+ * Sets an external logger to be used.
66
+ * @param {ExternalLogger | undefined | null} logger - The external logger to set.
67
+ */
68
+ setExternalLogger(logger: ExternalLogger | undefined | null, level?: LoggerLevel): void;
69
+ /**
70
+ * Logs the start of a function.
71
+ * @param {string} [functionName] - Optional function name to log.
72
+ */
73
+ logFunctionStart(functionName?: string): void;
74
+ /**
75
+ * Logs the end of a function.
76
+ * @param {string} [functionName] - Optional function name to log.
77
+ */
78
+ logFunctionEnd(functionName?: string): void;
79
+ }
80
+ export default SimpleLogger;