错误堆栈在将堆栈拆分为数组时显示不同的信息

huangapple go评论79阅读模式
英文:

Error stack displays different information when I split the stack into array

问题

我正在尝试创建一个日志服务。
当我尝试仅显示 e.stack 时,它显示每个文件和文件的行号。

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); 显示:

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)

而通过循环,每个元素显示:

-------
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)

我想要保留数组中的第一个详细信息,但是当我拆分成数组时,它显示来自 main.js 或 app-app-module.js 的详细信息。

如何保留第一个详细信息(例如 at LogEntry.buildLogString (logEntry.ts:32))?

英文:

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)) ?

答案1

得分: 0

我使用 stacktrace.js 找到了解决方案。这为我提供了原始堆栈。

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 stack;
        })
        .catch(errback);

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

这显示了一个我想要的数组:

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:)"
英文:

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:)"

huangapple
  • 本文由 发表于 2020年1月3日 16:12:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定