51

Is it just me, or is console.log() too much to ask for from HTML5 web workers?

I know that manipulating the DOM is blocked because it is potentially dangerous, but is there really any possibility that console.log() could be maliciously exploited by a multithreaded worker?

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
ejang
  • 3,982
  • 8
  • 44
  • 70

2 Answers2

29

Agreed things would be a lot nicer, but it's not too hard to hack up a primitive console.log using postMessage. David Flanagan has a nice wrapper here.

Nick
  • 6,967
  • 2
  • 34
  • 56
ebidel
  • 23,921
  • 3
  • 63
  • 76
  • 1
    Nice wrapper indeed, but unfortunately the way it is implemented does not allow the use of jQuery.Hive. – ejang Sep 01 '11 at 00:49
  • 4
    I would suggest to use Chrome dev tools (form 17+) - then you will get a nice option to debug your workers without the need to use console.log – Ido Green Dec 25 '11 at 09:16
  • 15
    Debugging is not the same as console logging, they have different but slightly overlapping usecases. – Florian Bösch Aug 24 '12 at 10:47
25

Just wanted to post that console.log is now possible at least within the Chrome Browser.

I do not know which version it was added but 35.0.1916.153 m has it.

Limitation

There is a small limitation with it though, It can only output primitives (strings, numbers, booleans) sometimes single dimension arrays.

And it can only take the first argument within the console log.

Normal Console log:

console.log("status:", _status); // status: working
console.log({ status: _status }); // { "status": working }

Worker Console log:

console.log("status:", _status); // status:
console.log({ status: _status }); // [object Object]

You could use console.log(JSON.stringify({ status: _status })); but this would not handle circular referencing objects and will not output in a pretty/easy to read objects.

Update: You can get pretty print with stringify by doing console.log(JSON.stringify(someObject, null, " "));.

WORMSS
  • 1,625
  • 28
  • 36