/*-------------------------------------------------------------------------------------------------------------- * Copyright (c) insite-gmbh. All rights reserved. * Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------------------------*/ import { Observable } from 'rxjs/Rx'; import { Injectable } from '@angular/core'; import { Http, Headers, Response } from '@angular/http'; import { InaxConfiguration, ProfileService, Result} from '../../../@inax/common'; import { ToastsManager, Toast } from 'ng2-toastr/ng2-toastr'; @Injectable() export class InaxLoggerService { constructor(private _http: Http, private _configuration: InaxConfiguration, private _profileService: ProfileService, private _toastr: ToastsManager) { } /*********************************************************************** * WEB API * ********************************************************************/ public logServerInfo(message: string): Observable { let accessUrl = this._configuration.buildRestUrl("Logger","Info"); let headers = this._profileService.addAuthorization(new Headers()); return this._http.post(accessUrl, message, { headers }).map(res => res.json()); } public logServerDebug(message: string): Observable { let accessUrl = this._configuration.buildRestUrl("Logger","Debug"); let headers = this._profileService.addAuthorization(new Headers()); return this._http.post(accessUrl, message, { headers }).map(res => res.json()); } public logServerError(message: string): Observable { let accessUrl = this._configuration.buildRestUrl("Logger","Error"); let headers = this._profileService.addAuthorization(new Headers()); return this._http.post(accessUrl, message, { headers }).map(res => res.json()); } public logServerFatal(message: string): Observable { let accessUrl = this._configuration.buildRestUrl("Logger","Fatal"); let headers = this._profileService.addAuthorization(new Headers()); return this._http.post(accessUrl, message, { headers }).map(res => res.json()); } public logServerWarn(message: string): Observable { let accessUrl = this._configuration.buildRestUrl("Logger","Warn"); let headers = this._profileService.addAuthorization(new Headers()); return this._http.post(accessUrl, message, { headers }).map(res => res.json()); } public logInfo(message: string, showToast: boolean = false, data: any = null, source: string = "") { this.log(message, data, source, showToast, 'info'); } public logWarning(message: string, showToast: boolean = false, data: any = null, source: string = "") { this.log(message, data, source, showToast, 'warning'); } public logSuccess(message: string, showToast: boolean = false, data: any = null, source: string = "") { this.log(message, data, source, showToast, 'success'); } public logError(message: string, showToast: boolean = false, data: any = null, source: string = "") { this.log(message, data, source, showToast, 'error'); } private log(message: string, data: any, source: string, showToast: boolean, toastType: string) { source = source ? '[' + source + '] ' : ''; if (toastType === 'error') console.error(message, data); else console.log(message, data); if (showToast) { if (toastType === 'error') { this._toastr.error(message); } else if (toastType === 'warning') { this._toastr.warning(message); } else if (toastType === 'success') { this._toastr.success(message); } else { this._toastr.info(message); } } } }