Skip to content

Log formatting with a template literal #41

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
18 changes: 7 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,11 @@ Below you can find an example of how to use vuejs-logger :

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| isEnabled | false | Boolean | true | Enables the vuejs-logger plugin, useful toggle for production/development. |
| logLevel | false | String | 'debug' | Choose between ['debug', 'info', 'warn', 'error', 'fatal']. Read [production tips](#production-tips). |
| stringifyArguments | false | Boolean | false | If true, all input will go through JSON.stringify(). Useful when printing reactive properties.|
| showLogLevel | false | Boolean | false | If true, the loglevel will be shown. |
| showMethodName | false | Boolean | false | If true, the method name of the parent function will be shown in the console. |
| separator | false | String | ' l ' | The seperator between parts of the output ( see [screenshot](#screenshot). |
| showConsoleColors | false | Boolean | false | If true, enables console.warn, console.fatal, console.error for corresponding loglevels. |
| isEnabled | false | Boolean | `true ` | Enables the vuejs-logger plugin, useful toggle for production/development. |
| logLevel | false | String | `"debug"` | Choose between ['debug', 'info', 'warn', 'error', 'fatal']. Read [production tips](#production-tips). |
| stringifyArguments | false | Boolean | `false` | If true, all input will go through JSON.stringify(). Useful when printing reactive properties.|
| showConsoleColors | false | Boolean | `false` | If true, enables console.warn, console.fatal, console.error for corresponding loglevels. |
| format | false | String | <pre lang="js">"${message} ${args.join(' \| ')}"</pre> | Log message format template, using the template string syntax without backticks. The arguments `message`, `methodName`, `level` and `args` can be used. |

#### Code example

Expand All @@ -75,10 +73,8 @@ const options = {
isEnabled: true,
logLevel : isProduction ? 'error' : 'debug',
stringifyArguments : false,
showLogLevel : true,
showMethodName : true,
separator: '|',
showConsoleColors: true
showConsoleColors: true,
format: "${logLevel} | ${methodName} | ${message} ${args.join(' ')}",
};

Vue.use(VueLogger, options);
Expand Down
9 changes: 9 additions & 0 deletions src/interfaces/logger-format-params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {LogLevels} from "../enum/log-levels";


export interface ILoggerFormatParams {
message: string;
methodName: string;
level: LogLevels;
args: any[];
}
4 changes: 1 addition & 3 deletions src/interfaces/logger-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import {LogLevels} from "../enum/log-levels";
export interface ILoggerOptions {
isEnabled: boolean;
logLevel: LogLevels;
separator: string;
showConsoleColors: boolean;
showLogLevel: boolean;
showMethodName: boolean;
stringifyArguments: boolean;
format: string;
}
33 changes: 17 additions & 16 deletions src/vue-logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {LogLevels} from "./enum/log-levels";
import {ILogger} from "./interfaces/logger";
import {ILoggerFormatParams} from "./interfaces/logger-format-params";
import {ILoggerOptions} from "./interfaces/logger-options";

class VueLogger implements ILogger {
Expand All @@ -25,19 +26,13 @@ class VueLogger implements ILogger {
if (options.stringifyArguments && typeof options.stringifyArguments !== "boolean") {
return false;
}
if (options.showLogLevel && typeof options.showLogLevel !== "boolean") {
return false;
}
if (options.showConsoleColors && typeof options.showConsoleColors !== "boolean") {
return false;
}
if (options.separator && (typeof options.separator !== "string" || (typeof options.separator === "string" && options.separator.length > 3))) {
return false;
}
if (typeof options.isEnabled !== "boolean") {
if (options.format && typeof options.format !== "string") {
return false;
}
return !(options.showMethodName && typeof options.showMethodName !== "boolean");
return typeof options.isEnabled === "boolean";
}

private getMethodName(): string {
Expand Down Expand Up @@ -66,12 +61,15 @@ class VueLogger implements ILogger {
const logger = {};
logLevels.forEach((logLevel) => {
if (logLevels.indexOf(logLevel) >= logLevels.indexOf(options.logLevel) && options.isEnabled) {
logger[logLevel] = (...args) => {
const methodName = this.getMethodName();
const methodNamePrefix = options.showMethodName ? methodName + ` ${options.separator} ` : "";
const logLevelPrefix = options.showLogLevel ? logLevel + ` ${options.separator} ` : "";
logger[logLevel] = (message, ...args) => {
const formattedArguments = options.stringifyArguments ? args.map((a) => JSON.stringify(a)) : args;
const logMessage = `${logLevelPrefix} ${methodNamePrefix}`;
const formatParams: ILoggerFormatParams = {
message,
methodName: this.getMethodName(),
level: logLevel as LogLevels,
args: formattedArguments,
}
const logMessage = this.interpolateTemplateString(options.format, formatParams);
this.printLogMessage(logLevel, logMessage, options.showConsoleColors, formattedArguments);
return `${logMessage} ${formattedArguments.toString()}`;
};
Expand All @@ -83,6 +81,11 @@ class VueLogger implements ILogger {
return logger;
}

private interpolateTemplateString(templateString: string, params: {[key: string]: any}): string {
const names = Object.keys(params)
return new Function(`const \{${names}\} = this; return \`${templateString}\`;`).call(params);
}

private printLogMessage(logLevel: string, logMessage: string, showConsoleColors: boolean, formattedArguments: any) {
if (showConsoleColors && (logLevel === "warn" || logLevel === "error" || logLevel === "fatal")) {
console[logLevel === "fatal" ? "error" : logLevel](logMessage, ...formattedArguments);
Expand All @@ -95,11 +98,9 @@ class VueLogger implements ILogger {
return {
isEnabled: true,
logLevel: LogLevels.DEBUG,
separator: "|",
showConsoleColors: false,
showLogLevel: false,
showMethodName: false,
stringifyArguments: false,
format: "${message} ${args.join(' | ')}",
};
}
}
Expand Down
67 changes: 7 additions & 60 deletions tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ describe("isValidOptions()", () => {
isEnabled: true,
logLevel: "debug",
stringifyArguments: false,
showLogLevel: false,
showMethodName: true,
separator: "|",
showConsoleColors: false,
format: "${methodName} ${message} ${args.join(' | ')}",
} as any, logLevels);
strictEqual(input, true);
});
Expand All @@ -25,111 +23,60 @@ describe("isValidOptions()", () => {
isEnabled: true,
logLevel: "debug",
stringifyArguments: false,
showLogLevel: false,
showMethodName: true,
separator: "|||||",
showConsoleColors: false,
} as any, logLevels), false);

strictEqual(VueLogger.isValidOptions({
isEnabled: true,
logLevel: "debug",
stringifyArguments: false,
showLogLevel: false,
showMethodName: true,
separator: "|",
showConsoleColors: "FOO",
format: "${message} ${args.join(' | ')}",
} as any, logLevels), false);

strictEqual(VueLogger.isValidOptions({
isEnabled: true,
logLevel: "debug",
stringifyArguments: false,
showLogLevel: false,
showMethodName: "TEST",
separator: "|",
showConsoleColors: false,
format: 6,
} as any, logLevels), false);

strictEqual(VueLogger.isValidOptions({
isEnabled: true,
logLevel: "debug",
stringifyArguments: "TEST",
showLogLevel: false,
showMethodName: false,
separator: "|",
showConsoleColors: false,
} as any, logLevels), false);

strictEqual(VueLogger.isValidOptions({
isEnabled: true,
logLevel: "debug",
stringifyArguments: false,
showLogLevel: "TEST",
showMethodName: false,
separator: "|",
showConsoleColors: false,
format: "${message} ${args.join(' | ')}",
} as any, logLevels), false);

strictEqual(VueLogger.isValidOptions({
isEnabled: true,
logLevel: "TEST",
stringifyArguments: false,
showLogLevel: false,
showMethodName: false,
separator: "|",
showConsoleColors: false,
format: "${message} ${args.join(' | ')}",
} as any, logLevels), false);

strictEqual(VueLogger.isValidOptions({
logLevel: "debug",
isEnabled: true,
} as any, logLevels), true);

strictEqual(VueLogger.isValidOptions({
isEnabled: "",
logLevel: "debug",
separator: "1234",
} as any, logLevels), false);

strictEqual(VueLogger.isValidOptions({
isEnabled: true,
logLevel: "debug",
stringifyArguments: false,
showLogLevel: false,
showMethodName: false,
separator: "|",
showConsoleColors: false,
} as any, logLevels), true);

strictEqual(VueLogger.isValidOptions({
isEnabled: false,
logLevel: "debug",
stringifyArguments: false,
showLogLevel: false,
showMethodName: false,
separator: "|",
showConsoleColors: false,
} as any, logLevels), true);

strictEqual(VueLogger.isValidOptions({
isEnabled: "",
logLevel: "debug",
stringifyArguments: false,
showLogLevel: false,
showMethodName: false,
separator: "|",
showConsoleColors: false,
format: "${message} ${args.join(' | ')}",
} as any, logLevels), false);

strictEqual(VueLogger.isValidOptions({
isEnabled: null,
logLevel: "debug",
stringifyArguments: false,
showLogLevel: false,
showMethodName: false,
separator: "|",
showConsoleColors: false,
format: "${message} ${args.join(' | ')}",
} as any, logLevels), false);
});
});
8 changes: 2 additions & 6 deletions tests/install.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ describe("vue-logger.ts", () => {
const options: ILoggerOptions = {
isEnabled: true,
logLevel: LogLevels.FATAL,
separator: "|",
stringifyArguments: false,
showConsoleColors: true,
showLogLevel: false,
showMethodName: false,
format: "${message} ${args.join(' | ')}",
};
Vue.use(VueLogger, options);
expect(Vue.$log).to.be.a("object");
Expand All @@ -31,11 +29,9 @@ describe("vue-logger.ts", () => {
const options: any = {
isEnabled: true,
logLevel: LogLevels.DEBUG,
separator: "|",
stringifyArguments: false,
showConsoleColors: true,
showLogLevel: false,
showMethodName: "wrong value for test.",
format: true,
};
expect(() => {
VueLogger.install(Vue, options);
Expand Down
4 changes: 1 addition & 3 deletions tests/output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ describe("output", () => {
isEnabled: true,
logLevel: LogLevels.DEBUG,
stringifyArguments: false,
showLogLevel: false,
showMethodName: true,
separator: "|",
showConsoleColors: false,
format: "${methodName} ${message} ${args.join(' | ')}",
} as ILoggerOptions;

Vue.use(VueLogger, options);
Expand Down