Last active
September 1, 2017 07:55
-
-
Save mrister/185dc0f6e72ba1ef7481bdb281e93c65 to your computer and use it in GitHub Desktop.
Logging to Loggly from Angular application
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
// To configure an external lib like loggly-tracker follow these steps | |
// 1. Either download or in your index.html add areference to a Loggly script | |
// <script type="text/javascript" src="//cloudfront.loggly.com/js/loggly.tracker-2.1.min.js"></script> | |
// 2. In typings.d.ts file declare the Loggly global variable like: | |
// declare var LogglyTracker; | |
// 3. You should be all set to use the LogglyTracker | |
import {BrowserModule} from '@angular/platform-browser'; | |
import {ErrorHandler, Injectable, NgModule} from '@angular/core'; | |
import {AppComponent} from './app.component'; | |
import {ErrorClickComponent} from './error-click/error-click.component'; | |
// define a Loggly service | |
@Injectable() | |
export class LogglyLoggerService { | |
// https://github.com/loggly/loggly-jslogger | |
private loggly: any = new LogglyTracker(); | |
constructor() { | |
this.loggly.push({ | |
logglyKey: 'YOUR_LOGGLY_TOKEN', | |
sendConsoleErrors: true, | |
tag: 'AngularJS-logs' | |
}); | |
} | |
log(error: Error) { | |
// do not push error as it will fail due to circular structure | |
this.loggly.push({ message: error.message, stack: error.stack }); | |
} | |
} | |
// our global error handler | |
@Injectable() | |
class GlobalErrorHandler extends ErrorHandler { | |
constructor(private loggerService: LogglyLoggerService) { | |
super(); | |
} | |
handleError(error) { | |
this.loggerService.log(error); | |
} | |
} | |
@NgModule({ | |
declarations: [ | |
AppComponent, | |
ErrorClickComponent | |
], | |
imports: [ | |
BrowserModule | |
], | |
providers: [ | |
{ | |
provide: ErrorHandler, | |
useClass: GlobalErrorHandler | |
}, | |
// register teh service | |
LogglyLoggerService, | |
], | |
bootstrap: [AppComponent] | |
}) | |
export class AppModule {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment