2009-03-02 2 views
4

방화 광 콘솔에 기록 된 가장 최근의 명령을 읽는 방법을 찾고 있습니다. 예를 들어자바 스크립트에서 방화 광 콘솔을 읽는 중

, 나는 다음

console.debug('The most current request URI is /sweatsocks'); 

그리고 (의사) 코드의 또 다른 조각을하지 뭔가를 할 수 있었다 다음

if (mostRecentConsoleEntry().endsWith('/sweatsocks')) { 
    // do some stuff 
} 

코드 아래에있을 것 디버그 문을되는 컨텍스트 테스트하고 셀렌 스크립트 내에서 콘솔 검사가 수행됩니다. 이렇게하면 런타임에 빌드 된 물건뿐만 아니라 js 함수에 깊게 묻혀있는 정보를 볼 수 있습니다.

+0

"가장 현재 요청 URI를 is/sweatsocks "- 내가 들었던 가장 거대한 URI입니다. – nickf

답변

1

console.log()을 다시 쓰고 모든 로그를 배열에 추가 할 수 있습니까? 그런 다음 원래 console.log()을 실행하고 콘솔에서 디버그 출력을 얻으려고 반복합니다.

5

console.log 함수를 덮어 쓰면 필요한 추가 기능을 추가 할 수 있습니다. console는 printf와 스타일 구문 수 있기 때문에

var oldLog = console.log; 
var lastLog; 
console.log = function() { 
    // do whatever you need to do here: store the logs into a different variable, etc 
    // eg: 
    lastLog = arguments; 

    // then call the regular log command 
    oldLog.apply(console, arguments); 
}; 

이 가장 방탄 해결책이 될 수 없습니다 :

console.log("%d + %d = %s", 1, 3, "four"); 

를 ...하지만 그것은 아마 당신의 시작입니다.

+0

그건 내가 원했던 것입니다 :) +1 – alex

+0

console.log는 읽기 전용이므로 FF3 및 Firebug 1.3.3에서는 작동하지 않습니다. 또한 콘솔 개체에 속성을 추가 할 수 없습니다. – some

+1

apply의 구문은 .apply (thisobject, array)입니다. 따라서 콘솔 객체를 변경하려고하면 다음과 같아야합니다. console.oldLog.apply (console.oldLog, arguments); 대신 개인 변수를 저장하는 자체 실행 함수를 사용하는 것이 좋습니다. – some

2

console.debug를 덮어 쓰지 않고 덮어 쓰지 말고, console.debug와 필요한 기능을 구현하십시오.

var debugCalls = [ ]; 
function myDebug(errorMessage){ 
    console.debug(errorMessage); //maintain original functionality 
    debugCalls[debugCalls.length] = errorMessage; 
    //the previous argument to myDebug is debugCalls[debugCalls.length] 

    //you may also want to call an ajax function to report this error 
    mailError(errorMessage); 
} 
0

좀 더 정교한 버전의 내가 함께 넣어 :

/** 
* Console log with memory 
* 
* Example: 
* 
*  console.log(1); 
*  console.history[0]; // [1] 
* 
*  console.log(123, 456); 
*  console.history.slice(-1)[0]; // [123, 456] 
* 
*  console.log('third'); 
*  // Setting the limit immediately trims the array, 
*  // just like .length (but removes from start instead of end). 
*  console.history.limit = 2; 
*  console.history[0]; // [123, 456], the [1] has been removed 
* 
* @author Timo Tijhof, 2012 
*/ 
console.log = (function() { 
    var log = console.log, 
     limit = 10, 
     history = [], 
     slice = history.slice; 

    function update() { 
     if (history.length > limit) { 
      // Trim the array leaving only the last N entries 
      console.history.splice(0, console.history.length - limit); 
     } 
    } 

    if (console.history !== undefined) { 
     return log; 
    } 

    Object.defineProperty(history, 'limit', { 
     get: function() { return limit; }, 
     set: function (val) { 
      limit = val; 
      update(); 
     } 
    }); 

    console.history = history; 

    return function() { 
     history.push(slice.call(arguments)); 
     update(); 
     return log.apply(console, arguments); 
    }; 

}()); 
+0

업데이트 : Object.defineProperty 논리를 추가하여 console.history.limit을 설정하여 다음 log() 호출 후 즉시 트리밍하지 않도록했습니다 (내부 배열 길이와 동일) – Krinkle

관련 문제