需要参考$ log.log调用行号 [英] need reference to the $log.log call line number

查看:236
本文介绍了需要参考$ log.log调用行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我用角$日志服务控制台中的所有行显示了以angular.js参考:5687,而不是在那里我叫$ log.log函数的行

我要如何才能到达那里我叫$登录该行的参考?我也有我自己的服务,大约$日志包裹,我怎么可以参考调用我的服务,而不是$登录?

例如在 Logger.js

1)angular.module('MyApp的')。工厂('记录仪',函数($日志){
 2),返回{日志:函数(MSG){$ log.log(MSG); };
 3)});

而在 SomeCtrl.js

1)angular.module('MyApp的')。控制器('SomeCtrl',函数($范围,$日志,记录仪){
 ...
10)$ log.log('嗨从$登录'); //参照SomeCtrl.js:10
 ...
25)Logger.log('嗨从记录仪'); //参照SomeCtrl.js:25


解决方案

由于我假设你只记录在发展,像大多数开发人员,你在Chrome开发时,可以将属性添加到像全球范围内所以:

通过<一个href=\"http://stackoverflow.com/questions/11386492/accessing-line-number-in-v8-javascript-chrome-node-js\">Accessing在V8的JavaScript(Chrome浏览器和Node.js的)号码

  Object.defineProperty(窗口,'__stack',{
  得到:函数(){
    。VAR原稿=错误prepareStackTrace;
    错误prepareStackTrace =功能(_,栈){返回堆栈。 };
    VAR ERR =新的错误;
    Error.captureStackTrace(ERR,arguments.callee的);
    VAR堆栈= err.stack;
    错误prepareStackTrace =原稿。
    返回堆栈;
  }
});Object.defineProperty(窗口'__line',{
  得到:函数(){
    返回__stack [1] .getLineNumber();
  }
});的console.log(__线);

现在你可以在你的code使用 __行的任何地方。

修改:它并不像你可以从调用堆栈中的文件名。如果你正在写的JavaScript干净,每个文件应该有自己的范围,在其中你可以定义变种文件名='test.js'; ,所以你可以看起来像:

定义全局地方:

  Object.defineProperty(窗口,'__ prevLine',{
    得到:函数(){
        的console.log('__线,__stack);
        返回__stack [2] .getLineNumber();
    }
});功能日志(多个){
    的console.log(S,__ prevLine)
}

和您的实现:

 (函数(){
    VAR _scriptName ='test.js';    // ...很多code的    日志('发生了什么事情,_scriptName);
}());

下面是一个演示的想法小提琴:

http://jsfiddle.net/langdonx/zJJ8r/

When I use angular $log service, all the lines in the console show up with a reference to angular.js:5687 instead of the line where I called the $log.log function.

How can I get a reference to the line where I called $log? Also I have my own service that wraps around $log, how can I reference calls to my service instead of $log?

For example in Logger.js:

 1) angular.module('MyApp').factory('Logger', function($log){
 2)    return { log : function(msg) { $log.log(msg); };
 3) });

And in SomeCtrl.js

 1) angular.module('MyApp').controller('SomeCtrl', function($scope, $log, Logger) {
 ...
10) $log.log('Hi from $log');     // reference to SomeCtrl.js:10
 ...
25) Logger.log('Hi from Logger'); // reference to SomeCtrl.js:25

解决方案

Since I assume you're logging only in development and, like most developers, you're developing in Chrome, you can add a property to the global scope like so:

via Accessing line number in V8 JavaScript (Chrome & Node.js)

Object.defineProperty(window, '__stack', {
  get: function(){
    var orig = Error.prepareStackTrace;
    Error.prepareStackTrace = function(_, stack){ return stack; };
    var err = new Error;
    Error.captureStackTrace(err, arguments.callee);
    var stack = err.stack;
    Error.prepareStackTrace = orig;
    return stack;
  }
});

Object.defineProperty(window, '__line', {
  get: function(){
    return __stack[1].getLineNumber();
  }
});

console.log(__line);

And now you can use __line anywhere in your code.

Edit: It doesn't look like you can get the filename from the call stack. If you're writing clean JavaScript, each file should have its own scope in which you could define var filename = 'test.js';, so your could look like:

Defined globally somewhere:

Object.defineProperty(window, '__prevLine', {
    get: function () {
        console.log('__line', __stack);
        return __stack[2].getLineNumber();
    }
});

function log(s) {
    console.log(s, __prevLine)
}

And your implementation:

(function () {
    var _scriptName = 'test.js';

    // ... lots of code

    log('something happened', _scriptName);
}());

Here's a fiddle that demonstrates the idea:

http://jsfiddle.net/langdonx/zJJ8r/

这篇关于需要参考$ log.log调用行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆