|
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"; |
4 | 4 |
|
5 | 5 | class VueLogger implements ILogger {
|
6 | 6 |
|
7 | 7 | private errorMessage: string = "Provided options for vuejs-logger are not valid.";
|
8 | 8 | private logLevels: string[] = Object.keys(LogLevels).map((l) => l.toLowerCase());
|
| 9 | + private options: ILoggerOptions = this.getDefaultOptions(); |
| 10 | + private vueInstance: any; |
9 | 11 |
|
10 | 12 | public install(Vue: any, options: ILoggerOptions) {
|
11 |
| - options = Object.assign(this.getDefaultOptions(), options); |
| 13 | + this.options = Object.assign(this.options, options); |
12 | 14 |
|
13 | 15 | if (this.isValidOptions(options, this.logLevels)) {
|
| 16 | + this.vueInstance = Vue; |
14 | 17 | Vue.$log = this.initLoggerInstance(options, this.logLevels);
|
15 | 18 | Vue.prototype.$log = Vue.$log;
|
16 | 19 | } else {
|
17 | 20 | throw new Error(this.errorMessage);
|
18 | 21 | }
|
19 | 22 | }
|
20 | 23 |
|
| 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 | + |
21 | 41 | public isValidOptions(options: ILoggerOptions, logLevels: string[]): boolean {
|
22 | 42 | if (!(options.logLevel && typeof options.logLevel === "string" && logLevels.indexOf(options.logLevel) > -1)) {
|
23 | 43 | return false;
|
@@ -65,24 +85,29 @@ class VueLogger implements ILogger {
|
65 | 85 | private initLoggerInstance(options: ILoggerOptions, logLevels: string[]) {
|
66 | 86 | const logger = {};
|
67 | 87 | 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 | + }, |
82 | 94 | );
|
| 95 | + logger['changeLogLevel'] = this.changeLogLevel.bind(this) |
83 | 96 | return logger;
|
84 | 97 | }
|
85 | 98 |
|
| 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 | + |
86 | 111 | private printLogMessage(logLevel: string, logMessage: string, showConsoleColors: boolean, formattedArguments: any) {
|
87 | 112 | if (showConsoleColors && (logLevel === "warn" || logLevel === "error" || logLevel === "fatal")) {
|
88 | 113 | console[logLevel === "fatal" ? "error" : logLevel](logMessage, ...formattedArguments);
|
|
0 commit comments