@lennardgeissler/logger 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +2 -2
- package/dist/components/LoggerProvider.d.ts +13 -2
- package/dist/components/LoggerProvider.js +11 -0
- package/dist/context/LoggerContext.d.ts +8 -1
- package/dist/context/LoggerContext.js +7 -0
- package/dist/core/Logger.d.ts +97 -0
- package/dist/core/Logger.js +97 -0
- package/dist/formatters/BaseFormatter.d.ts +13 -0
- package/dist/formatters/BaseFormatter.js +6 -0
- package/dist/formatters/JsonFormatter.d.ts +10 -0
- package/dist/formatters/JsonFormatter.js +10 -0
- package/dist/formatters/TextFormatter.d.ts +16 -0
- package/dist/formatters/TextFormatter.js +16 -0
- package/dist/handlers/BaseHandler.d.ts +25 -0
- package/dist/handlers/BaseHandler.js +17 -0
- package/dist/handlers/ConsoleHandler.d.ts +10 -0
- package/dist/handlers/ConsoleHandler.js +10 -0
- package/dist/handlers/FileHandler.d.ts +16 -0
- package/dist/handlers/FileHandler.js +16 -0
- package/dist/handlers/StorageHandler.d.ts +18 -0
- package/dist/handlers/StorageHandler.js +18 -0
- package/dist/hooks/useLogger.d.ts +8 -1
- package/dist/hooks/useLogger.js +6 -0
- package/dist/react/index.d.ts +4 -0
- package/dist/react/index.js +4 -0
- package/dist/types/LogLevel.d.ts +5 -0
- package/dist/types/LogMessage.d.ts +8 -0
- package/dist/utils/getLogLevelColor.d.ts +5 -0
- package/dist/utils/getLogLevelColor.js +5 -0
- package/dist/utils/getTimestamp.d.ts +4 -0
- package/dist/utils/getTimestamp.js +4 -0
- package/package.json +50 -46
package/LICENSE
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2024 Lennard Geißler
|
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
CHANGED
@@ -30,7 +30,7 @@ const handler = new ConsoleHandler();
|
|
30
30
|
handler.setFormatter(new TextFormatter('[{timestamp}] [{level}] {message}'));
|
31
31
|
logger.addHandler(handler);
|
32
32
|
// Start logging!
|
33
|
-
logger.info('Application started', { version: '1.0.
|
33
|
+
logger.info('Application started', { version: '1.0.1' });
|
34
34
|
logger.debug('Debug message');
|
35
35
|
logger.warn('Warning message', { details: 'Something went wrong' });
|
36
36
|
logger.error('Error occurred', { error: new Error('Failed to process') });
|
@@ -156,4 +156,4 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
|
|
156
156
|
|
157
157
|
## 👨💻 Authors
|
158
158
|
|
159
|
-
- Lennard Geissler
|
159
|
+
- Lennard Geissler (@LennardGeissler)
|
@@ -1,9 +1,20 @@
|
|
1
|
-
import
|
1
|
+
import { JSX, ReactNode } from 'react';
|
2
2
|
import { Logger } from '../core/Logger';
|
3
3
|
interface LoggerProviderProps {
|
4
4
|
children: ReactNode;
|
5
5
|
logger?: Logger;
|
6
6
|
handlers?: Array<any>;
|
7
7
|
}
|
8
|
-
|
8
|
+
/**
|
9
|
+
* A React component that provides a logging context to its child components.
|
10
|
+
* It initializes a logger instance and configures it with specified handlers.
|
11
|
+
*
|
12
|
+
* @param {LoggerProviderProps} props - The props for the LoggerProvider component.
|
13
|
+
* @param {ReactNode} props.children - The child components that will have access to the logger context.
|
14
|
+
* @param {Logger} [props.logger] - An optional custom logger instance. If not provided, the default singleton Logger is used.
|
15
|
+
* @param {Array<any>} [props.handlers] - An optional array of handlers to configure the logger. Defaults to a single ConsoleHandler.
|
16
|
+
*
|
17
|
+
* @returns {JSX.Element} A context provider wrapping the child components.
|
18
|
+
*/
|
19
|
+
export declare function LoggerProvider({ children, logger, handlers }: LoggerProviderProps): JSX.Element;
|
9
20
|
export {};
|
@@ -8,6 +8,17 @@ const react_1 = __importDefault(require("react"));
|
|
8
8
|
const Logger_1 = require("../core/Logger");
|
9
9
|
const LoggerContext_1 = require("../context/LoggerContext");
|
10
10
|
const ConsoleHandler_1 = require("../handlers/ConsoleHandler");
|
11
|
+
/**
|
12
|
+
* A React component that provides a logging context to its child components.
|
13
|
+
* It initializes a logger instance and configures it with specified handlers.
|
14
|
+
*
|
15
|
+
* @param {LoggerProviderProps} props - The props for the LoggerProvider component.
|
16
|
+
* @param {ReactNode} props.children - The child components that will have access to the logger context.
|
17
|
+
* @param {Logger} [props.logger] - An optional custom logger instance. If not provided, the default singleton Logger is used.
|
18
|
+
* @param {Array<any>} [props.handlers] - An optional array of handlers to configure the logger. Defaults to a single ConsoleHandler.
|
19
|
+
*
|
20
|
+
* @returns {JSX.Element} A context provider wrapping the child components.
|
21
|
+
*/
|
11
22
|
function LoggerProvider({ children, logger = Logger_1.Logger.getInstance(), handlers = [new ConsoleHandler_1.ConsoleHandler()] }) {
|
12
23
|
// Initialize logger with handlers if provided
|
13
24
|
react_1.default.useEffect(() => {
|
@@ -1,2 +1,9 @@
|
|
1
1
|
import { Logger } from '../core/Logger';
|
2
|
-
|
2
|
+
/**
|
3
|
+
* A React context for providing a Logger instance to components within the application.
|
4
|
+
* It allows child components to access and use a shared Logger instance for logging purposes.
|
5
|
+
*
|
6
|
+
* @type {React.Context<Logger | null>}
|
7
|
+
* @defaultValue null - The default value is `null`, indicating that no Logger instance is provided by default.
|
8
|
+
*/
|
9
|
+
export declare const LoggerContext: React.Context<Logger | null>;
|
@@ -2,4 +2,11 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.LoggerContext = void 0;
|
4
4
|
const react_1 = require("react");
|
5
|
+
/**
|
6
|
+
* A React context for providing a Logger instance to components within the application.
|
7
|
+
* It allows child components to access and use a shared Logger instance for logging purposes.
|
8
|
+
*
|
9
|
+
* @type {React.Context<Logger | null>}
|
10
|
+
* @defaultValue null - The default value is `null`, indicating that no Logger instance is provided by default.
|
11
|
+
*/
|
5
12
|
exports.LoggerContext = (0, react_1.createContext)(null);
|
package/dist/core/Logger.d.ts
CHANGED
@@ -1,21 +1,118 @@
|
|
1
1
|
import { LogLevel } from "../types/LogLevel";
|
2
2
|
import { BaseHandler } from "../handlers/BaseHandler";
|
3
|
+
/**
|
4
|
+
* A singleton class for managing application logging.
|
5
|
+
* It supports multiple log levels, custom handlers, and centralized logging configuration.
|
6
|
+
*/
|
3
7
|
export declare class Logger {
|
4
8
|
private static instance;
|
5
9
|
private logLevel;
|
6
10
|
private handlers;
|
11
|
+
/**
|
12
|
+
* Private constructor to prevent direct instantiation.
|
13
|
+
* Use `Logger.getInstance()` to obtain the singleton instance.
|
14
|
+
* @private
|
15
|
+
*/
|
7
16
|
private constructor();
|
17
|
+
/**
|
18
|
+
* Retrieves the singleton instance of the Logger.
|
19
|
+
*
|
20
|
+
* @returns {Logger} The singleton Logger instance.
|
21
|
+
*/
|
8
22
|
static getInstance(): Logger;
|
23
|
+
/**
|
24
|
+
* Adds a handler to the Logger.
|
25
|
+
*
|
26
|
+
* @param {BaseHandler} handler - The handler to add.
|
27
|
+
* @returns {void}
|
28
|
+
*/
|
9
29
|
addHandler(handler: BaseHandler): void;
|
30
|
+
/**
|
31
|
+
* Removes a handler from the Logger.
|
32
|
+
*
|
33
|
+
* @param {BaseHandler} handler - The handler to remove.
|
34
|
+
* @returns {void}
|
35
|
+
*/
|
10
36
|
removeHandler(handler: BaseHandler): void;
|
37
|
+
/**
|
38
|
+
* Clears all handlers from the Logger.
|
39
|
+
*
|
40
|
+
* @returns {void}
|
41
|
+
*/
|
11
42
|
clearHandlers(): void;
|
43
|
+
/**
|
44
|
+
* Sets the log level of the Logger. Messages below this level are ignored.
|
45
|
+
*
|
46
|
+
* @param {LogLevel} level - The log level to set.
|
47
|
+
* @returns {void}
|
48
|
+
*/
|
12
49
|
setLogLevel(level: LogLevel): void;
|
50
|
+
/**
|
51
|
+
* Determines whether a message at the specified log level should be logged.
|
52
|
+
*
|
53
|
+
* @private
|
54
|
+
* @param {LogLevel} level - The log level of the message.
|
55
|
+
* @returns {boolean} `true` if the message should be logged; otherwise, `false`.
|
56
|
+
*/
|
13
57
|
private shouldLog;
|
58
|
+
/**
|
59
|
+
* Creates a structured log message object.
|
60
|
+
*
|
61
|
+
* @private
|
62
|
+
* @param {LogLevel} level - The log level of the message.
|
63
|
+
* @param {string} message - The message to log.
|
64
|
+
* @param {Record<string, any> | null} [context] - Additional context for the log message.
|
65
|
+
* @returns {LogMessage} A structured log message object.
|
66
|
+
*/
|
14
67
|
private createLogMessage;
|
68
|
+
/**
|
69
|
+
* Logs a message at the specified level, dispatching it to the registered handlers.
|
70
|
+
*
|
71
|
+
* @private
|
72
|
+
* @param {LogLevel} level - The log level of the message.
|
73
|
+
* @param {string} message - The message to log.
|
74
|
+
* @param {Record<string, any> | null} [context] - Additional context for the log message.
|
75
|
+
* @returns {void}
|
76
|
+
*/
|
15
77
|
private log;
|
78
|
+
/**
|
79
|
+
* Logs a debug message.
|
80
|
+
*
|
81
|
+
* @param {string} message - The message to log.
|
82
|
+
* @param {Record<string, any> | null} [context] - Additional context for the log message.
|
83
|
+
* @returns {void}
|
84
|
+
*/
|
16
85
|
debug(message: string, context?: Record<string, any> | null): void;
|
86
|
+
/**
|
87
|
+
* Logs an informational message.
|
88
|
+
*
|
89
|
+
* @param {string} message - The message to log.
|
90
|
+
* @param {Record<string, any> | null} [context] - Additional context for the log message.
|
91
|
+
* @returns {void}
|
92
|
+
*/
|
17
93
|
info(message: string, context?: Record<string, any> | null): void;
|
94
|
+
/**
|
95
|
+
* Logs a warning message.
|
96
|
+
*
|
97
|
+
* @param {string} message - The message to log.
|
98
|
+
* @param {Record<string, any> | null} [context] - Additional context for the log message.
|
99
|
+
* @returns {void}
|
100
|
+
*/
|
18
101
|
warn(message: string, context?: Record<string, any> | null): void;
|
102
|
+
/**
|
103
|
+
* Logs an error message.
|
104
|
+
*
|
105
|
+
* @param {string} message - The message to log.
|
106
|
+
* @param {Record<string, any> | null} [context] - Additional context for the log message.
|
107
|
+
* @returns {void}
|
108
|
+
*/
|
19
109
|
error(message: string, context?: Record<string, any> | null): void;
|
110
|
+
/**
|
111
|
+
* Logs a fatal error message.
|
112
|
+
*
|
113
|
+
* @param {string} message - The message to log.
|
114
|
+
* @param {Record<string, any> | null} [context] - Additional context for the log message.
|
115
|
+
* @returns {void}
|
116
|
+
*/
|
20
117
|
fatal(message: string, context?: Record<string, any> | null): void;
|
21
118
|
}
|
package/dist/core/Logger.js
CHANGED
@@ -2,36 +2,89 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.Logger = void 0;
|
4
4
|
const getTimestamp_1 = require("../utils/getTimestamp");
|
5
|
+
/**
|
6
|
+
* A singleton class for managing application logging.
|
7
|
+
* It supports multiple log levels, custom handlers, and centralized logging configuration.
|
8
|
+
*/
|
5
9
|
class Logger {
|
10
|
+
/**
|
11
|
+
* Private constructor to prevent direct instantiation.
|
12
|
+
* Use `Logger.getInstance()` to obtain the singleton instance.
|
13
|
+
* @private
|
14
|
+
*/
|
6
15
|
constructor() {
|
7
16
|
this.logLevel = "info";
|
8
17
|
this.handlers = [];
|
9
18
|
}
|
19
|
+
/**
|
20
|
+
* Retrieves the singleton instance of the Logger.
|
21
|
+
*
|
22
|
+
* @returns {Logger} The singleton Logger instance.
|
23
|
+
*/
|
10
24
|
static getInstance() {
|
11
25
|
if (!Logger.instance) {
|
12
26
|
Logger.instance = new Logger();
|
13
27
|
}
|
14
28
|
return Logger.instance;
|
15
29
|
}
|
30
|
+
/**
|
31
|
+
* Adds a handler to the Logger.
|
32
|
+
*
|
33
|
+
* @param {BaseHandler} handler - The handler to add.
|
34
|
+
* @returns {void}
|
35
|
+
*/
|
16
36
|
addHandler(handler) {
|
17
37
|
this.handlers.push(handler);
|
18
38
|
}
|
39
|
+
/**
|
40
|
+
* Removes a handler from the Logger.
|
41
|
+
*
|
42
|
+
* @param {BaseHandler} handler - The handler to remove.
|
43
|
+
* @returns {void}
|
44
|
+
*/
|
19
45
|
removeHandler(handler) {
|
20
46
|
const index = this.handlers.indexOf(handler);
|
21
47
|
if (index > -1) {
|
22
48
|
this.handlers.splice(index, 1);
|
23
49
|
}
|
24
50
|
}
|
51
|
+
/**
|
52
|
+
* Clears all handlers from the Logger.
|
53
|
+
*
|
54
|
+
* @returns {void}
|
55
|
+
*/
|
25
56
|
clearHandlers() {
|
26
57
|
this.handlers = [];
|
27
58
|
}
|
59
|
+
/**
|
60
|
+
* Sets the log level of the Logger. Messages below this level are ignored.
|
61
|
+
*
|
62
|
+
* @param {LogLevel} level - The log level to set.
|
63
|
+
* @returns {void}
|
64
|
+
*/
|
28
65
|
setLogLevel(level) {
|
29
66
|
this.logLevel = level;
|
30
67
|
}
|
68
|
+
/**
|
69
|
+
* Determines whether a message at the specified log level should be logged.
|
70
|
+
*
|
71
|
+
* @private
|
72
|
+
* @param {LogLevel} level - The log level of the message.
|
73
|
+
* @returns {boolean} `true` if the message should be logged; otherwise, `false`.
|
74
|
+
*/
|
31
75
|
shouldLog(level) {
|
32
76
|
const levels = ["debug", "info", "warn", "error", "fatal"];
|
33
77
|
return levels.indexOf(level) >= levels.indexOf(this.logLevel);
|
34
78
|
}
|
79
|
+
/**
|
80
|
+
* Creates a structured log message object.
|
81
|
+
*
|
82
|
+
* @private
|
83
|
+
* @param {LogLevel} level - The log level of the message.
|
84
|
+
* @param {string} message - The message to log.
|
85
|
+
* @param {Record<string, any> | null} [context] - Additional context for the log message.
|
86
|
+
* @returns {LogMessage} A structured log message object.
|
87
|
+
*/
|
35
88
|
createLogMessage(level, message, context) {
|
36
89
|
return {
|
37
90
|
timestamp: (0, getTimestamp_1.getTimestamp)(),
|
@@ -40,6 +93,15 @@ class Logger {
|
|
40
93
|
context
|
41
94
|
};
|
42
95
|
}
|
96
|
+
/**
|
97
|
+
* Logs a message at the specified level, dispatching it to the registered handlers.
|
98
|
+
*
|
99
|
+
* @private
|
100
|
+
* @param {LogLevel} level - The log level of the message.
|
101
|
+
* @param {string} message - The message to log.
|
102
|
+
* @param {Record<string, any> | null} [context] - Additional context for the log message.
|
103
|
+
* @returns {void}
|
104
|
+
*/
|
43
105
|
log(level, message, context) {
|
44
106
|
if (!this.shouldLog(level))
|
45
107
|
return;
|
@@ -62,18 +124,53 @@ class Logger {
|
|
62
124
|
}
|
63
125
|
});
|
64
126
|
}
|
127
|
+
/**
|
128
|
+
* Logs a debug message.
|
129
|
+
*
|
130
|
+
* @param {string} message - The message to log.
|
131
|
+
* @param {Record<string, any> | null} [context] - Additional context for the log message.
|
132
|
+
* @returns {void}
|
133
|
+
*/
|
65
134
|
debug(message, context) {
|
66
135
|
this.log("debug", message, context);
|
67
136
|
}
|
137
|
+
/**
|
138
|
+
* Logs an informational message.
|
139
|
+
*
|
140
|
+
* @param {string} message - The message to log.
|
141
|
+
* @param {Record<string, any> | null} [context] - Additional context for the log message.
|
142
|
+
* @returns {void}
|
143
|
+
*/
|
68
144
|
info(message, context) {
|
69
145
|
this.log("info", message, context);
|
70
146
|
}
|
147
|
+
/**
|
148
|
+
* Logs a warning message.
|
149
|
+
*
|
150
|
+
* @param {string} message - The message to log.
|
151
|
+
* @param {Record<string, any> | null} [context] - Additional context for the log message.
|
152
|
+
* @returns {void}
|
153
|
+
*/
|
71
154
|
warn(message, context) {
|
72
155
|
this.log("warn", message, context);
|
73
156
|
}
|
157
|
+
/**
|
158
|
+
* Logs an error message.
|
159
|
+
*
|
160
|
+
* @param {string} message - The message to log.
|
161
|
+
* @param {Record<string, any> | null} [context] - Additional context for the log message.
|
162
|
+
* @returns {void}
|
163
|
+
*/
|
74
164
|
error(message, context) {
|
75
165
|
this.log("error", message, context);
|
76
166
|
}
|
167
|
+
/**
|
168
|
+
* Logs a fatal error message.
|
169
|
+
*
|
170
|
+
* @param {string} message - The message to log.
|
171
|
+
* @param {Record<string, any> | null} [context] - Additional context for the log message.
|
172
|
+
* @returns {void}
|
173
|
+
*/
|
77
174
|
fatal(message, context) {
|
78
175
|
this.log("fatal", message, context);
|
79
176
|
}
|
@@ -1,4 +1,17 @@
|
|
1
1
|
import { LogMessage } from "../types/LogMessage";
|
2
|
+
/**
|
3
|
+
* BaseFormatter is an abstract class that defines the structure for log message formatters.
|
4
|
+
* Subclasses should implement the `format` method to specify how log messages are formatted.
|
5
|
+
*
|
6
|
+
* @abstract
|
7
|
+
*/
|
2
8
|
export declare abstract class BaseFormatter {
|
9
|
+
/**
|
10
|
+
* Formats a log message into a string.
|
11
|
+
*
|
12
|
+
* @abstract
|
13
|
+
* @param {LogMessage} log - The log message to format.
|
14
|
+
* @returns {string} The formatted log message as a string.
|
15
|
+
*/
|
3
16
|
abstract format(log: LogMessage): string;
|
4
17
|
}
|
@@ -1,6 +1,12 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.BaseFormatter = void 0;
|
4
|
+
/**
|
5
|
+
* BaseFormatter is an abstract class that defines the structure for log message formatters.
|
6
|
+
* Subclasses should implement the `format` method to specify how log messages are formatted.
|
7
|
+
*
|
8
|
+
* @abstract
|
9
|
+
*/
|
4
10
|
class BaseFormatter {
|
5
11
|
}
|
6
12
|
exports.BaseFormatter = BaseFormatter;
|
@@ -1,5 +1,15 @@
|
|
1
1
|
import { BaseFormatter } from "./BaseFormatter";
|
2
2
|
import { LogMessage } from "../types/LogMessage";
|
3
|
+
/**
|
4
|
+
* A concrete implementation of the BaseFormatter class.
|
5
|
+
* It formats log messages as JSON strings for structured logging.
|
6
|
+
*/
|
3
7
|
export declare class JsonFormatter extends BaseFormatter {
|
8
|
+
/**
|
9
|
+
* Formats a log message into a JSON string.
|
10
|
+
*
|
11
|
+
* @param {LogMessage} log - The log message to format.
|
12
|
+
* @returns {string} The formatted log message as a JSON string.
|
13
|
+
*/
|
4
14
|
format(log: LogMessage): string;
|
5
15
|
}
|
@@ -2,7 +2,17 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.JsonFormatter = void 0;
|
4
4
|
const BaseFormatter_1 = require("./BaseFormatter");
|
5
|
+
/**
|
6
|
+
* A concrete implementation of the BaseFormatter class.
|
7
|
+
* It formats log messages as JSON strings for structured logging.
|
8
|
+
*/
|
5
9
|
class JsonFormatter extends BaseFormatter_1.BaseFormatter {
|
10
|
+
/**
|
11
|
+
* Formats a log message into a JSON string.
|
12
|
+
*
|
13
|
+
* @param {LogMessage} log - The log message to format.
|
14
|
+
* @returns {string} The formatted log message as a JSON string.
|
15
|
+
*/
|
6
16
|
format(log) {
|
7
17
|
return JSON.stringify({
|
8
18
|
timestamp: log.timestamp,
|
@@ -1,7 +1,23 @@
|
|
1
1
|
import { BaseFormatter } from "./BaseFormatter";
|
2
2
|
import { LogMessage } from "../types/LogMessage";
|
3
|
+
/**
|
4
|
+
* A concrete implementation of the BaseFormatter class.
|
5
|
+
* It formats log messages into a customizable plain text format.
|
6
|
+
*/
|
3
7
|
export declare class TextFormatter extends BaseFormatter {
|
4
8
|
private template;
|
9
|
+
/**
|
10
|
+
* Constructs a TextFormatter with a customizable template.
|
11
|
+
*
|
12
|
+
* @param {string} [template] - The template string for formatting log messages.
|
13
|
+
* @default '[{timestamp}] [{level}] {message} {context}' - The default template.
|
14
|
+
*/
|
5
15
|
constructor(template?: string);
|
16
|
+
/**
|
17
|
+
* Formats a log message into a string using the configured template.
|
18
|
+
*
|
19
|
+
* @param {LogMessage} log - The log message to format.
|
20
|
+
* @returns {string} The formatted log message as a string.
|
21
|
+
*/
|
6
22
|
format(log: LogMessage): string;
|
7
23
|
}
|
@@ -2,11 +2,27 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.TextFormatter = void 0;
|
4
4
|
const BaseFormatter_1 = require("./BaseFormatter");
|
5
|
+
/**
|
6
|
+
* A concrete implementation of the BaseFormatter class.
|
7
|
+
* It formats log messages into a customizable plain text format.
|
8
|
+
*/
|
5
9
|
class TextFormatter extends BaseFormatter_1.BaseFormatter {
|
10
|
+
/**
|
11
|
+
* Constructs a TextFormatter with a customizable template.
|
12
|
+
*
|
13
|
+
* @param {string} [template] - The template string for formatting log messages.
|
14
|
+
* @default '[{timestamp}] [{level}] {message} {context}' - The default template.
|
15
|
+
*/
|
6
16
|
constructor(template = '[{timestamp}] [{level}] {message} {context}') {
|
7
17
|
super();
|
8
18
|
this.template = template;
|
9
19
|
}
|
20
|
+
/**
|
21
|
+
* Formats a log message into a string using the configured template.
|
22
|
+
*
|
23
|
+
* @param {LogMessage} log - The log message to format.
|
24
|
+
* @returns {string} The formatted log message as a string.
|
25
|
+
*/
|
10
26
|
format(log) {
|
11
27
|
return this.template
|
12
28
|
.replace('{timestamp}', log.timestamp)
|
@@ -1,8 +1,33 @@
|
|
1
1
|
import { LogMessage } from "../types/LogMessage";
|
2
2
|
import { BaseFormatter } from "../formatters/BaseFormatter";
|
3
|
+
/**
|
4
|
+
* An abstract class that defines the structure for log handlers.
|
5
|
+
* Log handlers are responsible for processing log messages, such as sending them
|
6
|
+
* to a destination (e.g., console, file, or remote server).
|
7
|
+
*/
|
3
8
|
export declare abstract class BaseHandler {
|
4
9
|
protected formatter: BaseFormatter;
|
10
|
+
/**
|
11
|
+
* Constructs a BaseHandler with an optional formatter.
|
12
|
+
*
|
13
|
+
* @param {BaseFormatter} [formatter] - The formatter to use for this handler.
|
14
|
+
* @default new TextFormatter() - The default formatter.
|
15
|
+
*/
|
5
16
|
constructor(formatter?: BaseFormatter);
|
17
|
+
/**
|
18
|
+
* Sets the formatter for this handler.
|
19
|
+
*
|
20
|
+
* @param {BaseFormatter} formatter - The formatter to use for this handler.
|
21
|
+
* @returns {void}
|
22
|
+
*/
|
6
23
|
setFormatter(formatter: BaseFormatter): void;
|
24
|
+
/**
|
25
|
+
* Handles a log message. This method must be implemented by subclasses
|
26
|
+
* to define the specific behavior of the handler.
|
27
|
+
*
|
28
|
+
* @abstract
|
29
|
+
* @param {LogMessage} log - The log message to handle.
|
30
|
+
* @returns {void}
|
31
|
+
*/
|
7
32
|
abstract handle(log: LogMessage): void;
|
8
33
|
}
|
@@ -2,10 +2,27 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.BaseHandler = void 0;
|
4
4
|
const TextFormatter_1 = require("../formatters/TextFormatter");
|
5
|
+
/**
|
6
|
+
* An abstract class that defines the structure for log handlers.
|
7
|
+
* Log handlers are responsible for processing log messages, such as sending them
|
8
|
+
* to a destination (e.g., console, file, or remote server).
|
9
|
+
*/
|
5
10
|
class BaseHandler {
|
11
|
+
/**
|
12
|
+
* Constructs a BaseHandler with an optional formatter.
|
13
|
+
*
|
14
|
+
* @param {BaseFormatter} [formatter] - The formatter to use for this handler.
|
15
|
+
* @default new TextFormatter() - The default formatter.
|
16
|
+
*/
|
6
17
|
constructor(formatter) {
|
7
18
|
this.formatter = formatter || new TextFormatter_1.TextFormatter();
|
8
19
|
}
|
20
|
+
/**
|
21
|
+
* Sets the formatter for this handler.
|
22
|
+
*
|
23
|
+
* @param {BaseFormatter} formatter - The formatter to use for this handler.
|
24
|
+
* @returns {void}
|
25
|
+
*/
|
9
26
|
setFormatter(formatter) {
|
10
27
|
this.formatter = formatter;
|
11
28
|
}
|
@@ -1,5 +1,15 @@
|
|
1
1
|
import { BaseHandler } from "./BaseHandler";
|
2
2
|
import { LogMessage } from "../types/LogMessage";
|
3
|
+
/**
|
4
|
+
* A concrete implementation of the BaseHandler class.
|
5
|
+
* It logs log messages to the console with color-coded levels.
|
6
|
+
*/
|
3
7
|
export declare class ConsoleHandler extends BaseHandler {
|
8
|
+
/**
|
9
|
+
* Handles a log message by logging it to the console with color-coded levels.
|
10
|
+
*
|
11
|
+
* @param {LogMessage} log - The log message to handle.
|
12
|
+
* @returns {void}
|
13
|
+
*/
|
4
14
|
handle(log: LogMessage): void;
|
5
15
|
}
|
@@ -3,7 +3,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ConsoleHandler = void 0;
|
4
4
|
const BaseHandler_1 = require("./BaseHandler");
|
5
5
|
const getLogLevelColor_1 = require("../utils/getLogLevelColor");
|
6
|
+
/**
|
7
|
+
* A concrete implementation of the BaseHandler class.
|
8
|
+
* It logs log messages to the console with color-coded levels.
|
9
|
+
*/
|
6
10
|
class ConsoleHandler extends BaseHandler_1.BaseHandler {
|
11
|
+
/**
|
12
|
+
* Handles a log message by logging it to the console with color-coded levels.
|
13
|
+
*
|
14
|
+
* @param {LogMessage} log - The log message to handle.
|
15
|
+
* @returns {void}
|
16
|
+
*/
|
7
17
|
handle(log) {
|
8
18
|
const formattedLog = this.formatter.format(log);
|
9
19
|
if (typeof window !== 'undefined') {
|
@@ -1,7 +1,23 @@
|
|
1
1
|
import { BaseHandler } from "./BaseHandler";
|
2
2
|
import { LogMessage } from "../types/LogMessage";
|
3
|
+
/**
|
4
|
+
* A concrete implementation of the BaseHandler class.
|
5
|
+
* It logs log messages to a file.
|
6
|
+
*/
|
3
7
|
export declare class FileHandler extends BaseHandler {
|
4
8
|
private filePath;
|
9
|
+
/**
|
10
|
+
* Constructs a FileHandler with a specified file path.
|
11
|
+
*
|
12
|
+
* @param {string} [filePath] - The path to the log file.
|
13
|
+
* @default "logs.txt" - The default file path.
|
14
|
+
*/
|
5
15
|
constructor(filePath?: string);
|
16
|
+
/**
|
17
|
+
* Handles a log message by appending it to the file.
|
18
|
+
*
|
19
|
+
* @param {LogMessage} log - The log message to handle.
|
20
|
+
* @returns {void}
|
21
|
+
*/
|
6
22
|
handle(log: LogMessage): void;
|
7
23
|
}
|
@@ -7,11 +7,27 @@ exports.FileHandler = void 0;
|
|
7
7
|
const BaseHandler_1 = require("./BaseHandler");
|
8
8
|
const fs_1 = __importDefault(require("fs"));
|
9
9
|
const path_1 = __importDefault(require("path"));
|
10
|
+
/**
|
11
|
+
* A concrete implementation of the BaseHandler class.
|
12
|
+
* It logs log messages to a file.
|
13
|
+
*/
|
10
14
|
class FileHandler extends BaseHandler_1.BaseHandler {
|
15
|
+
/**
|
16
|
+
* Constructs a FileHandler with a specified file path.
|
17
|
+
*
|
18
|
+
* @param {string} [filePath] - The path to the log file.
|
19
|
+
* @default "logs.txt" - The default file path.
|
20
|
+
*/
|
11
21
|
constructor(filePath = "logs.txt") {
|
12
22
|
super();
|
13
23
|
this.filePath = path_1.default.resolve(filePath);
|
14
24
|
}
|
25
|
+
/**
|
26
|
+
* Handles a log message by appending it to the file.
|
27
|
+
*
|
28
|
+
* @param {LogMessage} log - The log message to handle.
|
29
|
+
* @returns {void}
|
30
|
+
*/
|
15
31
|
handle(log) {
|
16
32
|
const formattedLog = this.formatter.format(log) + '\n';
|
17
33
|
fs_1.default.appendFileSync(this.filePath, formattedLog, "utf-8");
|
@@ -1,8 +1,26 @@
|
|
1
1
|
import { BaseHandler } from "./BaseHandler";
|
2
2
|
import { LogMessage } from "../types/LogMessage";
|
3
|
+
/**
|
4
|
+
* A concrete implementation of the BaseHandler class.
|
5
|
+
* It logs log messages to a storage (localStorage or sessionStorage).
|
6
|
+
*/
|
3
7
|
export declare class StorageHandler extends BaseHandler {
|
4
8
|
private storage;
|
5
9
|
private key;
|
10
|
+
/**
|
11
|
+
* Constructs a StorageHandler with a specified storage type and key.
|
12
|
+
*
|
13
|
+
* @param {boolean} useSessionStorage - Whether to use sessionStorage or localStorage.
|
14
|
+
* @param {string} key - The key to use for storing log messages.
|
15
|
+
* @default false - Use localStorage.
|
16
|
+
* @default "logs" - The default key for storing log messages.
|
17
|
+
*/
|
6
18
|
constructor(useSessionStorage?: boolean, key?: string);
|
19
|
+
/**
|
20
|
+
* Handles a log message by appending it to the storage.
|
21
|
+
*
|
22
|
+
* @param {LogMessage} log - The log message to handle.
|
23
|
+
* @returns {void}
|
24
|
+
*/
|
7
25
|
handle(log: LogMessage): void;
|
8
26
|
}
|
@@ -2,12 +2,30 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.StorageHandler = void 0;
|
4
4
|
const BaseHandler_1 = require("./BaseHandler");
|
5
|
+
/**
|
6
|
+
* A concrete implementation of the BaseHandler class.
|
7
|
+
* It logs log messages to a storage (localStorage or sessionStorage).
|
8
|
+
*/
|
5
9
|
class StorageHandler extends BaseHandler_1.BaseHandler {
|
10
|
+
/**
|
11
|
+
* Constructs a StorageHandler with a specified storage type and key.
|
12
|
+
*
|
13
|
+
* @param {boolean} useSessionStorage - Whether to use sessionStorage or localStorage.
|
14
|
+
* @param {string} key - The key to use for storing log messages.
|
15
|
+
* @default false - Use localStorage.
|
16
|
+
* @default "logs" - The default key for storing log messages.
|
17
|
+
*/
|
6
18
|
constructor(useSessionStorage = false, key = "logs") {
|
7
19
|
super();
|
8
20
|
this.storage = useSessionStorage ? sessionStorage : localStorage;
|
9
21
|
this.key = key;
|
10
22
|
}
|
23
|
+
/**
|
24
|
+
* Handles a log message by appending it to the storage.
|
25
|
+
*
|
26
|
+
* @param {LogMessage} log - The log message to handle.
|
27
|
+
* @returns {void}
|
28
|
+
*/
|
11
29
|
handle(log) {
|
12
30
|
const existingLogs = this.storage.getItem(this.key);
|
13
31
|
const logs = existingLogs ? JSON.parse(existingLogs) : [];
|
@@ -1 +1,8 @@
|
|
1
|
-
|
1
|
+
import { Logger } from '../core/Logger';
|
2
|
+
/**
|
3
|
+
* Custom hook to access the Logger instance from the LoggerContext.
|
4
|
+
*
|
5
|
+
* @returns {Logger} The Logger instance.
|
6
|
+
* @throws {Error} If used outside of a LoggerProvider.
|
7
|
+
*/
|
8
|
+
export declare function useLogger(): Logger;
|
package/dist/hooks/useLogger.js
CHANGED
@@ -3,6 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useLogger = useLogger;
|
4
4
|
const react_1 = require("react");
|
5
5
|
const LoggerContext_1 = require("../context/LoggerContext");
|
6
|
+
/**
|
7
|
+
* Custom hook to access the Logger instance from the LoggerContext.
|
8
|
+
*
|
9
|
+
* @returns {Logger} The Logger instance.
|
10
|
+
* @throws {Error} If used outside of a LoggerProvider.
|
11
|
+
*/
|
6
12
|
function useLogger() {
|
7
13
|
const logger = (0, react_1.useContext)(LoggerContext_1.LoggerContext);
|
8
14
|
if (!logger) {
|
package/dist/react/index.d.ts
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
/**
|
2
|
+
* Exports the LoggerProvider, useLogger, and LoggerContext from the react module.
|
3
|
+
* @module react
|
4
|
+
*/
|
1
5
|
export { LoggerProvider } from '../components/LoggerProvider';
|
2
6
|
export { useLogger } from '../hooks/useLogger';
|
3
7
|
export { LoggerContext } from '../context/LoggerContext';
|
package/dist/react/index.js
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.LoggerContext = exports.useLogger = exports.LoggerProvider = void 0;
|
4
|
+
/**
|
5
|
+
* Exports the LoggerProvider, useLogger, and LoggerContext from the react module.
|
6
|
+
* @module react
|
7
|
+
*/
|
4
8
|
var LoggerProvider_1 = require("../components/LoggerProvider");
|
5
9
|
Object.defineProperty(exports, "LoggerProvider", { enumerable: true, get: function () { return LoggerProvider_1.LoggerProvider; } });
|
6
10
|
var useLogger_1 = require("../hooks/useLogger");
|
package/dist/types/LogLevel.d.ts
CHANGED
@@ -1,4 +1,12 @@
|
|
1
1
|
import { LogLevel } from "./LogLevel";
|
2
|
+
/**
|
3
|
+
* An interface representing a log message.
|
4
|
+
* @interface
|
5
|
+
* @property {string} timestamp - The timestamp of the log message.
|
6
|
+
* @property {LogLevel} level - The log level of the message.
|
7
|
+
* @property {string} message - The log message.
|
8
|
+
* @property {Record<string, any> | null} context - Optional context data for the log message.
|
9
|
+
*/
|
2
10
|
export interface LogMessage {
|
3
11
|
timestamp: string;
|
4
12
|
level: LogLevel;
|
@@ -1,6 +1,11 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.getLogLevelColor = getLogLevelColor;
|
4
|
+
/**
|
5
|
+
* Get the color for a log level.
|
6
|
+
* @param {LogLevel} level - The log level.
|
7
|
+
* @returns {string} The color for the log level.
|
8
|
+
*/
|
4
9
|
function getLogLevelColor(level) {
|
5
10
|
switch (level) {
|
6
11
|
case "debug":
|
@@ -1,6 +1,10 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.getTimestamp = getTimestamp;
|
4
|
+
/**
|
5
|
+
* Get the current timestamp in ISO format.
|
6
|
+
* @returns {string} The current timestamp in ISO format.
|
7
|
+
*/
|
4
8
|
function getTimestamp() {
|
5
9
|
return new Date().toISOString();
|
6
10
|
}
|
package/package.json
CHANGED
@@ -1,46 +1,50 @@
|
|
1
|
-
{
|
2
|
-
"name": "@lennardgeissler/logger",
|
3
|
-
"version": "1.0.
|
4
|
-
"main": "dist/index.js",
|
5
|
-
"types": "dist/index.d.ts",
|
6
|
-
"files": [
|
7
|
-
"dist",
|
8
|
-
"README.md",
|
9
|
-
"CHANGELOG.md",
|
10
|
-
"LICENSE"
|
11
|
-
],
|
12
|
-
"scripts": {
|
13
|
-
"clean": "if exist dist rd /s /q dist",
|
14
|
-
"build": "npm run clean && tsc",
|
15
|
-
"test": "jest",
|
16
|
-
"test:watch": "jest --watch",
|
17
|
-
"test:coverage": "jest --coverage",
|
18
|
-
"prepublishOnly": "npm test && npm run build",
|
19
|
-
"preversion": "npm test",
|
20
|
-
"postversion": "git push && git push --tags"
|
21
|
-
},
|
22
|
-
"keywords": [
|
23
|
-
"logging",
|
24
|
-
"typescript",
|
25
|
-
"react",
|
26
|
-
"node"
|
27
|
-
],
|
28
|
-
"author": "Lennard Geißler",
|
29
|
-
"license": "MIT",
|
30
|
-
"type": "commonjs",
|
31
|
-
"description": "",
|
32
|
-
"devDependencies": {
|
33
|
-
"@types/jest": "^29.5.14",
|
34
|
-
"@types/node": "^22.10.7",
|
35
|
-
"@types/react": "^19.0.7",
|
36
|
-
"@types/react-dom": "^19.0.3",
|
37
|
-
"@vitejs/plugin-react": "^4.3.4",
|
38
|
-
"jest": "^29.7.0",
|
39
|
-
"ts-jest": "^29.2.5",
|
40
|
-
"typescript": "^5.7.3"
|
41
|
-
},
|
42
|
-
"peerDependencies": {
|
43
|
-
"react": "^19.0.0",
|
44
|
-
"react-dom": "^19.0.0"
|
45
|
-
}
|
46
|
-
|
1
|
+
{
|
2
|
+
"name": "@lennardgeissler/logger",
|
3
|
+
"version": "1.0.1",
|
4
|
+
"main": "dist/index.js",
|
5
|
+
"types": "dist/index.d.ts",
|
6
|
+
"files": [
|
7
|
+
"dist",
|
8
|
+
"README.md",
|
9
|
+
"CHANGELOG.md",
|
10
|
+
"LICENSE"
|
11
|
+
],
|
12
|
+
"scripts": {
|
13
|
+
"clean": "if exist dist rd /s /q dist",
|
14
|
+
"build": "npm run clean && tsc",
|
15
|
+
"test": "jest",
|
16
|
+
"test:watch": "jest --watch",
|
17
|
+
"test:coverage": "jest --coverage",
|
18
|
+
"prepublishOnly": "npm test && npm run build",
|
19
|
+
"preversion": "npm test",
|
20
|
+
"postversion": "git push && git push --tags"
|
21
|
+
},
|
22
|
+
"keywords": [
|
23
|
+
"logging",
|
24
|
+
"typescript",
|
25
|
+
"react",
|
26
|
+
"node"
|
27
|
+
],
|
28
|
+
"author": "Lennard Geißler",
|
29
|
+
"license": "MIT",
|
30
|
+
"type": "commonjs",
|
31
|
+
"description": "",
|
32
|
+
"devDependencies": {
|
33
|
+
"@types/jest": "^29.5.14",
|
34
|
+
"@types/node": "^22.10.7",
|
35
|
+
"@types/react": "^19.0.7",
|
36
|
+
"@types/react-dom": "^19.0.3",
|
37
|
+
"@vitejs/plugin-react": "^4.3.4",
|
38
|
+
"jest": "^29.7.0",
|
39
|
+
"ts-jest": "^29.2.5",
|
40
|
+
"typescript": "^5.7.3"
|
41
|
+
},
|
42
|
+
"peerDependencies": {
|
43
|
+
"react": "^19.0.0",
|
44
|
+
"react-dom": "^19.0.0"
|
45
|
+
},
|
46
|
+
"repository": {
|
47
|
+
"type": "git",
|
48
|
+
"url": "https://github.com/lennardgeissler/logger.git"
|
49
|
+
}
|
50
|
+
}
|