Skip to content
This repository has been archived by the owner on Jan 14, 2022. It is now read-only.

Improved logger - Closes #830 #831

Merged
merged 9 commits into from
Nov 27, 2018
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
4 changes: 2 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ app.use((req, res, next) => {
`connect-src 'self' ${connectSrc};`,
`img-src 'self' https:;`,
`style-src 'self' 'unsafe-inline' https://fonts.googleapis.com;`,
`script-src 'self' 'sha256-L6JyfNh6FtKC6umsFxtawnD4dtWi8szFRQZU0tVgsQk=' 'unsafe-eval' www.googletagmanager.com www.google-analytics.com;`,
`font-src 'self' https://fonts.gstatic.com`,
`script-src 'self' 'unsafe-eval' 'unsafe-inline' https://tagmanager.google.com/ https://www.googletagmanager.com/ https://www.google-analytics.com/ http://*.cloudfront.net/ https://*.ipify.org/ https://*.crazyegg.com;`,
`font-src 'self' https://fonts.gstatic.com data:`,
].join(' ');
/* eslint-enable */

Expand Down
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ config.cacheTTL = 20;
// Collect logs (true - enabled, false - disabled)
config.log.enabled = true;
// Output for logs - can be device file or ordinary path
config.log.file = './logs/explorer.log';
config.log.output = ['/dev/stdout', './logs/explorer.log'];
// Log level - (trace, debug, info, warn, error)
config.log.level = 'info';

Expand Down
4 changes: 3 additions & 1 deletion known.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@
"2797034409072178585L" : { "owner" : "BitBay.net", "description" : "Withdrawal Wallet" },
"15610359283786884938L" : { "owner" : "Binance", "description" : "Hot Wallet" },
"14367838290034827614L" : { "owner" : "Coincheck.com", "description" : "Old Wallet" },
"16293716040102736949L" : { "owner" : "Coincheck.com", "description" : "Main Wallet"}
"16293716040102736949L" : { "owner" : "Coincheck.com", "description" : "Main Wallet"},
"9094944264474023843L" : { "owner" : "ICONOMI", "description" : "ICO Wallet"},
"12167418142926850031L" : { "owner" : "Binance", "description" : "Main Wallet"}
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lisk-explorer",
"version": "2.2.0-alpha",
"version": "2.1.6",
"description": "Lisk blockchain explorer",
"keywords": [
"lisk",
Expand Down
39 changes: 26 additions & 13 deletions utils/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,22 @@ const flatstr = require('flatstr');
const newConsole = require('console').Console;
const config = require('../config');

const output = fs.createWriteStream(config.log.file, { flags: 'a' });
const myConsole = new newConsole(output, output);
const defaultOutput = '/dev/stdout';

if (!Array.isArray(config.log.output)) {
if (typeof config.log.output === 'string') {
config.log.output = [config.log.output];
} else if (typeof config.log.file === 'string') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like this config file "backward compatibility". ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The backward compatibility is not the main reason for that. Potential users might still want to use a single output the most intuitive way possible.

config.log.output = [config.log.file];
} else {
config.log.output = [defaultOutput];
}
}

const logOutputs = config.log.output.map((outputPath) => {
const fileOutput = fs.createWriteStream(outputPath, { flags: 'a' });
return new newConsole(fileOutput, fileOutput);
});

const levels = {
trace: 0,
Expand All @@ -35,25 +49,24 @@ const logger = {};
logger.doLog = function doLog(level, msg, extra) {
if (config.log.enabled) {
const timestamp = Date.now();

// const stringMsg = typeof msg === 'string' ? msg : JSON.stringify(msg);
// const parsedMsg = stringMsg.replace(/(\r\n|\n|\r)/gm, ' ');
let outputString;

if (extra) {
const stringExtra = typeof extra === 'string' ? extra : JSON.stringify(extra);
const parsedExtra = stringExtra.replace(/(\r\n|\n|\r)/gm, ' ');
myConsole.log(flatstr(safeStringify({
level,
timestamp,
message: `${msg} ${parsedExtra}`,
})));
outputString = `${msg} ${parsedExtra}`;
} else {
myConsole.log(flatstr(safeStringify({
outputString = msg;
}

logOutputs.map((logOutput) => {
logOutput.log(flatstr(safeStringify({
level,
timestamp,
message: msg,
message: outputString,
})));
}
return logOutput;
});
}
};

Expand Down