mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[ADF-2477] message bus in log service (#3063)
* add message bus in log service to allow third pat integrations * fix test after review
This commit is contained in:
@@ -143,6 +143,18 @@ describe('Log Service', () => {
|
||||
expect(console.error).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('message Observable', (done) => {
|
||||
appConfigService.config['logLevel'] = 'trace';
|
||||
providesLogComponent = TestBed.createComponent(ProvidesLogComponent);
|
||||
|
||||
providesLogComponent.componentInstance.logService.onMessage.subscribe((message) => {
|
||||
done();
|
||||
});
|
||||
|
||||
providesLogComponent.componentInstance.log();
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@Component({
|
||||
|
@@ -15,16 +15,24 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/* tslint:disable:no-console */
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { AppConfigService } from '../app-config/app-config.service';
|
||||
import { logLevels, LogLevelsEnum } from '../models/log-levels.model';
|
||||
import { Subject } from 'rxjs/Subject';
|
||||
|
||||
@Injectable()
|
||||
export class LogService {
|
||||
|
||||
currentLogLevel: LogLevelsEnum = LogLevelsEnum.TRACE;
|
||||
|
||||
onMessage: Subject<any>;
|
||||
|
||||
constructor(appConfig: AppConfigService) {
|
||||
|
||||
this.onMessage = new Subject();
|
||||
|
||||
if (appConfig) {
|
||||
let configLevel: string = appConfig.get<string>('logLevel');
|
||||
|
||||
@@ -34,82 +42,79 @@ export class LogService {
|
||||
}
|
||||
}
|
||||
|
||||
get error(): (message?: any, ...optionalParams: any[]) => any {
|
||||
error(message?: any, ...optionalParams: any[]) {
|
||||
if (this.currentLogLevel >= LogLevelsEnum.ERROR) {
|
||||
return console.error.bind(console);
|
||||
|
||||
this.messageBus(message, 'ERROR');
|
||||
|
||||
console.error(message, ...optionalParams);
|
||||
}
|
||||
return (message?: any, ...optionalParams: any[]) => {
|
||||
};
|
||||
}
|
||||
|
||||
get debug(): (message?: any, ...optionalParams: any[]) => any {
|
||||
debug(message?: any, ...optionalParams: any[]) {
|
||||
if (this.currentLogLevel >= LogLevelsEnum.DEBUG) {
|
||||
return console.debug.bind(console);
|
||||
|
||||
this.messageBus(message, 'DEBUG');
|
||||
|
||||
console.debug(message, ...optionalParams);
|
||||
}
|
||||
return (message?: any, ...optionalParams: any[]) => {
|
||||
};
|
||||
}
|
||||
|
||||
get info(): (message?: any, ...optionalParams: any[]) => any {
|
||||
info(message?: any, ...optionalParams: any[]) {
|
||||
if (this.currentLogLevel >= LogLevelsEnum.INFO) {
|
||||
return console.info.bind(console);
|
||||
|
||||
this.messageBus(message, 'INFO');
|
||||
|
||||
console.info(message, ...optionalParams);
|
||||
}
|
||||
return (message?: any, ...optionalParams: any[]) => {
|
||||
};
|
||||
}
|
||||
|
||||
get log(): (message?: any, ...optionalParams: any[]) => any {
|
||||
log(message?: any, ...optionalParams: any[]) {
|
||||
if (this.currentLogLevel >= LogLevelsEnum.TRACE) {
|
||||
return console.log.bind(console);
|
||||
}
|
||||
|
||||
return (message?: any, ...optionalParams: any[]) => {
|
||||
};
|
||||
this.messageBus(message, 'LOG');
|
||||
|
||||
console.log(message, ...optionalParams);
|
||||
}
|
||||
}
|
||||
|
||||
get trace(): (message?: any, ...optionalParams: any[]) => any {
|
||||
trace(message?: any, ...optionalParams: any[]) {
|
||||
if (this.currentLogLevel >= LogLevelsEnum.TRACE) {
|
||||
return console.trace.bind(console);
|
||||
}
|
||||
return (message?: any, ...optionalParams: any[]) => {
|
||||
};
|
||||
|
||||
this.messageBus(message, 'TRACE');
|
||||
|
||||
console.trace(message, ...optionalParams);
|
||||
}
|
||||
}
|
||||
|
||||
get warn(): (message?: any, ...optionalParams: any[]) => any {
|
||||
warn(message?: any, ...optionalParams: any[]) {
|
||||
if (this.currentLogLevel >= LogLevelsEnum.WARN) {
|
||||
return console.warn.bind(console);
|
||||
}
|
||||
return (message?: any, ...optionalParams: any[]) => {
|
||||
};
|
||||
|
||||
this.messageBus(message, 'WARN');
|
||||
|
||||
console.warn(message, ...optionalParams);
|
||||
}
|
||||
}
|
||||
|
||||
get assert(): (message?: any, ...optionalParams: any[]) => any {
|
||||
assert(test?: boolean, message?: string, ...optionalParams: any[]) {
|
||||
if (this.currentLogLevel !== LogLevelsEnum.SILENT) {
|
||||
return console.assert.bind(console);
|
||||
}
|
||||
return (message?: any, ...optionalParams: any[]) => {
|
||||
};
|
||||
|
||||
this.messageBus(message, 'ASSERT');
|
||||
|
||||
console.assert(test, message, ...optionalParams);
|
||||
}
|
||||
}
|
||||
|
||||
get group(): (message?: any, ...optionalParams: any[]) => any {
|
||||
group(groupTitle?: string, ...optionalParams: any[]) {
|
||||
if (this.currentLogLevel !== LogLevelsEnum.SILENT) {
|
||||
return console.group.bind(console);
|
||||
console.group(groupTitle, ...optionalParams);
|
||||
}
|
||||
return (message?: any, ...optionalParams: any[]) => {
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
get groupEnd(): (message?: any, ...optionalParams: any[]) => any {
|
||||
groupEnd() {
|
||||
if (this.currentLogLevel !== LogLevelsEnum.SILENT) {
|
||||
return console.groupEnd.bind(console);
|
||||
console.groupEnd();
|
||||
}
|
||||
return (message?: any, ...optionalParams: any[]) => {
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
getCurrentLogLevel(level: string): LogLevelsEnum {
|
||||
@@ -119,4 +124,8 @@ export class LogService {
|
||||
|
||||
return referencedLevel ? referencedLevel.level : 5;
|
||||
}
|
||||
|
||||
messageBus(text: string, logLevel: string) {
|
||||
this.onMessage.next({ text: text, type: logLevel });
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user