1

I am developing application using Angular & I want to know recommended way to use error logging.

My query is should I go for console logging or text based logging as text based logging stores historical error information on server which later can be use for debugging. but this will also use server space while for console log there is no way to track error if customer close the browser & even don't remember how to re-generate it.

Sushil Jadhav
  • 2,217
  • 2
  • 19
  • 33

1 Answers1

0

Maybe you can choose error.service to send you log when catched an error with mail or you can add a simple db (end of day a cron jobs works daily which remove logs). Console.log output also shows you history of the error. So, you can reproduce error .

https://stackblitz.com/edit/angular-rr28hm

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  name = 'Angular';
  error: string = null;

  constructor() {
    try {
      throw new Error("hi, i am an error!");
    } catch (error) {
      this.error = JSON.stringify(error.stack);
      console.log(this.error);
      // this.http.post("url", { data: this.error }).subscribe((response) => console.log("POST: error saved to db: ", response));
    }
  }
}

When you parse error string you will see the error line number and history.

Serkan KONAKCI
  • 1,100
  • 11
  • 16
  • Even after browser close it is able to show historical error if customer did this after issue? We don't want to install any browser extension on client PC. – Sushil Jadhav Nov 19 '18 at 06:09
  • Maybe you can add event trigger while window.unload(). https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onunload – Serkan KONAKCI Nov 19 '18 at 17:46