[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:
Eugenio Romano
2018-03-13 15:48:19 +00:00
committed by GitHub
parent 6c56d164ec
commit 27de866193
3 changed files with 96 additions and 43 deletions

View File

@@ -53,4 +53,36 @@ If you want set for example the log to warning:
{ {
"logLevel": "WARN" "logLevel": "WARN"
} }
``` ```
### Log message bus
The logservice provide also an Observable ***onMessage*** where you can subscribe and recive all the logs:
The messagge object recived form the bus is composed:
```ts
{
text: "Message log text"
type: "ERROR|DEBUG|INFO|LOG|TRACE|WARN|ASSERT"
}
```
## Usage
```ts
import { LogService } from '@alfresco/adf-core';
@Component({...})
export class AppComponent {
constructor(logService: LogService, myIntegrationService: MyIntegrationService)) {
logService.onMessage.subscribe((message) => {
myIntegrationService.send(message.text,message.type);
});
}
}
```

View File

@@ -143,6 +143,18 @@ describe('Log Service', () => {
expect(console.error).toHaveBeenCalled(); 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({ @Component({

View File

@@ -15,16 +15,24 @@
* limitations under the License. * limitations under the License.
*/ */
/* tslint:disable:no-console */
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { AppConfigService } from '../app-config/app-config.service'; import { AppConfigService } from '../app-config/app-config.service';
import { logLevels, LogLevelsEnum } from '../models/log-levels.model'; import { logLevels, LogLevelsEnum } from '../models/log-levels.model';
import { Subject } from 'rxjs/Subject';
@Injectable() @Injectable()
export class LogService { export class LogService {
currentLogLevel: LogLevelsEnum = LogLevelsEnum.TRACE; currentLogLevel: LogLevelsEnum = LogLevelsEnum.TRACE;
onMessage: Subject<any>;
constructor(appConfig: AppConfigService) { constructor(appConfig: AppConfigService) {
this.onMessage = new Subject();
if (appConfig) { if (appConfig) {
let configLevel: string = appConfig.get<string>('logLevel'); 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) { 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) { 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) { 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) { 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) { 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) { 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) { 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) { 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) { if (this.currentLogLevel !== LogLevelsEnum.SILENT) {
return console.groupEnd.bind(console); console.groupEnd();
} }
return (message?: any, ...optionalParams: any[]) => {
};
} }
getCurrentLogLevel(level: string): LogLevelsEnum { getCurrentLogLevel(level: string): LogLevelsEnum {
@@ -119,4 +124,8 @@ export class LogService {
return referencedLevel ? referencedLevel.level : 5; return referencedLevel ? referencedLevel.level : 5;
} }
messageBus(text: string, logLevel: string) {
this.onMessage.next({ text: text, type: logLevel });
}
} }