infront-logger 1.1.18 → 1.1.20

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 CHANGED
@@ -28,6 +28,55 @@ The logger supports both writing to file and to console.
28
28
  staticLogger.warn("Warning message");
29
29
  staticLogger.error("Error occurred", { error: "Details here" });
30
30
 
31
+ ### HTTP Transport
32
+ Enable HTTP transport to send logs to a remote HTTP endpoint:
33
+
34
+ const {BaseLogger} = require('infront-logger');
35
+
36
+ // Basic HTTP transport configuration
37
+ const logger = new BaseLogger("myComponent", {
38
+ http: {
39
+ host: "logs.example.com",
40
+ path: "/api/logs"
41
+ }
42
+ });
43
+
44
+ // HTTP transport with custom headers
45
+ const logger = new BaseLogger("myComponent", {
46
+ http: {
47
+ host: "logs.example.com",
48
+ path: "/api/logs",
49
+ headers: {
50
+ "Authorization": "Bearer token123",
51
+ "X-API-Key": "key456"
52
+ }
53
+ }
54
+ });
55
+
56
+ // HTTP transport with batching enabled
57
+ const logger = new BaseLogger("myComponent", {
58
+ http: {
59
+ host: "logs.example.com",
60
+ path: "/api/logs",
61
+ batch: true,
62
+ batchInterval: 2000 // Batch logs every 2 seconds
63
+ }
64
+ });
65
+
66
+ // HTTP transport with common keys added to all payloads
67
+ const logger = new BaseLogger("myComponent", {
68
+ http: {
69
+ host: "logs.example.com",
70
+ path: "/api/logs",
71
+ commonKeys: {
72
+ applicationName: "stream-engine",
73
+ environment: "production"
74
+ }
75
+ }
76
+ });
77
+
78
+ logger.info("This log will be sent to the HTTP endpoint");
79
+
31
80
  ### HttpLogger (Express Logger)
32
81
  const {HttpLogger} = require('infront-logger');
33
82
 
@@ -202,6 +251,14 @@ All methods support both string messages and context objects. Context objects ar
202
251
  - errorFilename - name of the default error log file (default: "error.log")
203
252
  - console - boolean - should log to console (default: true)
204
253
  - consoleJSON - boolean - print full json to console (default : false)
254
+ - http - object - Enable HTTP transport to send logs to an HTTP endpoint
255
+ - `http.host` - string (required) - The HTTP endpoint host (e.g., "logs.example.com")
256
+ - `http.path` - string (required) - The HTTP endpoint path (e.g., "/api/logs")
257
+ - `http.headers` - object (optional) - Custom headers to include in HTTP requests (default: `{}`)
258
+ - `http.batch` - boolean (optional) - Enable batching of log messages (default: `false`)
259
+ - `http.batchInterval` - number (optional) - Interval in milliseconds for batching logs when `batch` is enabled (default: `1000`)
260
+ - `http.commonKeys` - object (optional) - Common fields to add to all log payloads sent via HTTP transport (default: `undefined`)
261
+ - Note: HTTP transport always uses SSL/HTTPS (`ssl: true`)
205
262
  - sampleRate - number (0.0 to 1.0) - Percentage of logs to sample. 1.0 = log everything, 0.5 = log 50%, etc. Applies to all enabled transports (console and/or file) (default: 1.0)
206
263
  - bypassSamplingForErrors - boolean - If true, error logs bypass sampling and are always logged regardless of sampleRate settings (default: true)
207
264
  - throttleMs - number - Throttle logs by milliseconds. Prevents the same log message from being logged more than once within the specified time window. 0 = no throttling (default: 0)
package/dist/index.es.js CHANGED
@@ -99,11 +99,37 @@ function createTransport(options) {
99
99
  }
100
100
  return new transports.DailyRotateFile(options);
101
101
  }
102
+ const loggerCache = /* @__PURE__ */ new Map();
103
+ function transportCacheKey(o) {
104
+ return JSON.stringify({
105
+ dirname: o.dirname,
106
+ filename: o.filename,
107
+ errorFilename: o.errorFilename,
108
+ level: o.level,
109
+ levels: o.levels,
110
+ maxFiles: o.maxFiles,
111
+ maxFileSize: o.maxFileSize,
112
+ zippedArchive: o.zippedArchive,
113
+ dateFormat: o.dateFormat,
114
+ createSymlink: o.createSymlink,
115
+ symlinkName: o.symlinkName,
116
+ file: !!o.file,
117
+ console: !!o.console,
118
+ consoleJSON: !!o.consoleJSON,
119
+ http: o.http || null
120
+ });
121
+ }
102
122
  class Logger {
103
123
  constructor(options = {}) {
104
124
  this.options = { ...OPTIONS$1, ...options };
105
125
  if (!this.options.filename.includes(".")) this.options.filename += ".log";
106
126
  if (!this.options.errorFilename.includes(".")) this.options.errorFilename += ".log";
127
+ const cacheKey = transportCacheKey(this.options);
128
+ const cachedLogger = loggerCache.get(cacheKey);
129
+ if (cachedLogger) {
130
+ this.logger = cachedLogger;
131
+ return;
132
+ }
107
133
  let trans = [];
108
134
  let exceptionHandlers = [new transports.Console({
109
135
  format: getFormatter("console", this.options)
@@ -131,6 +157,26 @@ class Logger {
131
157
  format: getFormatter("console", this.options)
132
158
  }));
133
159
  }
160
+ if (this.options.http) {
161
+ const httpTransport = new transports.Http({
162
+ ...this.options,
163
+ format: format$1((info) => {
164
+ const payload = {
165
+ ...this.options.http.commonKeys,
166
+ text: info
167
+ };
168
+ return payload;
169
+ })(),
170
+ host: this.options.http.host,
171
+ path: this.options.http.path,
172
+ headers: this.options.http.headers || {},
173
+ ssl: true,
174
+ batch: this.options.http.batch !== void 0 ? this.options.http.batch : false,
175
+ batchInterval: this.options.http.batchInterval || 1e3,
176
+ handleExceptions: true
177
+ });
178
+ trans.push(httpTransport);
179
+ }
134
180
  this.logger = createLogger({
135
181
  level: this.options.level,
136
182
  levels: this.options.levels,
@@ -139,6 +185,7 @@ class Logger {
139
185
  transports: trans
140
186
  // exceptionHandlers
141
187
  });
188
+ loggerCache.set(cacheKey, this.logger);
142
189
  }
143
190
  }
144
191
  const redact$2 = (data, ...sensitiveKeysList) => {
package/dist/index.umd.js CHANGED
@@ -103,11 +103,37 @@
103
103
  }
104
104
  return new transports.DailyRotateFile(options);
105
105
  }
106
+ const loggerCache = /* @__PURE__ */ new Map();
107
+ function transportCacheKey(o) {
108
+ return JSON.stringify({
109
+ dirname: o.dirname,
110
+ filename: o.filename,
111
+ errorFilename: o.errorFilename,
112
+ level: o.level,
113
+ levels: o.levels,
114
+ maxFiles: o.maxFiles,
115
+ maxFileSize: o.maxFileSize,
116
+ zippedArchive: o.zippedArchive,
117
+ dateFormat: o.dateFormat,
118
+ createSymlink: o.createSymlink,
119
+ symlinkName: o.symlinkName,
120
+ file: !!o.file,
121
+ console: !!o.console,
122
+ consoleJSON: !!o.consoleJSON,
123
+ http: o.http || null
124
+ });
125
+ }
106
126
  class Logger {
107
127
  constructor(options = {}) {
108
128
  this.options = { ...OPTIONS$1, ...options };
109
129
  if (!this.options.filename.includes(".")) this.options.filename += ".log";
110
130
  if (!this.options.errorFilename.includes(".")) this.options.errorFilename += ".log";
131
+ const cacheKey = transportCacheKey(this.options);
132
+ const cachedLogger = loggerCache.get(cacheKey);
133
+ if (cachedLogger) {
134
+ this.logger = cachedLogger;
135
+ return;
136
+ }
111
137
  let trans = [];
112
138
  let exceptionHandlers = [new transports.Console({
113
139
  format: getFormatter("console", this.options)
@@ -135,6 +161,26 @@
135
161
  format: getFormatter("console", this.options)
136
162
  }));
137
163
  }
164
+ if (this.options.http) {
165
+ const httpTransport = new transports.Http({
166
+ ...this.options,
167
+ format: format$1((info) => {
168
+ const payload = {
169
+ ...this.options.http.commonKeys,
170
+ text: info
171
+ };
172
+ return payload;
173
+ })(),
174
+ host: this.options.http.host,
175
+ path: this.options.http.path,
176
+ headers: this.options.http.headers || {},
177
+ ssl: true,
178
+ batch: this.options.http.batch !== void 0 ? this.options.http.batch : false,
179
+ batchInterval: this.options.http.batchInterval || 1e3,
180
+ handleExceptions: true
181
+ });
182
+ trans.push(httpTransport);
183
+ }
138
184
  this.logger = createLogger({
139
185
  level: this.options.level,
140
186
  levels: this.options.levels,
@@ -143,6 +189,7 @@
143
189
  transports: trans
144
190
  // exceptionHandlers
145
191
  });
192
+ loggerCache.set(cacheKey, this.logger);
146
193
  }
147
194
  }
148
195
  const redact$2 = (data, ...sensitiveKeysList) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "infront-logger",
3
- "version": "1.1.18",
3
+ "version": "1.1.20",
4
4
  "description": "",
5
5
  "files": [
6
6
  "dist"