本文介绍了使用angular2-logger显示行号和文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用 Angular2-logger .

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

输出打印在控制台中,如下图所示

Output printed in console like image below

Angular2-logger 或替代类中是否有任何选项可以用来显示我所在位置的文件名行号 m调用 logger 方法? (与console.log(...)相似,如下图所示)

Is there any option in Angular2-logger or alternative class that I can use to show file name and line number of where I'm calling logger methods ? (same as console.log(...) like image bellow)

有什么解决办法吗?

推荐答案

我通过这种方式找到了解决方案:

I figured out the solution by this way:

1-我创建了LoggerService.

2-我将服务导入到app.module.ts并将其用作提供程序.

2- I imported the service to app.module.ts and use it as a provider.

3- LoggerService看起来像这样:

3- LoggerService looks like 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);
        }
    }
}

这篇关于使用angular2-logger显示行号和文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 12:37