Skip to content

Commit fbe375c

Browse files
committed
Implement logger change and test
1 parent b593391 commit fbe375c

File tree

3 files changed

+81
-19
lines changed

3 files changed

+81
-19
lines changed

src/vue-logger/interfaces/logger.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import {ILoggerOptions} from "./logger-options";
1+
import { ILoggerOptions } from "./logger-options";
22

33
export interface ILogger {
44
install(Vue: any, options: ILoggerOptions);
55

66
isValidOptions(options: ILoggerOptions, logLevels: string[]): boolean;
7+
8+
changeLogLevel(logLevel: string);
79
}

src/vue-logger/vue-logger.ts

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,43 @@
1-
import {LogLevels} from "./enum/log-levels";
2-
import {ILogger} from "./interfaces/logger";
3-
import {ILoggerOptions} from "./interfaces/logger-options";
1+
import { LogLevels } from "./enum/log-levels";
2+
import { ILogger } from "./interfaces/logger";
3+
import { ILoggerOptions } from "./interfaces/logger-options";
44

55
class VueLogger implements ILogger {
66

77
private errorMessage: string = "Provided options for vuejs-logger are not valid.";
88
private logLevels: string[] = Object.keys(LogLevels).map((l) => l.toLowerCase());
9+
private options: ILoggerOptions = this.getDefaultOptions();
10+
private vueInstance: any;
911

1012
public install(Vue: any, options: ILoggerOptions) {
11-
options = Object.assign(this.getDefaultOptions(), options);
13+
this.options = Object.assign(this.options, options);
1214

1315
if (this.isValidOptions(options, this.logLevels)) {
16+
this.vueInstance = Vue;
1417
Vue.$log = this.initLoggerInstance(options, this.logLevels);
1518
Vue.prototype.$log = Vue.$log;
1619
} else {
1720
throw new Error(this.errorMessage);
1821
}
1922
}
2023

24+
/**
25+
* Create a new logger with nother logLevel.
26+
* Exposed on the $log object to set the loglevel.
27+
* Implicitly enables the logger.
28+
*
29+
* @param aLogLevel the new loglevel
30+
*/
31+
private changeLogLevel(aLogLevel: string) {
32+
this.logLevels.forEach((logLevel) => {
33+
if (this.logLevels.indexOf(logLevel) >= this.logLevels.indexOf(aLogLevel)) {
34+
this.vueInstance.$log[logLevel] = this.createLoggerObject(this.options, logLevel);
35+
} else {
36+
this.vueInstance.$log[logLevel] = () => undefined;
37+
}
38+
});
39+
}
40+
2141
public isValidOptions(options: ILoggerOptions, logLevels: string[]): boolean {
2242
if (!(options.logLevel && typeof options.logLevel === "string" && logLevels.indexOf(options.logLevel) > -1)) {
2343
return false;
@@ -65,24 +85,29 @@ class VueLogger implements ILogger {
6585
private initLoggerInstance(options: ILoggerOptions, logLevels: string[]) {
6686
const logger = {};
6787
logLevels.forEach((logLevel) => {
68-
if (logLevels.indexOf(logLevel) >= logLevels.indexOf(options.logLevel) && options.isEnabled) {
69-
logger[logLevel] = (...args) => {
70-
const methodName = this.getMethodName();
71-
const methodNamePrefix = options.showMethodName ? methodName + ` ${options.separator} ` : "";
72-
const logLevelPrefix = options.showLogLevel ? logLevel + ` ${options.separator} ` : "";
73-
const formattedArguments = options.stringifyArguments ? args.map((a) => JSON.stringify(a)) : args;
74-
const logMessage = `${logLevelPrefix} ${methodNamePrefix}`;
75-
this.printLogMessage(logLevel, logMessage, options.showConsoleColors, formattedArguments);
76-
return `${logMessage} ${formattedArguments.toString()}`;
77-
};
78-
} else {
79-
logger[logLevel] = () => undefined;
80-
}
81-
},
88+
if (logLevels.indexOf(logLevel) >= logLevels.indexOf(options.logLevel) && options.isEnabled) {
89+
logger[logLevel] = this.createLoggerObject(options, logLevel);
90+
} else {
91+
logger[logLevel] = () => undefined;
92+
}
93+
},
8294
);
95+
logger['changeLogLevel'] = this.changeLogLevel.bind(this)
8396
return logger;
8497
}
8598

99+
private createLoggerObject(options: ILoggerOptions, logLevel: string) {
100+
return (...args) => {
101+
const methodName = this.getMethodName();
102+
const methodNamePrefix = options.showMethodName ? methodName + ` ${options.separator} ` : "";
103+
const logLevelPrefix = options.showLogLevel ? logLevel + ` ${options.separator} ` : "";
104+
const formattedArguments = options.stringifyArguments ? args.map((a) => JSON.stringify(a)) : args;
105+
const logMessage = `${logLevelPrefix} ${methodNamePrefix}`;
106+
this.printLogMessage(logLevel, logMessage, options.showConsoleColors, formattedArguments);
107+
return `${logMessage} ${formattedArguments.toString()}`;
108+
};
109+
}
110+
86111
private printLogMessage(logLevel: string, logMessage: string, showConsoleColors: boolean, formattedArguments: any) {
87112
if (showConsoleColors && (logLevel === "warn" || logLevel === "error" || logLevel === "fatal")) {
88113
console[logLevel === "fatal" ? "error" : logLevel](logMessage, ...formattedArguments);

tests/changelevel.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { notStrictEqual, strictEqual } from "assert";
2+
import chai from "chai";
3+
import Vue from "vue/dist/vue.min";
4+
import VueLogger from "../src";
5+
import { LogLevels } from "../src/vue-logger/enum/log-levels";
6+
import { ILoggerOptions } from "../src/vue-logger/interfaces/logger-options";
7+
const expect = chai.expect;
8+
9+
describe("changeLoglevel()", () => {
10+
11+
test("changeLogLevel('info') should set the loglevel to info after initialisation.", () => {
12+
const options: ILoggerOptions = {
13+
isEnabled: true,
14+
logLevel: LogLevels.ERROR,
15+
separator: "|",
16+
stringifyArguments: false,
17+
showConsoleColors: true,
18+
showLogLevel: false,
19+
showMethodName: false,
20+
};
21+
Vue.use(VueLogger, options);
22+
expect(Vue.$log).to.be.a("object");
23+
strictEqual(Vue.$log.debug("debug"), undefined);
24+
strictEqual(Vue.$log.info("info"), undefined);
25+
strictEqual(Vue.$log.warn("warn"), undefined);
26+
expect(Vue.$log.error("error")).to.exist;
27+
expect(Vue.$log.fatal("fatal")).to.exist;
28+
Vue.$log.changeLogLevel('info');
29+
strictEqual(Vue.$log.debug("debug"), undefined);
30+
expect(Vue.$log.error("info")).to.exist;
31+
expect(Vue.$log.error("warn")).to.exist;
32+
expect(Vue.$log.error("error")).to.exist;
33+
expect(Vue.$log.error("fatal")).to.exist;
34+
});
35+
});

0 commit comments

Comments
 (0)