Skip to content

Commit

Permalink
fix(output): Improve the output for both browser and node backends wi…
Browse files Browse the repository at this point in the history
…th errors
  • Loading branch information
martin31821 committed Mar 10, 2020
1 parent 48617c8 commit 6a1cd16
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 10 deletions.
2 changes: 2 additions & 0 deletions example/console-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ Logger.withFields({
* Annotate a single field and log a message with custom log level
*/
Logger.withField('answer', 42).log('customlevel', 'custom message')

Logger.withField('err', new Error('test error')).debug('test');
6 changes: 3 additions & 3 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"chalk": "^3.0.0"
},
"devDependencies": {
"@types/node": "^13.9.0",
"@typescript-eslint/eslint-plugin": "^2.21.0",
"@typescript-eslint/eslint-plugin-tslint": "^2.21.0",
"@typescript-eslint/parser": "^2.21.0",
Expand Down
9 changes: 5 additions & 4 deletions src/backends/console-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ export class ConsoleBackend implements ILogBackend {
// Human readable output supports coloring.
const shouldAddColor = this.options.color && this.options.colorStyleMap[level];
const coloredLevel = shouldAddColor ? `%c${level}%c` : level;
const contextFormat = Object.keys(context).map(key => `${key}=${context[key]}`).join(' ');
const message = `[${ts.toISOString()}] ${coloredLevel} ${msg} ${contextFormat}`;
const contextEntries = Object.keys(context).map(key => [key, '=', context[key]]);
const contextFormat = ([] as any[]).concat(...contextEntries);
const message = [`[${ts.toISOString()}] ${coloredLevel} ${msg}`, ...contextFormat];
if (shouldAddColor) {
console.log(message, this.options.colorStyleMap[level], 'color: initial');
console.log(...message, this.options.colorStyleMap[level], 'color: initial');
} else {
console.log(message);
console.log(...message);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/backends/console-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/* eslint-disable no-console */
import { ILogBackend, LogVariables } from '../Backend';
import * as chalk from 'chalk';
import { format } from 'util';

const DEFAULT_COLOR_MAP: ColorStyleMap = {
trace: chalk.gray,
Expand Down Expand Up @@ -45,8 +46,8 @@ export class ConsoleBackend implements ILogBackend {
// Human readable output supports coloring.
const shouldAddColor = this.options.color && this.options.colorStyleMap[level];
const coloredLevel = shouldAddColor ? this.options.colorStyleMap[level](level) : level;
const contextFormat = Object.keys(context).map(key => `${key}=${context[key]}`).join(' ');
const message = `[${ts.toISOString()}] ${coloredLevel} ${msg} ${contextFormat}`;
const contextFormat = Object.keys(context).map(key => `${key}=${format(context[key])}`).join(' ');
const message = `[${ts.toISOString()}] ${coloredLevel} ${format(msg)} ${contextFormat}`;
console.log(message);
}
}
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"lib": ["es2015", "es2017", "dom"],
"declaration": false,
"strict": true,
"types": [],
"types": [
"node"
],
"typeRoots": [
"node_modules/@types"
],
Expand Down

0 comments on commit 6a1cd16

Please sign in to comment.