Show line number and file name using angular2 -logger

I am using Angular2-logger in my project.

import { Logger } from "angular2-logger/core"; ... logger: Logger; ... logger.info('<log string>') 

Output printed on console as image below

enter image description here

Is there an option in Angular2 -logger or an alternative class that I can use to display the file name and line number where I call logger ? (same as console.log(...) like the image below)

enter image description here

Is there any solution?

+7
angular
source share
3 answers

I understood the solution this way:

1- I created a LoggerService .

2- I imported the service into app.module.ts and used it as a provider.

3- LoggerService looks something like this:

 import { Injectable } from '@angular/core'; @Injectable() export class LoggerService { public error(message) { try { throw new Error(); } catch (e) { console.log(e.stack); const stackTraces = e.stack.split('at'); const fileList = stackTraces[2].substr( stackTraces[2].lastIndexOf('/src'), stackTraces[2].length - 1 ).replace(')', '' ); const functionName = stackTraces[2].substr(0, stackTraces[2].indexOf('(') - 1); // Do whatever you want, use console.log or angular2-logger service console.log(message); } } } 
+1
source share

I would recommend you not use any logger libraries for two reasons

  • when angular provides it by default.
  • These libraries are independent of version updates.

When we register errors at the front end (for example, in a browser environment), you can create a specialized registration service.

Regarding the issue you mentioned, you can disable the logs by overwriting the service with usevalue with an empty class / function depending on the environment you want.

+1
source share

You use the Logger info method, which is the least verbose. It is not possible to configure it as you would like. An alternative is to use the error and warning methods - they provide a stack trace with the file name and line number.

Logging into the console from Angular2 -logger methods

0
source share

All Articles