代码之家  ›  专栏  ›  技术社区  ›  Omar

角度6:提供日志服务…我需要帮助获取原始文件路径

  •  0
  • Omar  · 技术社区  · 7 年前

    全局.ts

    if(this.mode.dev_模式){ s=“background:222;color:bada55”; 案例‘I’: 休息; 休息; 休息; s=“background:green;color:white;'font-weight:bold;'”; 违约: }
    
    
    

    问题

    export const _log = function(msg, style, data?) {
        if(this.mode.dev_mode){
            data = (data) ? data : '';
            let s = "";
            switch ( style ) {
                case 't':
                    s = "background: #222; color: #bada55";
                    break;
                case 'i':
                    s = "background: #efefef; color: blue; 'font-weight:900;'";
                    break;
                case 'e':
                    s = "background: red; color: white; 'font-weight:bold;'";
                    break;
                case 'd':
                    s = "background: #333; color: white; 'font-weight:bold;'";
                    break;
                case 'y':
                    s = "background: #FFBB3B; color: white; 'font-weight:bold;'";
                    break;
                case 's':
                    s = "background: green; color: white; 'font-weight:bold;'";
                    break;
                default:
                   s = "background: #222; color: #bada55";
            }
            console.log(`%c ${msg}`, s, data);
        }
    };
    

    _log(' == products ==> ', 'i', products);
    

    enter image description here

    看看所有日志都说什么是不使用原始函数的简单方法?例如,在这种情况下example.component.ts_global.ts……我可以每次都把它打印出来,但我更喜欢更自动化的东西,允许我保持Log()的简短。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Ashish Ranjan    7 年前

    _log(' == products ==> ', 'i', (new Error()), products);
    

    export const _log = function(msg, style, error, data) {
        let infoLine = error.stack.split('\n')[1];
        // very ugly but I found it consistent
        let startingIndex = infoLine.indexOf('(') + 1;
        let endingIndex = infoLine.indexOf(')');
        let fileNameAndLine = (infoLine.substring(startingIndex, endingIndex))
    
        ...
    
        console.log(`%c ${msg} at ${fileNameAndLine}`, s, data);
    }
    

    How can I determine the current line number in JavaScript?

    推荐文章