Created
March 28, 2018 15:41
-
-
Save michaelilyin/d9cf904bf8f56e8f40c5d94c5eab4045 to your computer and use it in GitHub Desktop.
Angular logging service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {NgModule} from '@angular/core'; | |
import {AppComponent} from './app.component'; | |
import {DEBUG, INFO, LoggingConfiguration} from './shared/utils/logging.service'; | |
@NgModule({ | |
declarations: [ | |
AppComponent | |
], | |
imports: [ | |
], | |
providers: [ | |
{ | |
provide: LoggingConfiguration, | |
useValue: new LoggingConfiguration(environment.production ? INFO : DEBUG) | |
} | |
], | |
bootstrap: [AppComponent] | |
}) | |
export class AppModule { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Injectable, Provider} from '@angular/core'; | |
export const TRACE = 10; | |
export const DEBUG = 20; | |
export const INFO = 30; | |
export const WARN = 40; | |
export const ERROR = 50; | |
export class LoggingConfiguration { | |
public constructor(public readonly level: number) { | |
} | |
} | |
export abstract class LoggingService { | |
abstract tracef(template: string, ...values: any[]); | |
abstract debugf(template: string, ...values: any[]); | |
abstract infof(template: string, ...values: any[]); | |
abstract warnf(template: string, ...values: any[]); | |
abstract errorf(template: string, ...values: any[]); | |
abstract trace(template: string, ...values: any[]); | |
abstract debug(template: string, ...values: any[]); | |
abstract info(template: string, ...values: any[]); | |
abstract warn(template: string, ...values: any[]); | |
abstract error(template: string, ...values: any[]); | |
} | |
@Injectable() | |
export class LoggingServiceImpl extends LoggingService { | |
private level: number; | |
private loggers = { | |
10: console.log, | |
20: console.log, | |
30: console.info, | |
40: console.warn, | |
50: console.error | |
}; | |
constructor(config: LoggingConfiguration) { | |
super(); | |
this.level = config.level; | |
} | |
tracef(template: string, ...values) { | |
this.logf(TRACE, template, ...values); | |
} | |
debugf(template: string, ...values) { | |
this.logf(DEBUG, template, ...values); | |
} | |
infof(template: string, ...values) { | |
this.logf(INFO, template, ...values); | |
} | |
warnf(template: string, ...values) { | |
this.logf(WARN, template, ...values); | |
} | |
errorf(template: string, ...values) { | |
this.logf(ERROR, template, ...values); | |
} | |
trace(message: string, ...values) { | |
this.log(TRACE, message, ...values); | |
} | |
debug(message: string, ...values) { | |
this.log(DEBUG, message, ...values); | |
} | |
info(message: string, ...values) { | |
this.log(INFO, message, ...values); | |
} | |
warn(message: string, ...values) { | |
this.log(WARN, message, ...values); | |
} | |
error(message: string, ...values) { | |
this.log(ERROR, message, ...values); | |
} | |
private logf(level: number, template: string, ...values) { | |
if (level >= this.level) { | |
const fun = this.loggers[level]; | |
const str = this.format(template, ...values); | |
fun.call(console, str); | |
} | |
} | |
private log(level: number, message: string, ...values) { | |
if (level >= this.level) { | |
const fun = this.loggers[level]; | |
fun.call(console, message, ...values); | |
} | |
} | |
private format(template: string, ...values) { | |
return template.replace(/([^{]|^){(\d+)}([^{]|$)/g, (match, _, number) => { | |
const value = values[number]; | |
const stringed = typeof value === 'object' ? JSON.stringify(value) : value; | |
const result = typeof value !== 'undefined' ? stringed : match; | |
const first = match[0]; | |
const last = match[match.length - 1]; | |
return `${first === '{' ? '' : first}${result}${last === '}' ? '' : last}`; | |
}); | |
} | |
} | |
export const LoggingServiceProvider: Provider = { | |
provide: LoggingService, | |
useClass: LoggingServiceImpl | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment