log-tonic 1.0.1 → 1.1.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/README.md +78 -26
- package/lib/index.d.ts +3 -1
- package/lib/index.js +16 -0
- package/lib/logger.d.ts +12 -53
- package/lib/logger.js +46 -65
- package/lib/loggerFactory.d.ts +7 -17
- package/lib/loggerFactory.js +14 -9
- package/lib/transports.d.ts +12 -0
- package/lib/transports.js +41 -0
- package/lib/types.d.ts +29 -0
- package/lib/types.js +19 -0
- package/package.json +5 -2
- package/src/index.ts +0 -18
- package/src/logger.ts +0 -132
- package/src/loggerFactory.ts +0 -77
- package/tsconfig.json +0 -15
package/README.md
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
|
|
2
2
|
# Log-Tonic
|
|
3
3
|
|
|
4
|
-
**Log-Tonic** is
|
|
4
|
+
**Log-Tonic** is an enterprise-grade, flexible logging utility for Node.js and TypeScript projects. It supports structured JSON logging, multiple transports (Console, File, etc.), log levels, and customizable formatting.
|
|
5
5
|
|
|
6
6
|
## Features
|
|
7
7
|
|
|
8
|
-
- **
|
|
9
|
-
- **
|
|
10
|
-
- **
|
|
11
|
-
- **
|
|
12
|
-
- **TypeScript Support**:
|
|
8
|
+
- **Enterprise Ready**: Structured JSON logging out of the box for easy ingestion by tools like Splunk, ELK, etc.
|
|
9
|
+
- **Transport System**: Modular transport system. Writes to Console by default, but extensible to File, HTTP, etc.
|
|
10
|
+
- **Rich Log Levels**: Support for `debug`, `info`, `warn`, `error`, and `fatal`.
|
|
11
|
+
- **Feature-Specific Logging**: Create scoped loggers for different modules or features.
|
|
12
|
+
- **TypeScript Support**: Built with TypeScript for full type safety.
|
|
13
13
|
|
|
14
14
|
## Installation
|
|
15
15
|
|
|
@@ -26,13 +26,17 @@ npm install log-tonic
|
|
|
26
26
|
Initialize the logger at the start of your application with the desired configuration:
|
|
27
27
|
|
|
28
28
|
```typescript
|
|
29
|
-
import { LoggerFactory } from 'log-tonic';
|
|
29
|
+
import { LoggerFactory, ConsoleTransport, LogLevel } from 'log-tonic';
|
|
30
30
|
|
|
31
31
|
LoggerFactory.initialize({
|
|
32
|
-
|
|
33
|
-
appName: '
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
minLevel: LogLevel.DEBUG,
|
|
33
|
+
appName: 'MyEnterpriseApp',
|
|
34
|
+
transports: [
|
|
35
|
+
new ConsoleTransport({
|
|
36
|
+
useJson: true, // Enable structured JSON logging
|
|
37
|
+
useColor: true // Enable colors for non-JSON output
|
|
38
|
+
})
|
|
39
|
+
]
|
|
36
40
|
});
|
|
37
41
|
```
|
|
38
42
|
|
|
@@ -43,32 +47,80 @@ Create a logger for a specific feature or module within your application:
|
|
|
43
47
|
```typescript
|
|
44
48
|
const authLogger = LoggerFactory.createLogger('AuthService');
|
|
45
49
|
|
|
46
|
-
|
|
47
|
-
authLogger.
|
|
48
|
-
authLogger.
|
|
50
|
+
// Pass metadata object as the second argument
|
|
51
|
+
authLogger.info('User login successful', { userId: '12345', role: 'admin' });
|
|
52
|
+
authLogger.error('Login failed', { error: 'Invalid credentials' });
|
|
49
53
|
```
|
|
50
54
|
|
|
51
|
-
### 3.
|
|
55
|
+
### 3. Log Levels
|
|
56
|
+
|
|
57
|
+
Available log levels in order of priority:
|
|
58
|
+
1. `DEBUG`
|
|
59
|
+
2. `INFO`
|
|
60
|
+
3. `WARN`
|
|
61
|
+
4. `ERROR`
|
|
62
|
+
5. `FATAL`
|
|
52
63
|
|
|
53
|
-
|
|
54
|
-
- **appName**: The name of your application (default is `MyApp`).
|
|
55
|
-
- **timeFormat**: The format for timestamps, using date-fns format tokens (e.g., `yyyy-MM-dd HH:mm:ss`).
|
|
56
|
-
- **messageFormat**: Optionally set `prefix` and `suffix` to wrap your log messages (e.g., `{ prefix: '--> INFO: ', suffix: ' <-- END' }`).
|
|
64
|
+
### 4. Custom Transports
|
|
57
65
|
|
|
58
|
-
|
|
66
|
+
You can create custom transports by implementing the `Transport` interface:
|
|
59
67
|
|
|
60
68
|
```typescript
|
|
61
|
-
|
|
69
|
+
import { Transport, LogEntry } from 'log-tonic';
|
|
70
|
+
|
|
71
|
+
class MyCustomTransport implements Transport {
|
|
72
|
+
log(entry: LogEntry): void {
|
|
73
|
+
// Send log to external service
|
|
74
|
+
console.log('Sending to cloud:', entry);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Migration Guide (v1.0.x -> v1.1.0)
|
|
80
|
+
|
|
81
|
+
If you are upgrading from `v1.0.1` or earlier, there are some breaking changes to support the new enterprise features.
|
|
82
|
+
|
|
83
|
+
### 1. Initialization Changes
|
|
62
84
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
85
|
+
The `level` property has been renamed to `minLevel` and now accepts a `LogLevel` enum instead of a string.
|
|
86
|
+
|
|
87
|
+
**v1.0.0 (Old):**
|
|
88
|
+
```typescript
|
|
89
|
+
LoggerFactory.initialize({
|
|
90
|
+
level: 'debug',
|
|
91
|
+
appName: 'MyApp'
|
|
92
|
+
});
|
|
66
93
|
```
|
|
67
94
|
|
|
68
|
-
|
|
95
|
+
**v1.1.0 (New):**
|
|
96
|
+
```typescript
|
|
97
|
+
import { LoggerFactory, LogLevel } from 'log-tonic';
|
|
69
98
|
|
|
99
|
+
LoggerFactory.initialize({
|
|
100
|
+
minLevel: LogLevel.DEBUG, // Changed from string to Enum
|
|
101
|
+
appName: 'MyApp'
|
|
102
|
+
// Transports are optional, defaults to ConsoleTransport
|
|
103
|
+
});
|
|
70
104
|
```
|
|
71
|
-
|
|
105
|
+
|
|
106
|
+
### 2. Log Levels
|
|
107
|
+
|
|
108
|
+
We have moved from strict string types to an Enum for better type safety and added new levels.
|
|
109
|
+
|
|
110
|
+
- Old: `'debug'`, `'info'`, `'error'`
|
|
111
|
+
- New: `LogLevel.DEBUG`, `LogLevel.INFO`, `LogLevel.WARN`, `LogLevel.ERROR`, `LogLevel.FATAL`
|
|
112
|
+
|
|
113
|
+
### 3. Creating Loggers
|
|
114
|
+
|
|
115
|
+
`createLogger` remains largely valid, but methods now support metadata objects.
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
// v1.0.0
|
|
119
|
+
logger.info('Message');
|
|
120
|
+
|
|
121
|
+
// v1.1.0
|
|
122
|
+
logger.info('Message'); // Still works
|
|
123
|
+
logger.info('Message', { meta: 'data' }); // New capability
|
|
72
124
|
```
|
|
73
125
|
|
|
74
126
|
## Contributing
|
package/lib/index.d.ts
CHANGED
|
@@ -13,5 +13,7 @@
|
|
|
13
13
|
*
|
|
14
14
|
* Copyright 2024 Sitharaj Seenivasan
|
|
15
15
|
*/
|
|
16
|
-
export
|
|
16
|
+
export * from './types';
|
|
17
|
+
export * from './transports';
|
|
18
|
+
export { LoggerFactory } from './loggerFactory';
|
|
17
19
|
export { default as Logger } from './logger';
|
package/lib/index.js
CHANGED
|
@@ -14,11 +14,27 @@
|
|
|
14
14
|
*
|
|
15
15
|
* Copyright 2024 Sitharaj Seenivasan
|
|
16
16
|
*/
|
|
17
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
18
|
+
if (k2 === undefined) k2 = k;
|
|
19
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
20
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
21
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
22
|
+
}
|
|
23
|
+
Object.defineProperty(o, k2, desc);
|
|
24
|
+
}) : (function(o, m, k, k2) {
|
|
25
|
+
if (k2 === undefined) k2 = k;
|
|
26
|
+
o[k2] = m[k];
|
|
27
|
+
}));
|
|
28
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
29
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
30
|
+
};
|
|
17
31
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
18
32
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
19
33
|
};
|
|
20
34
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
35
|
exports.Logger = exports.LoggerFactory = void 0;
|
|
36
|
+
__exportStar(require("./types"), exports);
|
|
37
|
+
__exportStar(require("./transports"), exports);
|
|
22
38
|
var loggerFactory_1 = require("./loggerFactory");
|
|
23
39
|
Object.defineProperty(exports, "LoggerFactory", { enumerable: true, get: function () { return loggerFactory_1.LoggerFactory; } });
|
|
24
40
|
var logger_1 = require("./logger");
|
package/lib/logger.d.ts
CHANGED
|
@@ -13,70 +13,29 @@
|
|
|
13
13
|
*
|
|
14
14
|
* Copyright 2024 Sitharaj Seenivasan
|
|
15
15
|
*/
|
|
16
|
+
import { LoggerConfig } from './types';
|
|
16
17
|
/**
|
|
17
|
-
* Logger class
|
|
18
|
+
* Enterprise-grade Logger class.
|
|
19
|
+
* Supports multiple transports, structured logging, and configurable formatting.
|
|
18
20
|
*
|
|
19
21
|
* @class Logger
|
|
20
22
|
*/
|
|
21
23
|
declare class Logger {
|
|
22
|
-
private
|
|
23
|
-
private
|
|
24
|
-
private timeFormat;
|
|
25
|
-
private messageFormat;
|
|
26
|
-
private levels;
|
|
24
|
+
private config;
|
|
25
|
+
private transports;
|
|
27
26
|
/**
|
|
28
27
|
* Constructs a Logger instance.
|
|
29
28
|
*
|
|
30
|
-
* @param {
|
|
31
|
-
* @param {string} [appName] - The name of the application.
|
|
32
|
-
* @param {string} [timeFormat] - The format for the timestamp.
|
|
33
|
-
* @param {Object} [messageFormat] - The prefix and suffix for log messages.
|
|
34
|
-
*/
|
|
35
|
-
constructor(level: string, appName?: string, timeFormat?: string, messageFormat?: {
|
|
36
|
-
prefix?: string;
|
|
37
|
-
suffix?: string;
|
|
38
|
-
});
|
|
39
|
-
/**
|
|
40
|
-
* Formats the current timestamp according to the specified time format using date-fns.
|
|
41
|
-
*
|
|
42
|
-
* @returns {string} The formatted timestamp.
|
|
29
|
+
* @param {LoggerConfig} config - Configuration object.
|
|
43
30
|
*/
|
|
31
|
+
constructor(config: LoggerConfig);
|
|
44
32
|
private formatTime;
|
|
45
|
-
/**
|
|
46
|
-
* Formats the log message by adding the specified prefix and suffix.
|
|
47
|
-
*
|
|
48
|
-
* @param {string} message - The log message.
|
|
49
|
-
* @returns {string} The formatted log message.
|
|
50
|
-
*/
|
|
51
33
|
private formatMessage;
|
|
52
|
-
/**
|
|
53
|
-
* Logs a message with the specified level and feature.
|
|
54
|
-
*
|
|
55
|
-
* @param {string} level - The logging level (debug, info, error).
|
|
56
|
-
* @param {string} feature - The feature name associated with the log message.
|
|
57
|
-
* @param {string} message - The log message.
|
|
58
|
-
*/
|
|
59
34
|
private log;
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
*/
|
|
66
|
-
info(feature: string, message: string): void;
|
|
67
|
-
/**
|
|
68
|
-
* Logs an error message.
|
|
69
|
-
*
|
|
70
|
-
* @param {string} feature - The feature name associated with the log message.
|
|
71
|
-
* @param {string} message - The log message.
|
|
72
|
-
*/
|
|
73
|
-
error(feature: string, message: string): void;
|
|
74
|
-
/**
|
|
75
|
-
* Logs a debug message.
|
|
76
|
-
*
|
|
77
|
-
* @param {string} feature - The feature name associated with the log message.
|
|
78
|
-
* @param {string} message - The log message.
|
|
79
|
-
*/
|
|
80
|
-
debug(feature: string, message: string): void;
|
|
35
|
+
debug(feature: string, message: string, meta?: Record<string, any>): void;
|
|
36
|
+
info(feature: string, message: string, meta?: Record<string, any>): void;
|
|
37
|
+
warn(feature: string, message: string, meta?: Record<string, any>): void;
|
|
38
|
+
error(feature: string, message: string, meta?: Record<string, any>): void;
|
|
39
|
+
fatal(feature: string, message: string, meta?: Record<string, any>): void;
|
|
81
40
|
}
|
|
82
41
|
export default Logger;
|
package/lib/logger.js
CHANGED
|
@@ -16,8 +16,11 @@
|
|
|
16
16
|
*/
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
var date_fns_1 = require("date-fns");
|
|
19
|
+
var types_1 = require("./types");
|
|
20
|
+
var transports_1 = require("./transports");
|
|
19
21
|
/**
|
|
20
|
-
* Logger class
|
|
22
|
+
* Enterprise-grade Logger class.
|
|
23
|
+
* Supports multiple transports, structured logging, and configurable formatting.
|
|
21
24
|
*
|
|
22
25
|
* @class Logger
|
|
23
26
|
*/
|
|
@@ -25,85 +28,63 @@ var Logger = /** @class */ (function () {
|
|
|
25
28
|
/**
|
|
26
29
|
* Constructs a Logger instance.
|
|
27
30
|
*
|
|
28
|
-
* @param {
|
|
29
|
-
* @param {string} [appName] - The name of the application.
|
|
30
|
-
* @param {string} [timeFormat] - The format for the timestamp.
|
|
31
|
-
* @param {Object} [messageFormat] - The prefix and suffix for log messages.
|
|
31
|
+
* @param {LoggerConfig} config - Configuration object.
|
|
32
32
|
*/
|
|
33
|
-
function Logger(
|
|
34
|
-
this.
|
|
35
|
-
this.
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
this.levels = { debug: 0, info: 1, error: 2 };
|
|
33
|
+
function Logger(config) {
|
|
34
|
+
this.config = config;
|
|
35
|
+
this.transports = config.transports && config.transports.length > 0
|
|
36
|
+
? config.transports
|
|
37
|
+
: [new transports_1.ConsoleTransport()];
|
|
39
38
|
}
|
|
40
|
-
/**
|
|
41
|
-
* Formats the current timestamp according to the specified time format using date-fns.
|
|
42
|
-
*
|
|
43
|
-
* @returns {string} The formatted timestamp.
|
|
44
|
-
*/
|
|
45
39
|
Logger.prototype.formatTime = function () {
|
|
46
40
|
var now = new Date();
|
|
47
41
|
try {
|
|
48
|
-
return (0, date_fns_1.format)(now, this.timeFormat);
|
|
42
|
+
return (0, date_fns_1.format)(now, this.config.timeFormat || "yyyy-MM-dd HH:mm:ss");
|
|
49
43
|
}
|
|
50
44
|
catch (error) {
|
|
51
|
-
|
|
52
|
-
return now.toISOString().replace("T", " ").split(".")[0]; // Fallback to ISO format
|
|
45
|
+
return now.toISOString();
|
|
53
46
|
}
|
|
54
47
|
};
|
|
55
|
-
/**
|
|
56
|
-
* Formats the log message by adding the specified prefix and suffix.
|
|
57
|
-
*
|
|
58
|
-
* @param {string} message - The log message.
|
|
59
|
-
* @returns {string} The formatted log message.
|
|
60
|
-
*/
|
|
61
48
|
Logger.prototype.formatMessage = function (message) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
return message;
|
|
49
|
+
var _a = this.config.messageFormat || {}, prefix = _a.prefix, suffix = _a.suffix;
|
|
50
|
+
return "".concat(prefix || "").concat(message).concat(suffix || "");
|
|
66
51
|
};
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
52
|
+
Logger.prototype.log = function (level, feature, message, meta) {
|
|
53
|
+
if (types_1.LogLevelPriority[level] < types_1.LogLevelPriority[this.config.minLevel]) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
var entry = {
|
|
57
|
+
timestamp: this.formatTime(),
|
|
58
|
+
level: level,
|
|
59
|
+
appName: this.config.appName || "MyApp",
|
|
60
|
+
feature: feature,
|
|
61
|
+
message: this.formatMessage(message),
|
|
62
|
+
meta: meta,
|
|
63
|
+
};
|
|
64
|
+
for (var _i = 0, _a = this.transports; _i < _a.length; _i++) {
|
|
65
|
+
var transport = _a[_i];
|
|
66
|
+
try {
|
|
67
|
+
transport.log(entry);
|
|
68
|
+
}
|
|
69
|
+
catch (e) {
|
|
70
|
+
console.error("Failed to log to transport:", e);
|
|
71
|
+
}
|
|
79
72
|
}
|
|
80
73
|
};
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
*
|
|
84
|
-
* @param {string} feature - The feature name associated with the log message.
|
|
85
|
-
* @param {string} message - The log message.
|
|
86
|
-
*/
|
|
87
|
-
Logger.prototype.info = function (feature, message) {
|
|
88
|
-
this.log("info", feature, message);
|
|
74
|
+
Logger.prototype.debug = function (feature, message, meta) {
|
|
75
|
+
this.log(types_1.LogLevel.DEBUG, feature, message, meta);
|
|
89
76
|
};
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
*
|
|
93
|
-
* @param {string} feature - The feature name associated with the log message.
|
|
94
|
-
* @param {string} message - The log message.
|
|
95
|
-
*/
|
|
96
|
-
Logger.prototype.error = function (feature, message) {
|
|
97
|
-
this.log("error", feature, message);
|
|
77
|
+
Logger.prototype.info = function (feature, message, meta) {
|
|
78
|
+
this.log(types_1.LogLevel.INFO, feature, message, meta);
|
|
98
79
|
};
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
Logger.prototype.
|
|
106
|
-
this.log(
|
|
80
|
+
Logger.prototype.warn = function (feature, message, meta) {
|
|
81
|
+
this.log(types_1.LogLevel.WARN, feature, message, meta);
|
|
82
|
+
};
|
|
83
|
+
Logger.prototype.error = function (feature, message, meta) {
|
|
84
|
+
this.log(types_1.LogLevel.ERROR, feature, message, meta);
|
|
85
|
+
};
|
|
86
|
+
Logger.prototype.fatal = function (feature, message, meta) {
|
|
87
|
+
this.log(types_1.LogLevel.FATAL, feature, message, meta);
|
|
107
88
|
};
|
|
108
89
|
return Logger;
|
|
109
90
|
}());
|
package/lib/loggerFactory.d.ts
CHANGED
|
@@ -12,21 +12,9 @@
|
|
|
12
12
|
* limitations under the License.
|
|
13
13
|
*
|
|
14
14
|
* Copyright 2024 Sitharaj Seenivasan
|
|
15
|
-
|
|
16
15
|
*/
|
|
17
16
|
import Logger from "./logger";
|
|
18
|
-
|
|
19
|
-
* Interface representing the configuration options for the Logger.
|
|
20
|
-
*/
|
|
21
|
-
export interface LoggerConfig {
|
|
22
|
-
level: "debug" | "info" | "error";
|
|
23
|
-
appName?: string;
|
|
24
|
-
timeFormat?: string;
|
|
25
|
-
messageFormat?: {
|
|
26
|
-
prefix?: string;
|
|
27
|
-
suffix?: string;
|
|
28
|
-
};
|
|
29
|
-
}
|
|
17
|
+
import { LoggerConfig } from "./types";
|
|
30
18
|
/**
|
|
31
19
|
* LoggerFactory class responsible for creating and managing Logger instances.
|
|
32
20
|
*
|
|
@@ -45,11 +33,13 @@ export declare class LoggerFactory {
|
|
|
45
33
|
* Creates a logger for the specified feature.
|
|
46
34
|
*
|
|
47
35
|
* @param {string} feature - The feature name for which to create the logger.
|
|
48
|
-
* @returns {Object} An object containing methods for logging
|
|
36
|
+
* @returns {Object} An object containing methods for logging.
|
|
49
37
|
*/
|
|
50
38
|
static createLogger(feature: string): {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
39
|
+
debug: (message: string, meta?: Record<string, any>) => void;
|
|
40
|
+
info: (message: string, meta?: Record<string, any>) => void;
|
|
41
|
+
warn: (message: string, meta?: Record<string, any>) => void;
|
|
42
|
+
error: (message: string, meta?: Record<string, any>) => void;
|
|
43
|
+
fatal: (message: string, meta?: Record<string, any>) => void;
|
|
54
44
|
};
|
|
55
45
|
}
|
package/lib/loggerFactory.js
CHANGED
|
@@ -13,7 +13,6 @@
|
|
|
13
13
|
* limitations under the License.
|
|
14
14
|
*
|
|
15
15
|
* Copyright 2024 Sitharaj Seenivasan
|
|
16
|
-
|
|
17
16
|
*/
|
|
18
17
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
19
18
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
@@ -37,7 +36,7 @@ var LoggerFactory = /** @class */ (function () {
|
|
|
37
36
|
*/
|
|
38
37
|
LoggerFactory.initialize = function (config) {
|
|
39
38
|
if (!LoggerFactory.loggerInstance) {
|
|
40
|
-
LoggerFactory.loggerInstance = new logger_1.default(config
|
|
39
|
+
LoggerFactory.loggerInstance = new logger_1.default(config);
|
|
41
40
|
}
|
|
42
41
|
return LoggerFactory.loggerInstance;
|
|
43
42
|
};
|
|
@@ -45,21 +44,27 @@ var LoggerFactory = /** @class */ (function () {
|
|
|
45
44
|
* Creates a logger for the specified feature.
|
|
46
45
|
*
|
|
47
46
|
* @param {string} feature - The feature name for which to create the logger.
|
|
48
|
-
* @returns {Object} An object containing methods for logging
|
|
47
|
+
* @returns {Object} An object containing methods for logging.
|
|
49
48
|
*/
|
|
50
49
|
LoggerFactory.createLogger = function (feature) {
|
|
51
50
|
if (!LoggerFactory.loggerInstance) {
|
|
52
51
|
throw new Error("Logger is not initialized. Call LoggerFactory.initialize(config) first.");
|
|
53
52
|
}
|
|
54
53
|
return {
|
|
55
|
-
|
|
56
|
-
return LoggerFactory.loggerInstance.
|
|
54
|
+
debug: function (message, meta) {
|
|
55
|
+
return LoggerFactory.loggerInstance.debug(feature, message, meta);
|
|
57
56
|
},
|
|
58
|
-
|
|
59
|
-
return LoggerFactory.loggerInstance.
|
|
57
|
+
info: function (message, meta) {
|
|
58
|
+
return LoggerFactory.loggerInstance.info(feature, message, meta);
|
|
60
59
|
},
|
|
61
|
-
|
|
62
|
-
return LoggerFactory.loggerInstance.
|
|
60
|
+
warn: function (message, meta) {
|
|
61
|
+
return LoggerFactory.loggerInstance.warn(feature, message, meta);
|
|
62
|
+
},
|
|
63
|
+
error: function (message, meta) {
|
|
64
|
+
return LoggerFactory.loggerInstance.error(feature, message, meta);
|
|
65
|
+
},
|
|
66
|
+
fatal: function (message, meta) {
|
|
67
|
+
return LoggerFactory.loggerInstance.fatal(feature, message, meta);
|
|
63
68
|
},
|
|
64
69
|
};
|
|
65
70
|
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Transport, LogEntry } from './types';
|
|
2
|
+
export declare class ConsoleTransport implements Transport {
|
|
3
|
+
private useJson;
|
|
4
|
+
private useColor;
|
|
5
|
+
constructor(options?: {
|
|
6
|
+
useJson?: boolean;
|
|
7
|
+
useColor?: boolean;
|
|
8
|
+
});
|
|
9
|
+
private getColor;
|
|
10
|
+
private getReset;
|
|
11
|
+
log(entry: LogEntry): void;
|
|
12
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ConsoleTransport = void 0;
|
|
4
|
+
var types_1 = require("./types");
|
|
5
|
+
var ConsoleTransport = /** @class */ (function () {
|
|
6
|
+
function ConsoleTransport(options) {
|
|
7
|
+
if (options === void 0) { options = {}; }
|
|
8
|
+
this.useJson = options.useJson || false;
|
|
9
|
+
this.useColor = options.useColor !== false; // Default to true
|
|
10
|
+
}
|
|
11
|
+
ConsoleTransport.prototype.getColor = function (level) {
|
|
12
|
+
if (!this.useColor)
|
|
13
|
+
return '';
|
|
14
|
+
switch (level) {
|
|
15
|
+
case types_1.LogLevel.DEBUG: return '\x1b[34m'; // Blue
|
|
16
|
+
case types_1.LogLevel.INFO: return '\x1b[32m'; // Green
|
|
17
|
+
case types_1.LogLevel.WARN: return '\x1b[33m'; // Yellow
|
|
18
|
+
case types_1.LogLevel.ERROR: return '\x1b[31m'; // Red
|
|
19
|
+
case types_1.LogLevel.FATAL: return '\x1b[35m'; // Magenta
|
|
20
|
+
default: return '\x1b[37m'; // White
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
ConsoleTransport.prototype.getReset = function () {
|
|
24
|
+
return this.useColor ? '\x1b[0m' : '';
|
|
25
|
+
};
|
|
26
|
+
ConsoleTransport.prototype.log = function (entry) {
|
|
27
|
+
if (this.useJson) {
|
|
28
|
+
console.log(JSON.stringify(entry));
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
var color = this.getColor(entry.level);
|
|
32
|
+
var reset = this.getReset();
|
|
33
|
+
var metaStr = entry.meta && Object.keys(entry.meta).length > 0
|
|
34
|
+
? " ".concat(JSON.stringify(entry.meta))
|
|
35
|
+
: '';
|
|
36
|
+
var featureStr = entry.feature ? " [".concat(entry.feature, "]") : '';
|
|
37
|
+
console.log("".concat(entry.timestamp, " [").concat(entry.appName, "]").concat(featureStr, " ").concat(color).concat(entry.level.toUpperCase()).concat(reset, ": ").concat(entry.message).concat(metaStr));
|
|
38
|
+
};
|
|
39
|
+
return ConsoleTransport;
|
|
40
|
+
}());
|
|
41
|
+
exports.ConsoleTransport = ConsoleTransport;
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export declare enum LogLevel {
|
|
2
|
+
DEBUG = "debug",
|
|
3
|
+
INFO = "info",
|
|
4
|
+
WARN = "warn",
|
|
5
|
+
ERROR = "error",
|
|
6
|
+
FATAL = "fatal"
|
|
7
|
+
}
|
|
8
|
+
export declare const LogLevelPriority: Record<LogLevel, number>;
|
|
9
|
+
export interface LogEntry {
|
|
10
|
+
timestamp: string;
|
|
11
|
+
level: LogLevel;
|
|
12
|
+
appName: string;
|
|
13
|
+
feature?: string;
|
|
14
|
+
message: string;
|
|
15
|
+
meta?: Record<string, any>;
|
|
16
|
+
}
|
|
17
|
+
export interface Transport {
|
|
18
|
+
log(entry: LogEntry): void;
|
|
19
|
+
}
|
|
20
|
+
export interface LoggerConfig {
|
|
21
|
+
minLevel: LogLevel;
|
|
22
|
+
appName?: string;
|
|
23
|
+
timeFormat?: string;
|
|
24
|
+
transports?: Transport[];
|
|
25
|
+
messageFormat?: {
|
|
26
|
+
prefix?: string;
|
|
27
|
+
suffix?: string;
|
|
28
|
+
};
|
|
29
|
+
}
|
package/lib/types.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var _a;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.LogLevelPriority = exports.LogLevel = void 0;
|
|
5
|
+
var LogLevel;
|
|
6
|
+
(function (LogLevel) {
|
|
7
|
+
LogLevel["DEBUG"] = "debug";
|
|
8
|
+
LogLevel["INFO"] = "info";
|
|
9
|
+
LogLevel["WARN"] = "warn";
|
|
10
|
+
LogLevel["ERROR"] = "error";
|
|
11
|
+
LogLevel["FATAL"] = "fatal";
|
|
12
|
+
})(LogLevel || (exports.LogLevel = LogLevel = {}));
|
|
13
|
+
exports.LogLevelPriority = (_a = {},
|
|
14
|
+
_a[LogLevel.DEBUG] = 0,
|
|
15
|
+
_a[LogLevel.INFO] = 1,
|
|
16
|
+
_a[LogLevel.WARN] = 2,
|
|
17
|
+
_a[LogLevel.ERROR] = 3,
|
|
18
|
+
_a[LogLevel.FATAL] = 4,
|
|
19
|
+
_a);
|
package/package.json
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "log-tonic",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "A powerful and flexible logging utility for Node.js and TypeScript projects.",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"lib"
|
|
9
|
+
],
|
|
7
10
|
"scripts": {
|
|
8
11
|
"build": "tsc",
|
|
9
12
|
"prepublishOnly": "npm run build",
|
|
@@ -33,4 +36,4 @@
|
|
|
33
36
|
"devDependencies": {
|
|
34
37
|
"typescript": "^5.5.4"
|
|
35
38
|
}
|
|
36
|
-
}
|
|
39
|
+
}
|
package/src/index.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
-
* you may not use this file except in compliance with the License.
|
|
4
|
-
* You may obtain a copy of the License at
|
|
5
|
-
*
|
|
6
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
-
*
|
|
8
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
9
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
-
* See the License for the specific language governing permissions and
|
|
12
|
-
* limitations under the License.
|
|
13
|
-
*
|
|
14
|
-
* Copyright 2024 Sitharaj Seenivasan
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
export { LoggerFactory, LoggerConfig } from './loggerFactory';
|
|
18
|
-
export { default as Logger } from './logger';
|
package/src/logger.ts
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
-
* you may not use this file except in compliance with the License.
|
|
4
|
-
* You may obtain a copy of the License at
|
|
5
|
-
*
|
|
6
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
-
*
|
|
8
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
9
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
-
* See the License for the specific language governing permissions and
|
|
12
|
-
* limitations under the License.
|
|
13
|
-
*
|
|
14
|
-
* Copyright 2024 Sitharaj Seenivasan
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
import { format } from 'date-fns';
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Logger class responsible for handling log messages with different levels and features.
|
|
21
|
-
*
|
|
22
|
-
* @class Logger
|
|
23
|
-
*/
|
|
24
|
-
class Logger {
|
|
25
|
-
private level: string;
|
|
26
|
-
private appName: string;
|
|
27
|
-
private timeFormat: string;
|
|
28
|
-
private messageFormat: { prefix?: string; suffix?: string } | null;
|
|
29
|
-
private levels: { [key: string]: number };
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Constructs a Logger instance.
|
|
33
|
-
*
|
|
34
|
-
* @param {string} level - The logging level (debug, info, error).
|
|
35
|
-
* @param {string} [appName] - The name of the application.
|
|
36
|
-
* @param {string} [timeFormat] - The format for the timestamp.
|
|
37
|
-
* @param {Object} [messageFormat] - The prefix and suffix for log messages.
|
|
38
|
-
*/
|
|
39
|
-
constructor(
|
|
40
|
-
level: string,
|
|
41
|
-
appName?: string,
|
|
42
|
-
timeFormat?: string,
|
|
43
|
-
messageFormat?: { prefix?: string; suffix?: string }
|
|
44
|
-
) {
|
|
45
|
-
this.level = level;
|
|
46
|
-
this.appName = appName || "MyApp";
|
|
47
|
-
this.timeFormat = timeFormat || "YYYY-MM-DD HH:mm:ss";
|
|
48
|
-
this.messageFormat = messageFormat || null;
|
|
49
|
-
this.levels = { debug: 0, info: 1, error: 2 };
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Formats the current timestamp according to the specified time format using date-fns.
|
|
54
|
-
*
|
|
55
|
-
* @returns {string} The formatted timestamp.
|
|
56
|
-
*/
|
|
57
|
-
private formatTime(): string {
|
|
58
|
-
const now = new Date();
|
|
59
|
-
|
|
60
|
-
try {
|
|
61
|
-
return format(now, this.timeFormat);
|
|
62
|
-
} catch (error) {
|
|
63
|
-
console.warn(`Invalid time format provided: ${this.timeFormat}. Falling back to default ISO format.`);
|
|
64
|
-
return now.toISOString().replace("T", " ").split(".")[0]; // Fallback to ISO format
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Formats the log message by adding the specified prefix and suffix.
|
|
69
|
-
*
|
|
70
|
-
* @param {string} message - The log message.
|
|
71
|
-
* @returns {string} The formatted log message.
|
|
72
|
-
*/
|
|
73
|
-
private formatMessage(message: string): string {
|
|
74
|
-
if (this.messageFormat) {
|
|
75
|
-
return `${this.messageFormat.prefix || ""}${message}${
|
|
76
|
-
this.messageFormat.suffix || ""
|
|
77
|
-
}`;
|
|
78
|
-
}
|
|
79
|
-
return message;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Logs a message with the specified level and feature.
|
|
84
|
-
*
|
|
85
|
-
* @param {string} level - The logging level (debug, info, error).
|
|
86
|
-
* @param {string} feature - The feature name associated with the log message.
|
|
87
|
-
* @param {string} message - The log message.
|
|
88
|
-
*/
|
|
89
|
-
private log(level: string, feature: string, message: string): void {
|
|
90
|
-
if (this.levels[level] >= this.levels[this.level]) {
|
|
91
|
-
const timestamp = this.formatTime();
|
|
92
|
-
const formattedMessage = this.formatMessage(message);
|
|
93
|
-
console.log(
|
|
94
|
-
`${timestamp} [${
|
|
95
|
-
this.appName
|
|
96
|
-
}] [${feature}] ${level.toUpperCase()}: ${formattedMessage}`
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Logs an informational message.
|
|
103
|
-
*
|
|
104
|
-
* @param {string} feature - The feature name associated with the log message.
|
|
105
|
-
* @param {string} message - The log message.
|
|
106
|
-
*/
|
|
107
|
-
public info(feature: string, message: string): void {
|
|
108
|
-
this.log("info", feature, message);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Logs an error message.
|
|
113
|
-
*
|
|
114
|
-
* @param {string} feature - The feature name associated with the log message.
|
|
115
|
-
* @param {string} message - The log message.
|
|
116
|
-
*/
|
|
117
|
-
public error(feature: string, message: string): void {
|
|
118
|
-
this.log("error", feature, message);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Logs a debug message.
|
|
123
|
-
*
|
|
124
|
-
* @param {string} feature - The feature name associated with the log message.
|
|
125
|
-
* @param {string} message - The log message.
|
|
126
|
-
*/
|
|
127
|
-
public debug(feature: string, message: string): void {
|
|
128
|
-
this.log("debug", feature, message);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export default Logger;
|
package/src/loggerFactory.ts
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
-
* you may not use this file except in compliance with the License.
|
|
4
|
-
* You may obtain a copy of the License at
|
|
5
|
-
*
|
|
6
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
-
*
|
|
8
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
9
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
-
* See the License for the specific language governing permissions and
|
|
12
|
-
* limitations under the License.
|
|
13
|
-
*
|
|
14
|
-
* Copyright 2024 Sitharaj Seenivasan
|
|
15
|
-
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import Logger from "./logger";
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Interface representing the configuration options for the Logger.
|
|
22
|
-
*/
|
|
23
|
-
export interface LoggerConfig {
|
|
24
|
-
level: "debug" | "info" | "error";
|
|
25
|
-
appName?: string;
|
|
26
|
-
timeFormat?: string;
|
|
27
|
-
messageFormat?: { prefix?: string; suffix?: string };
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* LoggerFactory class responsible for creating and managing Logger instances.
|
|
32
|
-
*
|
|
33
|
-
* @class LoggerFactory
|
|
34
|
-
*/
|
|
35
|
-
export class LoggerFactory {
|
|
36
|
-
private static loggerInstance: Logger | null = null;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Initializes the logger with the provided configuration.
|
|
40
|
-
*
|
|
41
|
-
* @param {LoggerConfig} config - The configuration for the logger.
|
|
42
|
-
* @returns {Logger} The initialized Logger instance.
|
|
43
|
-
*/
|
|
44
|
-
public static initialize(config: LoggerConfig): Logger {
|
|
45
|
-
if (!LoggerFactory.loggerInstance) {
|
|
46
|
-
LoggerFactory.loggerInstance = new Logger(
|
|
47
|
-
config.level,
|
|
48
|
-
config.appName,
|
|
49
|
-
config.timeFormat,
|
|
50
|
-
config.messageFormat
|
|
51
|
-
);
|
|
52
|
-
}
|
|
53
|
-
return LoggerFactory.loggerInstance;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Creates a logger for the specified feature.
|
|
58
|
-
*
|
|
59
|
-
* @param {string} feature - The feature name for which to create the logger.
|
|
60
|
-
* @returns {Object} An object containing methods for logging info, error, and debug messages.
|
|
61
|
-
*/
|
|
62
|
-
public static createLogger(feature: string) {
|
|
63
|
-
if (!LoggerFactory.loggerInstance) {
|
|
64
|
-
throw new Error(
|
|
65
|
-
"Logger is not initialized. Call LoggerFactory.initialize(config) first."
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
return {
|
|
69
|
-
info: (message: string) =>
|
|
70
|
-
LoggerFactory.loggerInstance!.info(feature, message),
|
|
71
|
-
error: (message: string) =>
|
|
72
|
-
LoggerFactory.loggerInstance!.error(feature, message),
|
|
73
|
-
debug: (message: string) =>
|
|
74
|
-
LoggerFactory.loggerInstance!.debug(feature, message),
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "es5", // Target ECMAScript version
|
|
4
|
-
"module": "commonjs", // Module system to be used in the generated code
|
|
5
|
-
"declaration": true, // Generate .d.ts files alongside the compiled .js files
|
|
6
|
-
"outDir": "./lib", // Output directory for compiled JavaScript files
|
|
7
|
-
"rootDir": "./src", // Root directory of your TypeScript source files
|
|
8
|
-
"strict": true, // Enable all strict type-checking options
|
|
9
|
-
"esModuleInterop": true, // Allows default imports from modules with no default export
|
|
10
|
-
"skipLibCheck": true, // Skip type checking of declaration files (to speed up compilation)
|
|
11
|
-
"forceConsistentCasingInFileNames": true // Ensure file names are consistent between case-sensitive and insensitive environments
|
|
12
|
-
},
|
|
13
|
-
"include": ["src/**/*"], // Include all TypeScript files in the src directory
|
|
14
|
-
"exclude": ["node_modules", "lib"] // Exclude the node_modules and lib directories from compilation
|
|
15
|
-
}
|