@basalt.lab/basalt-logger 1.10.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Basalt
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # 📦 Basalt-logger
2
+
3
+ ## 📌 Table of contents
4
+
5
+ - [📦 Basalt-logger](#-basalt-logger)
6
+ - [📌 Table of contents](#-table-of-contents)
7
+ - [📝 Description](#-description)
8
+ - [🌟 Documentation](#-documentation)
9
+ - [🔧 Installation](#-installation)
10
+ - [⚖️ License](#-license)
11
+ - [📧 Contact](#-contact)
12
+
13
+ ## 📝 Description
14
+
15
+ `Basalt-logger` is a package that allows you to log events in your application in a simple and elegant way.
16
+ `Basalt-logger` is non-blocking and allows you to have multiple logging strategies. This means you can create your own logging strategies and choose to log in all strategies or only in certain ones.
17
+
18
+ ## 🌟 Documentation
19
+
20
+ - [Documentation](https://basalt-lab.github.io/basalt-doc/index.html).
21
+ - [References](https://basalt-lab.github.io/basalt-logger/index.html).
22
+
23
+ ## 🔧 Installation
24
+
25
+ NPM:
26
+ ```bash
27
+ npm i @basalt-lab/basalt-logger
28
+ ```
29
+
30
+ PNPM:
31
+ ```bash
32
+ pnpm i @basalt-lab/basalt-logger
33
+ ```
34
+
35
+ ## ⚖️ License
36
+
37
+ Distributed under the MIT License. See LICENSE for more information.
38
+
39
+ ## 📧 Contact
40
+
41
+ Mail - [basalt-lab@proton.me](basalt-lab@proton.me)
42
+
43
+ [Project link](https://github.com/Basalt-Lab/basalt-logger)
File without changes
@@ -0,0 +1,2 @@
1
+ import{randomUUID as k}from"crypto";class q extends Error{_uuidError=k();_date=new Date;_code;_detail;constructor(j){super();if(this._code=j.code??500,this.message=j.messageKey,this._detail=j.detail,this.name="BasaltError",Error.captureStackTrace)Error.captureStackTrace(this,this.constructor)}get uuidError(){return this._uuidError}get date(){return this._date}get code(){return this._code}get detail(){return this._detail}}var x={STRATEGY_ALREADY_ADDED:"error.basalt-logger.strategy_already_added",STRATEGY_NOT_FOUND:"error.basalt-logger.strategy_not_found",NO_STRATEGY_ADDED:"error.basalt-logger.no_strategy_added"};
2
+ export{q as b,x as c};
@@ -0,0 +1,2 @@
1
+ var j;((h)=>{h.ERROR="ERROR";h.WARN="WARN";h.INFO="INFO";h.DEBUG="DEBUG";h.LOG="LOG"})(j||={});
2
+ export{j as f};
@@ -0,0 +1,2 @@
1
+ class J{_colorize;constructor(x=!1){this._colorize=x}log(x,G,A){let H=this._colorize?"\x1B[33m":"",E=this._colorize?"\x1B[0m":"",I=typeof A==="string"?A:JSON.stringify(A),q="";switch(x){case"ERROR":q=this._colorize?"\x1B[31m":"";break;case"WARN":q=this._colorize?"\x1B[33m":"";break;case"INFO":q=this._colorize?"\x1B[36m":"";break;case"DEBUG":q=this._colorize?"\x1B[35m":"";break;default:q=this._colorize?"\x1B[37m":"";break}let B=`${`[${H}${G.toISOString().replace(/T/," ").replace(/\..+/,"")}${E}]`} ${q}${x}${E} : ${I}`;switch(x){case"ERROR":console.error(B);break;case"WARN":console.warn(B);break;case"INFO":console.info(B);break;case"DEBUG":console.debug(B);break;case"LOG":console.log(B);break;default:break}}}import{appendFile as K}from"fs";class M{_path;constructor(x){this._path=x}log(x,G,A){let H=`[${G.toISOString().replace(/T/," ").replace(/\..+/,"")}]`,E=typeof A==="string"?A:JSON.stringify(A),I=`${H} ${x} : ${E}`;K(this._path,`${I}
2
+ `,(q)=>{if(q)throw q})}}export{J as d,M as e};
File without changes
@@ -0,0 +1,2 @@
1
+ import{b as z,c as A}from"./chunk-kadx22tz.js";import{Transform as F}from"stream";class G{_strategies=new Map;_logStream;_pendingLogs=[];_maxPendingLogs=1e4;_writing=!1;constructor(){this._logStream=new F({objectMode:!0,transform:(q,w,x)=>{this.executeStrategies(q.level,new Date(q.date),q.object,q.strategiesNames),x()}})}addStrategy(q,w){if(this._strategies.has(q))throw new z({messageKey:A.STRATEGY_ALREADY_ADDED,detail:{strategyName:q}});this._strategies.set(q,w)}removeStrategy(q){if(!this._strategies.has(q))throw new z({messageKey:A.STRATEGY_NOT_FOUND,detail:{strategyName:q}});this._strategies.delete(q)}clearStrategies(){this._strategies.clear()}error(q,w){this.out("ERROR",q,w)}warn(q,w){this.out("WARN",q,w)}info(q,w){this.out("INFO",q,w)}debug(q,w){this.out("DEBUG",q,w)}log(q,w){this.out("LOG",q,w)}executeStrategies(q,w,x,C){for(let D of C)this._strategies.get(D)?.log(q,w,x)}out(q,w,x=[...this._strategies.keys()]){if(this._strategies.size===0)throw new z({messageKey:A.NO_STRATEGY_ADDED});if(this._pendingLogs.length>=this._maxPendingLogs)return;let C={date:new Date().toISOString(),level:q,object:w,strategiesNames:x};if(this._pendingLogs.push(C),!this._writing)this._writing=!0,setImmediate(this.writeLog.bind(this))}writeLog(){let q=!0;while(q&&this._pendingLogs.length>0){let w=this._pendingLogs.shift();q=this._logStream.write(w)}if(!q)this._logStream.once("drain",this.writeLog.bind(this));else this._writing=!1}}
2
+ export{G as a};
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Represents the options for the Basalt error.
3
+ */
4
+ export interface BasaltErrorOptions {
5
+ /**
6
+ * The error key.
7
+ */
8
+ messageKey: string;
9
+ /**
10
+ * The status code.
11
+ */
12
+ code?: number;
13
+ /**
14
+ * The error detail.
15
+ */
16
+ detail?: unknown;
17
+ }
18
+ /**
19
+ * BasaltError is a class that represents an error entity with a unique identifier.
20
+ */
21
+ export declare class BasaltError extends Error {
22
+ /**
23
+ * The unique identifier of the error.
24
+ * This identifier is used to track the error in the logs.
25
+ */
26
+ private readonly _uuidError;
27
+ /**
28
+ * The date when the error was created.
29
+ */
30
+ private readonly _date;
31
+ /**
32
+ * The error code.
33
+ */
34
+ private readonly _code;
35
+ /**
36
+ * The error detail.
37
+ */
38
+ private readonly _detail;
39
+ /**
40
+ * Creates a new instance of the ErrorEntity class.
41
+ *
42
+ * @param basaltErrorOptions - The options to create the error entity. ({@link BasaltErrorOptions})
43
+ */
44
+ constructor(basaltErrorOptions: Readonly<BasaltErrorOptions>);
45
+ /**
46
+ * Gets the unique identifier of the error.
47
+ *
48
+ * @returns The unique identifier of the error.
49
+ */
50
+ get uuidError(): string;
51
+ /**
52
+ * Gets the date when the error was created.
53
+ *
54
+ * @returns The date when the error was created.
55
+ */
56
+ get date(): Date;
57
+ /**
58
+ * Gets the error code.
59
+ *
60
+ * @returns The error code.
61
+ */
62
+ get code(): number;
63
+ /**
64
+ * Gets the error detail.
65
+ *
66
+ * @returns The error detail.
67
+ */
68
+ get detail(): unknown;
69
+ }
@@ -0,0 +1,2 @@
1
+ export * from './basalt.error.ts';
2
+ export * from './keys.error.ts';
@@ -0,0 +1 @@
1
+ import"../../chunk-x3cvtn7y.js";import{b as a,c as b}from"../../chunk-kadx22tz.js";export{b as ErrorKeys,a as BasaltError};
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Error keys for the Basalt logger
3
+ */
4
+ export declare const ErrorKeys: {
5
+ /**
6
+ * Interpolation :
7
+ * - strategyName : The name of the strategy.
8
+ */
9
+ STRATEGY_ALREADY_ADDED: string;
10
+ /**
11
+ * Interpolation :
12
+ * - strategyName : The name of the strategy
13
+ */
14
+ STRATEGY_NOT_FOUND: string;
15
+ NO_STRATEGY_ADDED: string;
16
+ };
@@ -0,0 +1 @@
1
+ export type * from './loggerStrategy.data.ts';
File without changes
@@ -0,0 +1,11 @@
1
+ import type { LogLevels } from '../../../common/type/enum/logLevels.enum.ts';
2
+ export interface LoggerStrategy {
3
+ /**
4
+ * Logs a message with the strategy's implementation.
5
+ *
6
+ * @param level - The log level at which the message should be logged. ({@link LogLevels})
7
+ * @param date - The date at which the message was logged.
8
+ * @param object - The object to log.
9
+ */
10
+ log(level: LogLevels, date: Date, object: unknown): void;
11
+ }
@@ -0,0 +1 @@
1
+ export * from './logLevels.enum.ts';
@@ -0,0 +1 @@
1
+ import"../../../chunk-amcnfafr.js";import{f as a}from"../../../chunk-mcf4zz53.js";export{a as LogLevels};
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Enum for log levels used in BasaltLogger.
3
+ * This enum defines different levels of logging severity.
4
+ */
5
+ export declare enum LogLevels {
6
+ /**
7
+ * Log level for error messages, indicating critical issues that need attention.
8
+ */
9
+ ERROR = "ERROR",
10
+ /**
11
+ * Log level for warning messages, indicating potential issues or warnings.
12
+ */
13
+ WARN = "WARN",
14
+ /**
15
+ * Log level for informational messages, providing general information about application processes.
16
+ */
17
+ INFO = "INFO",
18
+ /**
19
+ * Log level for debug messages, useful for debugging and development purposes.
20
+ */
21
+ DEBUG = "DEBUG",
22
+ /**
23
+ * Log level for general log messages, suitable for recording standard operational events.
24
+ */
25
+ LOG = "LOG"
26
+ }
@@ -0,0 +1,109 @@
1
+ import type { LoggerStrategy } from '../../common/type/data/loggerStrategy.data.ts';
2
+ /**
3
+ * BasaltLogger provides a flexible logging system that allows multiple strategies for log output.
4
+ */
5
+ export declare class BasaltLogger {
6
+ /**
7
+ * Stores the logging strategies mapped by their names. ({@link LoggerStrategy})
8
+ */
9
+ private readonly _strategies;
10
+ /**
11
+ * Transform stream to process log entries. ({@link Transform})
12
+ */
13
+ private readonly _logStream;
14
+ /**
15
+ * Stores the pending log entries. ({@link LogStreamObject})
16
+ */
17
+ private readonly _pendingLogs;
18
+ /**
19
+ * The maximum number of pending logs.
20
+ */
21
+ private readonly _maxPendingLogs;
22
+ /**
23
+ * Indicates if the logger is writing log entries
24
+ */
25
+ private _writing;
26
+ /**
27
+ * Initializes the BasaltLogger.
28
+ */
29
+ constructor();
30
+ /**
31
+ * Adds a logging strategy.
32
+ *
33
+ * @param name - The name of the strategy.
34
+ * @param strategy - The strategy to add. ({@link LoggerStrategy})
35
+ *
36
+ * @throws ({@link BasaltError}) - If the strategy is already added. ({@link ErrorKeys.STRATEGY_ALREADY_ADDED})
37
+ */
38
+ addStrategy(name: string, strategy: LoggerStrategy): void;
39
+ /**
40
+ * Removes a logging strategy by name.
41
+ *
42
+ * @param name - The name of the strategy.
43
+ *
44
+ * @throws ({@link BasaltError}) - If the strategy is not found. ({@link ErrorKeys.STRATEGY_NOT_FOUND})
45
+ */
46
+ removeStrategy(name: string): void;
47
+ /**
48
+ * Clears all logging strategies.
49
+ */
50
+ clearStrategies(): void;
51
+ /**
52
+ * Logs an error message.
53
+ *
54
+ * @param object - The object to log.
55
+ * @param strategiesNames - The names of the strategies to use. (default: all strategies)
56
+ */
57
+ error(object: unknown, strategiesNames?: string[]): void;
58
+ /**
59
+ * Logs a warn message.
60
+ *
61
+ * @param object - The object to log.
62
+ * @param strategiesNames - The names of the strategies to use. (default: all strategies)
63
+ */
64
+ warn(object: unknown, strategiesNames?: string[]): void;
65
+ /**
66
+ * Logs an info message.
67
+ *
68
+ * @param object - The object to log.
69
+ * @param strategiesNames - The names of the strategies to use. (default: all strategies)
70
+ */
71
+ info(object: unknown, strategiesNames?: string[]): void;
72
+ /**
73
+ * Logs a debug message.
74
+ *
75
+ * @param object - The object to log.
76
+ * @param strategiesNames - The names of the strategies to use. (default: all strategies)
77
+ */
78
+ debug(object: unknown, strategiesNames?: string[]): void;
79
+ /**
80
+ * Logs a log message.
81
+ *
82
+ * @param object - The object to log.
83
+ * @param strategiesNames - The names of the strategies to use. (default: all strategies)
84
+ */
85
+ log(object: unknown, strategiesNames?: string[]): void;
86
+ /**
87
+ * Executes the logging strategies.
88
+ *
89
+ * @param level - The log level. ({@link LogLevels})
90
+ * @param date - The date of the log entry.
91
+ * @param object - The object to log.
92
+ * @param strategiesNames - The names of the strategies to use.
93
+ */
94
+ private executeStrategies;
95
+ /**
96
+ * Outputs the log entry.
97
+ *
98
+ * @param level - The log level. ({@link LogLevels})
99
+ * @param object - The object to log.
100
+ * @param strategiesNames - The names of the strategies to use. (default: all strategies)
101
+ *
102
+ * @throws ({@link BasaltError}) - If no strategy is added. ({@link ErrorKeys.NO_STRATEGY_ADDED})
103
+ */
104
+ private out;
105
+ /**
106
+ * Writes the log entries.
107
+ */
108
+ private writeLog;
109
+ }
@@ -0,0 +1 @@
1
+ import{a}from"../../chunk-xymy9zxw.js";import"../../chunk-kadx22tz.js";import"../../chunk-mcf4zz53.js";export{a as BasaltLogger};
@@ -0,0 +1,22 @@
1
+ import type { LoggerStrategy } from '../../../common/type/data/loggerStrategy.data.ts';
2
+ import { LogLevels } from '../../../common/type/enum/logLevels.enum.ts';
3
+ /**
4
+ * ConsoleLoggerStrategy implements LoggerStrategy to provide logging functionality to the console. ({@link LoggerStrategy})
5
+ */
6
+ export declare class ConsoleLoggerStrategy implements LoggerStrategy {
7
+ private readonly _colorize;
8
+ /**
9
+ * Initializes the ConsoleLoggerStrategy.
10
+ *
11
+ * @param colorize - Indicates if the output should be colorized.
12
+ */
13
+ constructor(colorize?: boolean);
14
+ /**
15
+ * Logs a message to the console with the specified log level.
16
+ *
17
+ * @param level - The log level at which the message should be logged. ({@link LogLevels})
18
+ * @param date - The date at which the message was logged.
19
+ * @param object - The object to log.
20
+ */
21
+ log(level: LogLevels, date: Date, object: unknown): void;
22
+ }
@@ -0,0 +1,25 @@
1
+ import type { LoggerStrategy } from '../../../common/type/data/loggerStrategy.data.ts';
2
+ import type { LogLevels } from '../../../common/type/enum/logLevels.enum.ts';
3
+ /**
4
+ * FileLoggerStrategy implements LoggerStrategy to provide logging functionality to the file system. ({@link LoggerStrategy})
5
+ */
6
+ export declare class FileLoggerStrategy implements LoggerStrategy {
7
+ /**
8
+ * Path to the file to log to.
9
+ */
10
+ private readonly _path;
11
+ /**
12
+ * Constructor FileLoggerStrategy
13
+ *
14
+ * @param path - Path to the file to log to.
15
+ */
16
+ constructor(path: string);
17
+ /**
18
+ * Logs a message to the file system with the specified log level.
19
+ *
20
+ * @param level - The log level at which the message should be logged. {@link LogLevels}
21
+ * @param date - The date at which the message was logged.
22
+ * @param object - The object to log.
23
+ */
24
+ log(level: LogLevels, date: Date, object: unknown): void;
25
+ }
@@ -0,0 +1,2 @@
1
+ export * from './consoleLogger.strategy.ts';
2
+ export * from './fileLogger.strategy.ts';
@@ -0,0 +1 @@
1
+ import{d as a,e as b}from"../../../chunk-wp46zt40.js";import"../../../chunk-mcf4zz53.js";export{b as FileLoggerStrategy,a as ConsoleLoggerStrategy};
@@ -0,0 +1,5 @@
1
+ export * from './common/error/index.ts';
2
+ export type * from './common/type/data/index.ts';
3
+ export * from './common/type/enum/index.ts';
4
+ export * from './domain/service/basaltLogger.service.ts';
5
+ export * from './domain/service/strategy/index.ts';
package/build/index.js ADDED
@@ -0,0 +1 @@
1
+ import"./chunk-x3cvtn7y.js";import"./chunk-amcnfafr.js";import{a as u}from"./chunk-xymy9zxw.js";import{b as l,c as p}from"./chunk-kadx22tz.js";import{d as w,e as z}from"./chunk-wp46zt40.js";import{f as q}from"./chunk-mcf4zz53.js";export{q as LogLevels,z as FileLoggerStrategy,p as ErrorKeys,w as ConsoleLoggerStrategy,u as BasaltLogger,l as BasaltError};
package/package.json ADDED
@@ -0,0 +1,77 @@
1
+ {
2
+ "name": "@basalt.lab/basalt-logger",
3
+ "version": "1.10.0",
4
+ "description": "Logger module for Basalt Framework",
5
+ "type": "module",
6
+ "types": "./build/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./build/index.d.ts",
10
+ "import": "./build/index.js",
11
+ "require": "./build/index.js"
12
+ },
13
+ "./error": {
14
+ "types": "./build/common/error/index.d.js",
15
+ "import": "./build/common/error/index.js",
16
+ "require": "./build/common/error/index.js"
17
+ },
18
+ "./type": {
19
+ "types": "./build/common/type/data/index.d.js",
20
+ "import": "./build/common/type/data/index.js",
21
+ "require": "./build/common/type/data/index.js"
22
+ },
23
+ "./enum": {
24
+ "types": "./build/common/type/enum/index.d.js",
25
+ "import": "./build/common/type/enum/index.js",
26
+ "require": "./build/common/type/enum/index.js"
27
+ },
28
+ "./logger": {
29
+ "types": "./build/domain/service/basaltLogger.service.d.js",
30
+ "import": "./build/domain/service/basaltLogger.service.js",
31
+ "require": "./build/domain/service/basaltLogger.service.js"
32
+ },
33
+ "./strategy": {
34
+ "types": "./build/domain/service/strategy/index.d.js",
35
+ "import": "./build/domain/service/strategy/index.js",
36
+ "require": "./build/domain/service/strategy/index.js"
37
+ }
38
+ },
39
+ "scripts": {
40
+ "dev": "bun --watch source/index.ts",
41
+ "build": "tsc && tsc-alias && NODE_ENV=production bun bundler.config.ts",
42
+ "start": "NODE_ENV=production bun build/index.js",
43
+ "test": "bun test --coverage",
44
+ "docs": "typedoc",
45
+ "fix-lint": "eslint --fix ./source",
46
+ "lint": "eslint ./source"
47
+ },
48
+ "repository": {
49
+ "type": "git",
50
+ "url": "git+https://github.com/Basalt-Lab/basalt-logger.git"
51
+ },
52
+ "keywords": [
53
+ "basalt-logger",
54
+ "basalt",
55
+ "logger"
56
+ ],
57
+ "author": "Ruby",
58
+ "license": "MIT",
59
+ "homepage": "https://github.com/Basalt-Lab/basalt-logger#readme",
60
+ "bugs": {
61
+ "url": "https://github.com/Basalt-Lab/basalt-logger/issues"
62
+ },
63
+ "devDependencies": {
64
+ "@types/bun": "^1.1.13",
65
+ "@types/node": "^22.9.0",
66
+ "@eslint/js": "^9.14.0",
67
+ "@stylistic/eslint-plugin": "^2.10.1",
68
+ "eslint": "^9.14.0",
69
+ "eslint-plugin-tsdoc": "^0.3.0",
70
+ "globals": "^15.12.0",
71
+ "typescript-eslint": "^8.13.0",
72
+ "tsc-alias": "^1.8.10",
73
+ "tslib": "^2.8.1",
74
+ "typedoc": "^0.26.11",
75
+ "typescript": "^5.6.3"
76
+ }
77
+ }