Skip to content
Open
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
16 changes: 16 additions & 0 deletions src/Logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const stream = require('stream');
const { Transform: TransformStream } = stream;

class Logger extends TransformStream {
constructor(options = {}) {
options.writableObjectMode = true;
super(options);
}

_write(request, encoding, next) {
this.push(`${request.id} ${request.duration}\n`);
next();
}
}

module.exports = Logger;
63 changes: 63 additions & 0 deletions src/RequestBuffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const uuid = require('uuid');
const stream = require('stream');
const { Readable: ReadableStream } = stream;

class RequestBuffer extends ReadableStream {
constructor(options = {}) {
options.objectMode = true;
super(options);

this.pending = {};
this.buffered = [];
}

add() {
const start = new Date();
const id = uuid.v1();
this.pending[id] = {
id,
start
};
return id;
}

end(id) {
const end = new Date();
const request = this.pending[id];

if (!request) {
this.emit('error', new Error(`Request id '${id}' not found`));
return;
}

request.end = end;
request.duration = end - request.start;

this._stream(request);

delete this.pending[id];
}

_stream(request) {
if (this.flowing) {
this.push(request);
} else {
this.buffered.push(request);
}
}

_read() {
if (this.flowing) {
return;
}

if (this.buffered.length) {
this.buffered.forEach(request => this.push(request));
}

this.buffered = undefined;
this.flowing = true;
}
}

module.exports = RequestBuffer;
27 changes: 20 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import util from 'util';
import colors, { fg, reset as colorEnd } from 'ansi-256-colors';
const util = require('util');
const colors = require('ansi-256-colors');
const { fg, reset: colorEnd } = colors;
const RequestBuffer = require('./RequestBuffer');
const Logger = require('./Logger');

const {
getRgb: rgb
Expand Down Expand Up @@ -59,7 +62,7 @@ function breakLines(message, messageWidth, map) {
}
}

export function colorize(color = 0) {
function colorize(color = 0) {
if (typeof color === 'number') {
if (color <= 0) {
return character => character;
Expand Down Expand Up @@ -103,7 +106,7 @@ function printToConsole({
context,
getWidth,
maxLocaleTimeLength,
reporter,
// reporter,
slim,
slot,
slots
Expand Down Expand Up @@ -183,13 +186,13 @@ function printToConsole({
${formatLine(line)}
`;

reporter.write(formattedLine + '\n');
// reporter.write(formattedLine + '\n');
});
};
};
}

export default function createLogger(options = {}) {
function createLogger(options = {}) {
const {
minSlots = 1,
getLevel = GET_LEVEL,
Expand Down Expand Up @@ -233,8 +236,14 @@ export default function createLogger(options = {}) {
maxLocaleTimeLength = 5;
}

const requestBuffer = new RequestBuffer();
const logStream = new Logger();

requestBuffer.pipe(logStream).pipe(reporter);

return async function logger(context, next) {
const start = new Date;
const requestId = requestBuffer.add();

let slot;
for (let i = 0; i < slots.length; i++) {
Expand All @@ -256,7 +265,7 @@ export default function createLogger(options = {}) {
context,
getWidth,
maxLocaleTimeLength: () => maxLocaleTimeLength,
reporter,
// reporter,
slim,
slot,
slots
Expand Down Expand Up @@ -327,6 +336,7 @@ export default function createLogger(options = {}) {
}

const end = new Date;
requestBuffer.end(requestId);
const duration = end - start;

let $duration;
Expand Down Expand Up @@ -391,3 +401,6 @@ export default function createLogger(options = {}) {
}
};
}

module.exports.default = createLogger;
module.exports.colorize = colorize;