@pgpmjs/logger 1.5.0 → 2.0.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/esm/index.js +1 -1
- package/esm/logger.js +47 -1
- package/index.d.ts +2 -1
- package/index.js +5 -1
- package/logger.d.ts +2 -0
- package/logger.js +49 -2
- package/package.json +2 -2
package/esm/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { Logger, createLogger } from './logger';
|
|
1
|
+
export { Logger, createLogger, setLogLevel, setShowTimestamp, setLogFormat, setLogScopes } from './logger';
|
package/esm/logger.js
CHANGED
|
@@ -51,6 +51,12 @@ let showTimestamp = process.env.LOG_TIMESTAMP?.toLowerCase() === 'true' ||
|
|
|
51
51
|
export const setShowTimestamp = (show) => {
|
|
52
52
|
showTimestamp = show;
|
|
53
53
|
};
|
|
54
|
+
// Parse LOG_FORMAT from environment (default: 'pretty')
|
|
55
|
+
let logFormat = process.env.LOG_FORMAT?.toLowerCase() === 'json' ? 'json' : 'pretty';
|
|
56
|
+
// Update log format at runtime
|
|
57
|
+
export const setLogFormat = (format) => {
|
|
58
|
+
logFormat = format;
|
|
59
|
+
};
|
|
54
60
|
const parseScopeFilter = (env) => {
|
|
55
61
|
const include = new Set();
|
|
56
62
|
const exclude = new Set();
|
|
@@ -94,6 +100,47 @@ export class Logger {
|
|
|
94
100
|
!allowedScopes.has(this.scope)) {
|
|
95
101
|
return;
|
|
96
102
|
}
|
|
103
|
+
const stream = level === 'error' ? process.stderr : process.stdout;
|
|
104
|
+
if (logFormat === 'json') {
|
|
105
|
+
// JSON format: structured output for log aggregators
|
|
106
|
+
const entry = {
|
|
107
|
+
timestamp: new Date().toISOString(),
|
|
108
|
+
level,
|
|
109
|
+
scope: this.scope
|
|
110
|
+
};
|
|
111
|
+
// Extract message and data from args
|
|
112
|
+
const strings = [];
|
|
113
|
+
for (const arg of args) {
|
|
114
|
+
if (typeof arg === 'string') {
|
|
115
|
+
strings.push(arg);
|
|
116
|
+
}
|
|
117
|
+
else if (arg instanceof Error) {
|
|
118
|
+
if (!entry.error) {
|
|
119
|
+
entry.error = {
|
|
120
|
+
name: arg.name,
|
|
121
|
+
message: arg.message,
|
|
122
|
+
stack: arg.stack
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
// Preserve additional errors in the message string
|
|
127
|
+
strings.push(`Error[${arg.name}]: ${arg.message}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
else if (typeof arg === 'object' && arg !== null) {
|
|
131
|
+
Object.assign(entry, arg);
|
|
132
|
+
}
|
|
133
|
+
else if (arg !== undefined && arg !== null) {
|
|
134
|
+
strings.push(String(arg));
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (strings.length > 0) {
|
|
138
|
+
entry.message = strings.join(' ');
|
|
139
|
+
}
|
|
140
|
+
stream.write(safeStringify(entry) + '\n');
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
// Pretty format: colored output for terminals
|
|
97
144
|
const tag = yanse.bold(`[${this.scope}]`);
|
|
98
145
|
const color = levelColors[level];
|
|
99
146
|
const prefix = color(`${level.toUpperCase()}:`);
|
|
@@ -103,7 +150,6 @@ export class Logger {
|
|
|
103
150
|
? color(normalized)
|
|
104
151
|
: normalized;
|
|
105
152
|
});
|
|
106
|
-
const stream = level === 'error' ? process.stderr : process.stdout;
|
|
107
153
|
const outputParts = showTimestamp
|
|
108
154
|
? [yanse.dim(`[${new Date().toISOString()}]`), tag, prefix, ...formattedArgs]
|
|
109
155
|
: [tag, prefix, ...formattedArgs];
|
package/index.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export { Logger, createLogger } from './logger';
|
|
1
|
+
export { Logger, createLogger, setLogLevel, setShowTimestamp, setLogFormat, setLogScopes } from './logger';
|
|
2
|
+
export type { LogLevel, LogFormat } from './logger';
|
package/index.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createLogger = exports.Logger = void 0;
|
|
3
|
+
exports.setLogScopes = exports.setLogFormat = exports.setShowTimestamp = exports.setLogLevel = exports.createLogger = exports.Logger = void 0;
|
|
4
4
|
var logger_1 = require("./logger");
|
|
5
5
|
Object.defineProperty(exports, "Logger", { enumerable: true, get: function () { return logger_1.Logger; } });
|
|
6
6
|
Object.defineProperty(exports, "createLogger", { enumerable: true, get: function () { return logger_1.createLogger; } });
|
|
7
|
+
Object.defineProperty(exports, "setLogLevel", { enumerable: true, get: function () { return logger_1.setLogLevel; } });
|
|
8
|
+
Object.defineProperty(exports, "setShowTimestamp", { enumerable: true, get: function () { return logger_1.setShowTimestamp; } });
|
|
9
|
+
Object.defineProperty(exports, "setLogFormat", { enumerable: true, get: function () { return logger_1.setLogFormat; } });
|
|
10
|
+
Object.defineProperty(exports, "setLogScopes", { enumerable: true, get: function () { return logger_1.setLogScopes; } });
|
package/logger.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export type LogLevel = 'info' | 'warn' | 'error' | 'debug' | 'success';
|
|
2
|
+
export type LogFormat = 'pretty' | 'json';
|
|
2
3
|
export declare const setLogLevel: (level: LogLevel) => void;
|
|
3
4
|
export declare const setShowTimestamp: (show: boolean) => void;
|
|
5
|
+
export declare const setLogFormat: (format: LogFormat) => void;
|
|
4
6
|
export declare const setLogScopes: (scopes: string[]) => void;
|
|
5
7
|
export declare class Logger {
|
|
6
8
|
private scope;
|
package/logger.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.createLogger = exports.Logger = exports.setLogScopes = exports.setShowTimestamp = exports.setLogLevel = void 0;
|
|
6
|
+
exports.createLogger = exports.Logger = exports.setLogScopes = exports.setLogFormat = exports.setShowTimestamp = exports.setLogLevel = void 0;
|
|
7
7
|
const yanse_1 = __importDefault(require("yanse"));
|
|
8
8
|
const levelPriority = {
|
|
9
9
|
debug: 0,
|
|
@@ -59,6 +59,13 @@ const setShowTimestamp = (show) => {
|
|
|
59
59
|
showTimestamp = show;
|
|
60
60
|
};
|
|
61
61
|
exports.setShowTimestamp = setShowTimestamp;
|
|
62
|
+
// Parse LOG_FORMAT from environment (default: 'pretty')
|
|
63
|
+
let logFormat = process.env.LOG_FORMAT?.toLowerCase() === 'json' ? 'json' : 'pretty';
|
|
64
|
+
// Update log format at runtime
|
|
65
|
+
const setLogFormat = (format) => {
|
|
66
|
+
logFormat = format;
|
|
67
|
+
};
|
|
68
|
+
exports.setLogFormat = setLogFormat;
|
|
62
69
|
const parseScopeFilter = (env) => {
|
|
63
70
|
const include = new Set();
|
|
64
71
|
const exclude = new Set();
|
|
@@ -103,6 +110,47 @@ class Logger {
|
|
|
103
110
|
!allowedScopes.has(this.scope)) {
|
|
104
111
|
return;
|
|
105
112
|
}
|
|
113
|
+
const stream = level === 'error' ? process.stderr : process.stdout;
|
|
114
|
+
if (logFormat === 'json') {
|
|
115
|
+
// JSON format: structured output for log aggregators
|
|
116
|
+
const entry = {
|
|
117
|
+
timestamp: new Date().toISOString(),
|
|
118
|
+
level,
|
|
119
|
+
scope: this.scope
|
|
120
|
+
};
|
|
121
|
+
// Extract message and data from args
|
|
122
|
+
const strings = [];
|
|
123
|
+
for (const arg of args) {
|
|
124
|
+
if (typeof arg === 'string') {
|
|
125
|
+
strings.push(arg);
|
|
126
|
+
}
|
|
127
|
+
else if (arg instanceof Error) {
|
|
128
|
+
if (!entry.error) {
|
|
129
|
+
entry.error = {
|
|
130
|
+
name: arg.name,
|
|
131
|
+
message: arg.message,
|
|
132
|
+
stack: arg.stack
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
// Preserve additional errors in the message string
|
|
137
|
+
strings.push(`Error[${arg.name}]: ${arg.message}`);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
else if (typeof arg === 'object' && arg !== null) {
|
|
141
|
+
Object.assign(entry, arg);
|
|
142
|
+
}
|
|
143
|
+
else if (arg !== undefined && arg !== null) {
|
|
144
|
+
strings.push(String(arg));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (strings.length > 0) {
|
|
148
|
+
entry.message = strings.join(' ');
|
|
149
|
+
}
|
|
150
|
+
stream.write(safeStringify(entry) + '\n');
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
// Pretty format: colored output for terminals
|
|
106
154
|
const tag = yanse_1.default.bold(`[${this.scope}]`);
|
|
107
155
|
const color = levelColors[level];
|
|
108
156
|
const prefix = color(`${level.toUpperCase()}:`);
|
|
@@ -112,7 +160,6 @@ class Logger {
|
|
|
112
160
|
? color(normalized)
|
|
113
161
|
: normalized;
|
|
114
162
|
});
|
|
115
|
-
const stream = level === 'error' ? process.stderr : process.stdout;
|
|
116
163
|
const outputParts = showTimestamp
|
|
117
164
|
? [yanse_1.default.dim(`[${new Date().toISOString()}]`), tag, prefix, ...formattedArgs]
|
|
118
165
|
: [tag, prefix, ...formattedArgs];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pgpmjs/logger",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"author": "Constructive <developers@constructive.io>",
|
|
5
5
|
"description": "Simple logger utility",
|
|
6
6
|
"main": "index.js",
|
|
@@ -42,5 +42,5 @@
|
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"makage": "^0.1.10"
|
|
44
44
|
},
|
|
45
|
-
"gitHead": "
|
|
45
|
+
"gitHead": "390f4dd57fc158554518ec454bf2a4856d550552"
|
|
46
46
|
}
|