Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pretty default logger #244

Merged
merged 3 commits into from
Oct 30, 2019
Merged
Changes from 2 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
25 changes: 24 additions & 1 deletion packages/cubejs-server-core/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ const checkEnvForPlaceholders = () => {
}
};

const devLogger = (type, message, error) => {
const colors = {
red: '31', // ERROR
green: '32', // INFO
blue: '34', // TRACE
}
const withColor = (str, color = colors.green) => `\u001b[${color}m${str}\u001b[0m`;
const format = json => JSON.stringify(json, null, 2);

if ([ // INFO list
paveltiunov marked this conversation as resolved.
Show resolved Hide resolved
'Performing query',
'Performing query completed',
].includes(type)) {
if (error) console.log(`${withColor(type, colors.blue)}: ${error}`);
paveltiunov marked this conversation as resolved.
Show resolved Hide resolved
else console.log(`${withColor(type, colors.blue)}: ${format(message)}`);
return;
}
paveltiunov marked this conversation as resolved.
Show resolved Hide resolved
// ERROR
if (error) return console.log(`${withColor(type, colors.red)}: ${error}`);
// TRACE
console.log(`${withColor(type, colors.green)}: ${format(message)}`);
}

class CubejsServerCore {
constructor(options) {
options = options || {};
Expand Down Expand Up @@ -68,7 +91,7 @@ class CubejsServerCore {
this.logger = options.logger || ((msg, params) => {
const { error, ...restParams } = params;
if (process.env.NODE_ENV !== 'production') {
console.log(`${msg}: ${JSON.stringify(restParams)}${error ? `\n${error}` : ''}`);
devLogger(msg, restParams, error);
} else {
console.log(JSON.stringify({ message: msg, ...params }));
}
Expand Down