0

I am trying to create a log service. When I try to display just e.stack, it shows each file and line number of the file.

buildLogString() {        
        const e = new Error();
        console.log(e.stack);
        const stack = e.stack.split('\n');
        for (const item of stack) {
            console.log('-------');
            console.log(item);
        }
    }

console.log(e.stack); displays:

Error
    at LogEntry.buildLogString (logEntry.ts:32)
    at LogService.writeToLog (log.service.ts:50)
    at LogService.warn (log.service.ts:23)
    at new BodyComponent (body.component.ts:70)
    at createClass (core.js:21157)
    at createDirectiveInstance (core.js:21026)
    at createViewNodes (core.js:29386)
    at createRootView (core.js:29300)
    at callWithDebugContext (core.js:30308)
    at Object.debugCreateRootView [as createRootView] (core.js:29818)

And with for loop, each element shows:

-------
Error
 -------
     at LogEntry.buildLogString (http://localhost:4200/main.js:24639:21)
 -------
     at LogService.writeToLog (http://localhost:4200/main.js:24576:31)
 -------
     at LogService.warn (http://localhost:4200/main.js:24547:14)
 -------
     at new BodyComponent (http://localhost:4200/app-app-module.js:30694:27)

I want to keep the first details into the array but when I split into array, it shows details from main.js or app-app-module.js

How can I keep the first details (E.g. at LogEntry.buildLogString (logEntry.ts:32)) ?

sally
  • 379
  • 2
  • 8
  • 17
  • 1
    Does this answer your question? [How to get JavaScript caller function line number? How to get JavaScript caller source URL?](https://stackoverflow.com/questions/1340872/how-to-get-javascript-caller-function-line-number-how-to-get-javascript-caller) – PierreD Jan 03 '20 at 08:21
  • I already tried that. When it displays stack, it shows line number from each file but when I split stack, it shows line number from app-app-module.js or main.js – sally Jan 03 '20 at 08:38
  • Check this if it helps: https://stackoverflow.com/questions/47790410/show-line-number-and-file-name-using-angular2-logger – Prashant Pimpale Jan 03 '20 at 08:57

1 Answers1

0

I found solution with stacktrace.js. This gives me original stack.

https://www.stacktracejs.com/#!/docs/stacktrace-js

callback (stackframes) {
        const stack = [];
        stackframes.map(function(sf) {
            stack.push(sf.toString());
        });

        return stack;
    }

generateStack() {
    const errback = function(err) { console.log(err.message); };
    const frame = StackTrace.get()
        .then(this.callback)
        .then(function(stack) {                
            return newStack;
        })
        .catch(errback);


    frame.then(value => {
        console.log(value);
    });
}

this displays an array which I want:

0: "errback (webpack:///src/shared/services/log-service/logEntry.ts:73:36)"
1: "buildLogString (webpack:///src/shared/services/log-service/logEntry.ts:40:31)"
2: "this.shouldLog (webpack:///src/shared/services/log-service/log.service.ts:50:30)"
3: "warn (webpack:///src/shared/services/log-service/log.service.ts:23:13)"
4: "new BodyComponent (webpack:///src/app/core/body/desktop/body/body.component.ts:70:25)"
5: "Array (webpack:///node_modules/@angular/core/fesm5/core.js:21157:)"
6: "createDirectiveInstance (webpack:///node_modules/@angular/core/fesm5/core.js:21026:)"
7: "createPipeInstance (webpack:///node_modules/@angular/core/fesm5/core.js:29386:)"
sally
  • 379
  • 2
  • 8
  • 17