metalog 3.1.11 → 3.1.13

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.
Files changed (3) hide show
  1. package/metalog.d.ts +2 -1
  2. package/metalog.js +43 -46
  3. package/package.json +11 -11
package/metalog.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import EventEmitter = require('node:events');
1
+ import { EventEmitter } from 'node:events';
2
2
 
3
3
  interface LoggerOptions {
4
4
  path: string;
@@ -27,6 +27,7 @@ export class Logger extends EventEmitter {
27
27
  flushTimer: NodeJS.Timer;
28
28
  lock: boolean;
29
29
  buffer: Array<Buffer>;
30
+ bufferLength: number;
30
31
  file: string;
31
32
  toFile: Record<string, boolean>;
32
33
  fsEnabled: boolean;
package/metalog.js CHANGED
@@ -47,7 +47,7 @@ const DEFAULT_FLAGS = {
47
47
  error: false,
48
48
  };
49
49
 
50
- const logTypes = (types = LOG_TYPES) => {
50
+ const logTypes = (types) => {
51
51
  const flags = { ...DEFAULT_FLAGS };
52
52
  for (const type of types) {
53
53
  flags[type] = true;
@@ -77,74 +77,74 @@ const getNextReopen = () => {
77
77
  return nextDate - curTime + DAY_MILLISECONDS;
78
78
  };
79
79
 
80
- const isError = (val) =>
81
- Object.prototype.toString.call(val) === '[object Error]';
82
-
83
80
  class Console {
81
+ #write;
82
+ #groupIndent = 0;
83
+ #counts = new Map();
84
+ #times = new Map();
85
+ #readline = readline;
86
+
84
87
  constructor(write) {
85
- this._write = write;
86
- this._groupIndent = 0;
87
- this._counts = new Map();
88
- this._times = new Map();
88
+ this.#write = write;
89
89
  }
90
90
 
91
91
  assert(assertion, ...args) {
92
92
  try {
93
93
  console.assert(assertion, ...args);
94
94
  } catch (err) {
95
- this._write('error', this._groupIndent, err.stack);
95
+ this.#write('error', this.#groupIndent, err.stack);
96
96
  }
97
97
  }
98
98
 
99
99
  clear() {
100
- readline.cursorTo(process.stdout, 0, 0);
101
- readline.clearScreenDown(process.stdout);
100
+ this.#readline.cursorTo(process.stdout, 0, 0);
101
+ this.#readline.clearScreenDown(process.stdout);
102
102
  }
103
103
 
104
104
  count(label = 'default') {
105
- let cnt = this._counts.get(label) || 0;
105
+ let cnt = this.#counts.get(label) || 0;
106
106
  cnt++;
107
- this._counts.set(label, cnt);
108
- this._write('debug', this._groupIndent, `${label}: ${cnt}`);
107
+ this.#counts.set(label, cnt);
108
+ this.#write('debug', this.#groupIndent, `${label}: ${cnt}`);
109
109
  }
110
110
 
111
111
  countReset(label = 'default') {
112
- this._counts.delete(label);
112
+ this.#counts.delete(label);
113
113
  }
114
114
 
115
115
  debug(...args) {
116
- this._write('debug', this._groupIndent, ...args);
116
+ this.#write('debug', this.#groupIndent, ...args);
117
117
  }
118
118
 
119
119
  dir(...args) {
120
- this._write('debug', this._groupIndent, ...args);
120
+ this.#write('debug', this.#groupIndent, ...args);
121
121
  }
122
122
 
123
123
  trace(...args) {
124
124
  const msg = util.format(...args);
125
125
  const err = new Error(msg);
126
- this._write('debug', this._groupIndent, `Trace${err.stack}`);
126
+ this.#write('debug', this.#groupIndent, `Trace${err.stack}`);
127
127
  }
128
128
 
129
129
  info(...args) {
130
- this._write('info', this._groupIndent, ...args);
130
+ this.#write('info', this.#groupIndent, ...args);
131
131
  }
132
132
 
133
133
  log(...args) {
134
- this._write('log', this._groupIndent, ...args);
134
+ this.#write('log', this.#groupIndent, ...args);
135
135
  }
136
136
 
137
137
  warn(...args) {
138
- this._write('warn', this._groupIndent, ...args);
138
+ this.#write('warn', this.#groupIndent, ...args);
139
139
  }
140
140
 
141
141
  error(...args) {
142
- this._write('error', this._groupIndent, ...args);
142
+ this.#write('error', this.#groupIndent, ...args);
143
143
  }
144
144
 
145
145
  group(...args) {
146
146
  if (args.length !== 0) this.log(...args);
147
- this._groupIndent += INDENT;
147
+ this.#groupIndent += INDENT;
148
148
  }
149
149
 
150
150
  groupCollapsed(...args) {
@@ -152,34 +152,34 @@ class Console {
152
152
  }
153
153
 
154
154
  groupEnd() {
155
- if (this._groupIndent.length === 0) return;
156
- this._groupIndent -= INDENT;
155
+ if (this.#groupIndent.length === 0) return;
156
+ this.#groupIndent -= INDENT;
157
157
  }
158
158
 
159
159
  table(tabularData) {
160
- this._write('log', 0, JSON.stringify(tabularData));
160
+ this.#write('log', 0, JSON.stringify(tabularData));
161
161
  }
162
162
 
163
163
  time(label = 'default') {
164
- this._times.set(label, process.hrtime());
164
+ this.#times.set(label, process.hrtime());
165
165
  }
166
166
 
167
167
  timeEnd(label = 'default') {
168
- const startTime = this._times.get(label);
168
+ const startTime = this.#times.get(label);
169
169
  const totalTime = process.hrtime(startTime);
170
170
  const totalTimeMs = totalTime[0] * 1e3 + totalTime[1] / 1e6;
171
171
  this.timeLog(label, `${label}: ${totalTimeMs}ms`);
172
- this._times.delete(label);
172
+ this.#times.delete(label);
173
173
  }
174
174
 
175
175
  timeLog(label, ...args) {
176
- const startTime = this._times.get(label);
176
+ const startTime = this.#times.get(label);
177
177
  if (startTime === undefined) {
178
178
  const msg = `Warning: No such label '${label}'`;
179
- this._write('warn', this._groupIndent, msg);
179
+ this.#write('warn', this.#groupIndent, msg);
180
180
  return;
181
181
  }
182
- this._write('debug', this._groupIndent, ...args);
182
+ this.#write('debug', this.#groupIndent, ...args);
183
183
  }
184
184
  }
185
185
 
@@ -188,7 +188,7 @@ class Logger extends events.EventEmitter {
188
188
  super();
189
189
  const { workerId = 0, createStream = fs.createWriteStream } = args;
190
190
  const { writeInterval, writeBuffer, keepDays, home, json } = args;
191
- const { toFile = [], toStdout = [] } = args;
191
+ const { toFile = LOG_TYPES, toStdout = LOG_TYPES } = args;
192
192
  this.active = false;
193
193
  this.path = args.path;
194
194
  this.workerId = `W${workerId}`;
@@ -203,6 +203,7 @@ class Logger extends events.EventEmitter {
203
203
  this.flushTimer = null;
204
204
  this.lock = false;
205
205
  this.buffer = [];
206
+ this.bufferLength = 0;
206
207
  this.file = '';
207
208
  this.toFile = logTypes(toFile);
208
209
  this.fsEnabled = toFile.length !== 0;
@@ -216,10 +217,7 @@ class Logger extends events.EventEmitter {
216
217
  fs.access(this.path, (err) => {
217
218
  if (!err) resolve();
218
219
  fs.mkdir(this.path, (err) => {
219
- if (!err || err.code === 'EEXIST') {
220
- resolve();
221
- return;
222
- }
220
+ if (!err || err.code === 'EEXIST') return void resolve();
223
221
  const error = new Error(`Can not create directory: ${this.path}\n`);
224
222
  this.emit('error', error);
225
223
  reject();
@@ -249,8 +247,7 @@ class Logger extends events.EventEmitter {
249
247
  });
250
248
  }, nextReopen);
251
249
  if (this.keepDays) await this.rotate();
252
- const options = { flags: 'a', bufferSize: this.writeBuffer };
253
- this.stream = this.createStream(this.file, options);
250
+ this.stream = this.createStream(this.file, { flags: 'a' });
254
251
  this.flushTimer = setInterval(() => {
255
252
  this.flush();
256
253
  }, this.writeInterval);
@@ -277,10 +274,7 @@ class Logger extends events.EventEmitter {
277
274
  }
278
275
  return new Promise((resolve, reject) => {
279
276
  this.flush((err) => {
280
- if (err) {
281
- reject(err);
282
- return;
283
- }
277
+ if (err) return void reject(err);
284
278
  this.active = false;
285
279
  stream.end(() => {
286
280
  clearInterval(this.flushTimer);
@@ -351,13 +345,13 @@ class Logger extends events.EventEmitter {
351
345
  level: type,
352
346
  message: null,
353
347
  };
354
- if (isError(args[0])) {
348
+ if (metautil.isError(args[0])) {
355
349
  log.err = this.expandError(args[0]);
356
350
  args = args.slice(1);
357
351
  } else if (typeof args[0] === 'object') {
358
352
  Object.assign(log, args[0]);
359
- if (isError(log.err)) log.err = this.expandError(log.err);
360
- if (isError(log.error)) log.error = this.expandError(log.error);
353
+ if (metautil.isError(log.err)) log.err = this.expandError(log.err);
354
+ if (metautil.isError(log.error)) log.error = this.expandError(log.error);
361
355
  args = args.slice(1);
362
356
  }
363
357
  log.message = util.format(...args);
@@ -377,6 +371,8 @@ class Logger extends events.EventEmitter {
377
371
  : this.formatFile(type, indent, ...args);
378
372
  const buffer = Buffer.from(line + '\n');
379
373
  this.buffer.push(buffer);
374
+ this.bufferLength += buffer.length;
375
+ if (this.bufferLength >= this.writeBuffer) this.flush();
380
376
  }
381
377
  }
382
378
 
@@ -398,6 +394,7 @@ class Logger extends events.EventEmitter {
398
394
  this.lock = true;
399
395
  const buffer = Buffer.concat(this.buffer);
400
396
  this.buffer.length = 0;
397
+ this.bufferLength = 0;
401
398
  this.stream.write(buffer, () => {
402
399
  this.lock = false;
403
400
  this.emit('unlocked');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "metalog",
3
- "version": "3.1.11",
3
+ "version": "3.1.13",
4
4
  "author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>",
5
5
  "description": "Logger for Metarhia",
6
6
  "license": "MIT",
@@ -46,21 +46,21 @@
46
46
  "fmt": "prettier --write \"**/*.js\" \"**/*.json\" \"**/*.md\" \"**/.*rc\" \"**/*.ts\""
47
47
  },
48
48
  "engines": {
49
- "node": "16 || 18 || 19 || 20"
49
+ "node": "18 || 20"
50
50
  },
51
51
  "dependencies": {
52
- "concolor": "^1.0.6",
53
- "metautil": "^3.7.2"
52
+ "concolor": "^1.1.0",
53
+ "metautil": "^3.15.0"
54
54
  },
55
55
  "devDependencies": {
56
- "@types/node": "^18.16.1",
57
- "eslint": "^8.36.0",
58
- "eslint-config-metarhia": "^8.1.0",
59
- "eslint-config-prettier": "^8.7.0",
56
+ "@types/node": "^20.4.1",
57
+ "eslint": "^8.44.0",
58
+ "eslint-config-metarhia": "^8.2.0",
59
+ "eslint-config-prettier": "^9.0.0",
60
60
  "eslint-plugin-import": "^2.27.5",
61
- "eslint-plugin-prettier": "^4.0.0",
61
+ "eslint-plugin-prettier": "^5.0.0-alpha.2",
62
62
  "metatests": "^0.8.2",
63
- "prettier": "^2.8.4",
64
- "typescript": "^5.0.4"
63
+ "prettier": "^3.0.0",
64
+ "typescript": "^5.1.6"
65
65
  }
66
66
  }