Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions metalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const path = require('node:path');
const util = require('node:util');
const EventEmitter = require('node:events');
const readline = require('node:readline');

const metautil = require('metautil');
const concolor = require('concolor');

Expand All @@ -30,6 +31,7 @@ const CRASH_EVENTS = [
'SIGUSR2',
'uncaughtException',
'unhandledRejection',
'beforeExit',
'exit',
];

Expand Down Expand Up @@ -105,8 +107,9 @@ class BufferedStream extends EventEmitter {
if (!stream) throw new Error('Stream is required');
this.#writable = stream;
if (writeBuffer) this.#threshold = writeBuffer;
const interval = flushInterval || DEFAULT_FLUSH_INTERVAL;
this.#flushTimer = setInterval(() => void this.flush(), interval);
let flushEvery = DEFAULT_FLUSH_INTERVAL;
if (flushInterval > 0) flushEvery = flushInterval;
this.#flushTimer = setInterval(() => void this.flush(), flushEvery);
}

write(buffer) {
Expand Down Expand Up @@ -134,6 +137,7 @@ class BufferedStream extends EventEmitter {
this.#size = 0;
this.#writable.write(buffer, (error) => {
this.#flushing = false;
if (!error && this.#size > 0) return void this.flush(callback);
this.emit('drain');
if (callback) callback(error);
});
Expand Down Expand Up @@ -193,7 +197,7 @@ class Formatter {
if (metautil.isError(head)) {
json.error = this.expandError(head);
start = 1;
} else if (typeof head === 'object') {
} else if (head !== null && typeof head === 'object') {
Object.assign(json, head);
start = 1;
}
Expand Down Expand Up @@ -243,7 +247,7 @@ class Console {
}

count(label = 'default') {
const current = this.#counts.get(label) || 0;
const current = this.#counts.get(label) ?? 0;
const countValue = current + 1;
this.#counts.set(label, countValue);
this.#logger.write('debug', this.#groupIndent, [`${label}: ${countValue}`]);
Expand Down Expand Up @@ -310,9 +314,7 @@ class Console {
data = data.map((item) => {
const record = {};
for (const prop of properties) {
if (Object.prototype.hasOwnProperty.call(item, prop)) {
record[prop] = item[prop];
}
if (Object.hasOwn(item, prop)) record[prop] = item[prop];
}
return record;
});
Expand Down Expand Up @@ -430,13 +432,17 @@ class Logger extends EventEmitter {
this.emit('close');
return;
}
const stream = this.#stream;
if (stream.destroyed || stream.closed) return;
clearTimeout(this.#rotationTimer);
this.#rotationTimer = null;
this.active = false;
await this.#buffer.close();
const stream = this.#stream;
const buffer = this.#buffer;
this.#buffer = null;
if (buffer && stream && !stream.destroyed && !stream.closed) {
await buffer.close();
}
this.emit('close');
if (!this.#file) return;
try {
const stats = await fsp.stat(this.#file);
if (stats.size === 0) {
Expand Down Expand Up @@ -484,7 +490,7 @@ class Logger extends EventEmitter {

write(tag, indent, args) {
const toStdout = this.#toStdout[tag];
const toFile = this.#toFile[tag];
const toFile = this.#toFile[tag] && this.#buffer;
if (!toStdout && !toFile) return;
if (this.#options.json) {
const line = `${this.#formatter.formatJson(tag, indent, args)}\n`;
Expand Down
11 changes: 8 additions & 3 deletions test/buffering.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';

const test = require('node:test');
const { BufferedStream } = require('../metalog');
const fs = require('node:fs');
const path = require('node:path');

const { BufferedStream } = require('../metalog.js');

test('BufferedStream with stream', async () => {
const logDir = './log';
if (!fs.existsSync(logDir)) {
Expand Down Expand Up @@ -47,7 +48,9 @@ test('BufferedStream timer management', async () => {

bufferedStream.write(Buffer.from('Timer test message\n'));

await new Promise((resolve) => setTimeout(resolve, 200));
await new Promise((resolve) => {
setTimeout(resolve, 200);
});

await bufferedStream.close();
stream.destroy();
Expand All @@ -70,7 +73,9 @@ test('BufferedStream auto-flush on threshold', async () => {
bufferedStream.write(Buffer.from(`Message ${i}\n`));
}

await new Promise((resolve) => setTimeout(resolve, 100));
await new Promise((resolve) => {
setTimeout(resolve, 100);
});
await bufferedStream.close();
stream.destroy();
});
Expand Down
1 change: 1 addition & 0 deletions test/console.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const test = require('node:test');

const { Console, Logger } = require('..');

test('Console basic methods', async () => {
Expand Down
1 change: 1 addition & 0 deletions test/file.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const test = require('node:test');

const { Logger } = require('..');

const createLogger = () =>
Expand Down
22 changes: 16 additions & 6 deletions test/formatting.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

const test = require('node:test');
const { Formatter } = require('../metalog');

const { Formatter } = require('../metalog.js');

test('Formatter basic formatting', () => {
const formatter = new Formatter({
Expand Down Expand Up @@ -166,12 +167,21 @@ test('Formatter JSON with mixed arguments', () => {
}
});

test('Formatter default options', () => {
const formatter = new Formatter();
test('Formatter JSON with null head', () => {
const formatter = new Formatter({
json: true,
worker: 'W9',
home: '/test/home',
});

const json = formatter.formatJson('info', 0, [null, 'after null']);
const parsed = JSON.parse(json);

const formatted = formatter.format('info', 0, ['Default test']);
if (!formatted.includes('Default test')) {
throw new Error('Default formatter failed');
if (parsed.message !== 'null after null') {
throw new Error(`Null head mishandled: ${parsed.message}`);
}
if (Object.hasOwn(parsed, 'null')) {
throw new Error('Null was incorrectly assigned as object');
}
});

Expand Down
1 change: 1 addition & 0 deletions test/logger.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const test = require('node:test');

const { Logger } = require('..');

test('Logger basic functionality', async () => {
Expand Down
1 change: 1 addition & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const test = require('node:test');

const { nowDays, nameToDays } = require('..');

test('nowDays utility function', () => {
Expand Down
Loading