mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-02-21 07:29:37 +01:00
fix: bugs related to logging with type json (#893)
This commit cleans up some logging infra and ensures that the msg in the json log are the same as what you get when you use pretty. It also removes some confusing nulls that appear in the logs.
This commit is contained in:
parent
d2a715dcbc
commit
cd231badb7
@ -223,7 +223,7 @@ export function log(req: $RequestExtend, res: $ResponseExtend, next: $NextFuncti
|
|||||||
url: req.url,
|
url: req.url,
|
||||||
},
|
},
|
||||||
level: 35, // http
|
level: 35, // http
|
||||||
user: req.remote_user && req.remote_user.name,
|
user: req.remote_user && req.remote_user.name || null,
|
||||||
remoteIP,
|
remoteIP,
|
||||||
status: res.statusCode,
|
status: res.statusCode,
|
||||||
error: res._verdaccio_error,
|
error: res._verdaccio_error,
|
||||||
|
@ -37,41 +37,45 @@ function setup(logs) {
|
|||||||
const stream = new Stream();
|
const stream = new Stream();
|
||||||
stream.writable = true;
|
stream.writable = true;
|
||||||
|
|
||||||
if (target.type === 'stdout' || target.type === 'stderr') {
|
let dest;
|
||||||
// destination stream
|
let destIsTTY = false;
|
||||||
const dest = target.type === 'stdout' ? process.stdout : process.stderr;
|
const prettyPrint = (obj) => print(obj.level, obj.msg, obj, destIsTTY) + '\n';
|
||||||
|
const prettyTimestampedPrint = (obj) => obj.time.toISOString() + print(obj.level, obj.msg, obj, destIsTTY) + '\n';
|
||||||
|
const jsonPrint = (obj) => {
|
||||||
|
const msg = fillInMsgTemplate(obj.msg, obj, destIsTTY);
|
||||||
|
return JSON.stringify({...obj, msg}, Logger.safeCycles()) + '\n';
|
||||||
|
};
|
||||||
|
|
||||||
if (target.format === 'pretty') {
|
if (target.type === 'file') {
|
||||||
// making fake stream for prettypritting
|
// destination stream
|
||||||
stream.write = function(obj) {
|
dest = require('fs').createWriteStream(target.path, {flags: 'a', encoding: 'utf8'});
|
||||||
dest.write(print(obj.level, obj.msg, obj, dest.isTTY) + '\n');
|
|
||||||
};
|
|
||||||
} else if (target.format === 'pretty-timestamped') {
|
|
||||||
// making fake stream for prettypritting
|
|
||||||
stream.write = function(obj) {
|
|
||||||
dest.write(obj.time.toISOString() + print(obj.level, obj.msg, obj, dest.isTTY) + '\n');
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
stream.write = function(obj) {
|
|
||||||
dest.write(JSON.stringify(obj, Logger.safeCycles()) + '\n');
|
|
||||||
};
|
|
||||||
}
|
|
||||||
} else if (target.type === 'file') {
|
|
||||||
const dest = require('fs').createWriteStream(target.path, {flags: 'a', encoding: 'utf8'});
|
|
||||||
dest.on('error', function(err) {
|
dest.on('error', function(err) {
|
||||||
Logger.emit('error', err);
|
Logger.emit('error', err);
|
||||||
});
|
});
|
||||||
stream.write = function(obj) {
|
} else if (target.type === 'stdout' || target.type === 'stderr') {
|
||||||
if (target.format === 'pretty') {
|
dest = target.type === 'stdout' ? process.stdout : process.stderr;
|
||||||
dest.write(print(obj.level, obj.msg, obj, false) + '\n');
|
destIsTTY = dest.isTTY;
|
||||||
} else {
|
|
||||||
dest.write(JSON.stringify(obj, Logger.safeCycles()) + '\n');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
} else {
|
} else {
|
||||||
throw Error('wrong target type for a log');
|
throw Error('wrong target type for a log');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (target.format === 'pretty') {
|
||||||
|
// making fake stream for prettypritting
|
||||||
|
stream.write = (obj) => {
|
||||||
|
dest.write(prettyPrint(obj));
|
||||||
|
};
|
||||||
|
} else if (target.format === 'pretty-timestamped') {
|
||||||
|
// making fake stream for prettypritting
|
||||||
|
stream.write = (obj) => {
|
||||||
|
dest.write(prettyTimestampedPrint(obj));
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
stream.write = (obj) => {
|
||||||
|
dest.write(jsonPrint(obj));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if (target.level === 'http') target.level = 35;
|
if (target.level === 'http') target.level = 35;
|
||||||
streams.push({
|
streams.push({
|
||||||
type: 'raw',
|
type: 'raw',
|
||||||
@ -132,19 +136,8 @@ function pad(str) {
|
|||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
function fillInMsgTemplate(msg, obj, colors) {
|
||||||
* Apply colors to a string based on level parameters.
|
return msg.replace(/@{(!?[$A-Za-z_][$0-9A-Za-z\._]*)}/g, (_, name) => {
|
||||||
* @param {*} type
|
|
||||||
* @param {*} msg
|
|
||||||
* @param {*} obj
|
|
||||||
* @param {*} colors
|
|
||||||
* @return {String}
|
|
||||||
*/
|
|
||||||
function print(type, msg, obj, colors) {
|
|
||||||
if (typeof type === 'number') {
|
|
||||||
type = getlvl(type);
|
|
||||||
}
|
|
||||||
let finalmsg = msg.replace(/@{(!?[$A-Za-z_][$0-9A-Za-z\._]*)}/g, function(_, name) {
|
|
||||||
let str = obj;
|
let str = obj;
|
||||||
let is_error;
|
let is_error;
|
||||||
if (name[0] === '!') {
|
if (name[0] === '!') {
|
||||||
@ -174,6 +167,21 @@ function print(type, msg, obj, colors) {
|
|||||||
return require('util').inspect(str, null, null, colors);
|
return require('util').inspect(str, null, null, colors);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Apply colors to a string based on level parameters.
|
||||||
|
* @param {*} type
|
||||||
|
* @param {*} msg
|
||||||
|
* @param {*} obj
|
||||||
|
* @param {*} colors
|
||||||
|
* @return {String}
|
||||||
|
*/
|
||||||
|
function print(type, msg, obj, colors) {
|
||||||
|
if (typeof type === 'number') {
|
||||||
|
type = getlvl(type);
|
||||||
|
}
|
||||||
|
const finalmsg = fillInMsgTemplate(msg, obj, colors);
|
||||||
|
|
||||||
const subsystems = [{
|
const subsystems = [{
|
||||||
in: chalk.green('<--'),
|
in: chalk.green('<--'),
|
||||||
|
@ -181,7 +181,7 @@ class ProxyStorage implements IProxy {
|
|||||||
? ', error: @{!error}'
|
? ', error: @{!error}'
|
||||||
: ', bytes: @{bytes.in}/@{bytes.out}';
|
: ', bytes: @{bytes.in}/@{bytes.out}';
|
||||||
self.logger.warn({
|
self.logger.warn({
|
||||||
err: err,
|
err: err || undefined, // if error is null/false change this to undefined so it wont log
|
||||||
request: {method: method, url: uri},
|
request: {method: method, url: uri},
|
||||||
level: 35, // http
|
level: 35, // http
|
||||||
status: res != null ? res.statusCode : 'ERR',
|
status: res != null ? res.statusCode : 'ERR',
|
||||||
|
Loading…
Reference in New Issue
Block a user