Greasemonkey provides a logging function, GM_log
, that allows you to write messages to JavaScript Console. Such messages should be taken out before release, but they are
enormously helpful in debugging. Plus, watching the console pile up with log messages is much more satisfying than sprinkling
alerts throughout your code and clicking OK over and over.
GM_log
takes one argument, the string to be logged. After logging to JavaScript Console, the user script will continue executing
normally.
![link to this example [link]](../images/permalink.gif)
Example: Write to JavaScript Console and continue (gmlog.user.js
)
if (/^http:\/\/diveintogreasemonkey\.org\//.test(window.location.href)) { GM_log('running on Dive Into Greasemonkey site w/o www prefix'); } else { GM_log('running elsewhere'); } GM_log('this line is always printed');
If you install this user script and open http://diveintogreasemonkey.org/, these two lines will appear in JavaScript Console:
Greasemonkey: http://diveintomark.org/projects/greasemonkey//Test Log: running on Dive Into Greasemonkey site w/o www prefix Greasemonkey: http://diveintomark.org/projects/greasemonkey//Test Log: this line is always printed
As you can see, Greasemonkey includes the namespace and script name, taken from the user script's metadata section, then the message that was passed as an argument to GM_log
.
If you visit somewhere other than http://diveintogreasemonkey.org/, these two lines will appear in JavaScript Console:
Greasemonkey: http://diveintomark.org/projects/greasemonkey//Test Log: running elsewhere Greasemonkey: http://diveintomark.org/projects/greasemonkey//Test Log: this line is always printed
I tried and failed to find a length limit for logged messages. It is larger than 255 characters. Plus, lines in JavaScript Console wrap properly, so you can always scroll down to see the rest of your log message. Go nuts with logging!
![]() |
In JavaScript Console, you can right-click (Mac users control-click) on any line and select Copy to copy it to the clipboard. |