Last active
February 12, 2020 06:45
-
-
Save galek/ae444d0e46c5e4b9ce6b211cfdccbcdd to your computer and use it in GitHub Desktop.
LoggerWrapper.ts for ionic and angular applications
This file contains 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
/* Copyright (C) 2009-2020, Nick Galko. All rights reserved. | |
* | |
* This file is part of the Nick Galko source-code | |
* (https://galek.github.io/portfolio/). | |
* | |
* Your use and or redistribution of this software in source and / or | |
* binary form, with or without modification, is subject to: (i) your | |
* ongoing acceptance of and compliance with the terms and conditions of | |
* the Nick Galko License Agreement; and (ii) your inclusion of this notice | |
* in any version of this software that you use or redistribute. | |
* A copy of the NGTech License Agreement is available by contacting | |
* Nick Galko. at https://galek.github.io/portfolio/ | |
*/ | |
import { Crashlytics } from '@ionic-native/fabric'; | |
import { Injectable } from '@angular/core'; | |
import { Platform, IonicErrorHandler } from 'ionic-angular'; | |
import { ConfigShop } from './config'; | |
// import * as stacktrace from 'stacktrace-js'; | |
@Injectable() | |
export class LoggerWrapper extends IonicErrorHandler { | |
constructor(private crashlytics: Crashlytics, private platform: Platform) { | |
super(); | |
} | |
addLog(message: string): void { | |
if (this.crashlytics) { this.crashlytics.addLog(message); } | |
else { this._showAlert(message); } | |
} | |
sendNoFatalError(message: string, code?: any): void { | |
if (this.platform.is('ios')) { | |
if (this.crashlytics) this.crashlytics.recordError(message, code); | |
} | |
else if (this.platform.is('android')) { | |
if (this.crashlytics) this.crashlytics.sendNonFatalCrash(message || code); | |
} | |
else { | |
if (code) { | |
if (typeof (code) == 'number') | |
this._showAlert(message + code); | |
else this._showAlert(message || code); | |
} | |
else this._showAlert(message); | |
} | |
} | |
sendFatalError(): void { | |
if (this.crashlytics) this.crashlytics.sendCrash(); | |
else this._showFatalError(); | |
} | |
setBoolValueForKey(value: boolean, key: string): void { | |
if (this.crashlytics) this.crashlytics.setBoolValueForKey(value, key); | |
} | |
setFloatValueForKey(value: number, key: string): void { | |
if (this.crashlytics) this.crashlytics.setFloatValueForKey(value, key); | |
} | |
setIntValueForKey(value: number, key: string): void { | |
if (this.crashlytics) this.crashlytics.setIntValueForKey(value, key); | |
} | |
setStringValueForKey(value: string, key: string): void { | |
if (this.crashlytics) this.crashlytics.setStringValueForKey(value, key); | |
} | |
setUserEmail(email: string): void { | |
if (this.crashlytics) this.crashlytics.setUserEmail(email); | |
} | |
setUserIdentifier(userId: string): void { | |
if (this.crashlytics) this.crashlytics.setUserIdentifier(userId); | |
} | |
setUserName(user: string): void { | |
if (this.crashlytics) this.crashlytics.setUserName(user); | |
} | |
handleError(err: any): void { | |
this.sendNoFatalError("Ionic error: " + err.message, err); | |
// // stacktrace.get().then( | |
// // trace => window.fabric.Crashlytics.endNonFatalCrash(error.message, trace) | |
// // ); | |
super.handleError(err); | |
} | |
private _showAlert(_mess: string): void { | |
if (ConfigShop.isDebug) alert(_mess); | |
else if (console) console.warn(_mess); | |
} | |
private _showFatalError(): void { | |
if (ConfigShop.isDebug) alert("Fatal error happens"); | |
else if (console) console.error("Fatal error happens"); | |
this.platform.exitApp(); | |
} | |
} | |
/* | |
USAGE: | |
0) Install fabric plugin. | |
1) In app.module.ts in 'providers' section add Crashlytics and LoggerWrapper. | |
Replace ConfigShop.isDebug on your variable what signalized what build is debuggable. | |
2) And in this file replace IonicErrorHandler on LoggerWrapper | |
Result must be: | |
{ provide: ErrorHandler, useClass: LoggerWrapper }, // IonicErrorHandler | |
2) import LoggerWrapper using DI (import module and private crashlytics: LoggerWrapper in ctor) where is needed; | |
3) call wrapper function there is needed. For example: | |
if(this.crashlytics) this.crashlytics.addLog('11'); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment