Skip to curriculum
Jump to the editor

19.4. Record console.log history

Easy
Topics
10–20 min

A support widget lets a user send a bug report, and the most useful thing to attach is what the page logged just before things went wrong. To get it, the page wraps console.log: a new function records each call and then hands it to the original, so the developer console still shows everything.

Write recordConsoleLog() in console-history.js. It starts recording at once and returns an object to read and end the record.

Signature

JAVASCRIPT
function recordConsoleLog() // returns { history, clear, stop }

history()  // returns an array: one array of arguments per call, oldest first
clear()    // forgets the recorded calls; recording goes on
stop()     // stops recording and puts the previous console.log back

Requirements

  1. Every console.log call after recordConsoleLog() is recorded as the array of its arguments, oldest first. A call with no arguments is recorded as []. Calls made before are not recorded.
  2. Each call still reaches the console.log that was there before, with the same arguments and with this unchanged, and the wrapper returns what it returns.
  3. Values are recorded as they were passed: an object is kept, not copied.
  4. history() returns a copy: changing the returned arrays never changes the record.
  5. clear() empties the record and recording continues.
  6. stop() ends recording and puts back the exact function that was there before. The record stays readable.
  7. stop() never removes a function installed after this recorder: when console.log is no longer this recorder's function, stop() only ends the recording. Calling stop() twice is harmless.
  8. Two recorders can run at once: both record each call, and the message is printed once.

Examples

JAVASCRIPT
const recorder = recordConsoleLog();
console.log("loading", 3);          // still printed
console.log({ status: 500 });
recorder.history();                 // [["loading", 3], [{ status: 500 }]]
recorder.stop();
console.log("not recorded");
recorder.history();                 // unchanged
JAVASCRIPT
const recorder = recordConsoleLog();
const copy = recorder.history();
copy.push(["fake"]);
recorder.history();                 // [] : the record is not changed
JAVASCRIPT
const a = recordConsoleLog();
const b = recordConsoleLog();       // b wraps a's function
a.stop();                           // console.log is b's function, so a only stops recording
console.log("x");                   // recorded by b only, printed once

Environment

  • Plain JavaScript in a browser page; the tests call the global recordConsoleLog from console-history.js.
  • Each test replaces console.log with a fake before calling your function, and puts the real one back after.
  • Other console methods (warn, error), timestamps and a size limit are not part of this problem.

Hints

  1. What has to be saved before console.log is replaced, and where can it live so stop can find it?
  2. How can stop tell whether console.log is still the function this recorder installed?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…