Difference between revisions of "GM log"

From GreaseSpot Wiki
Jump to navigationJump to search
m (Text replace - "</pre>}}" to "</pre>")
(simplify)
Line 3: Line 3:
== Description ==
== Description ==


This [[API_reference|API]] method allows script authors simple access to logging informational messages in the Error Console.
This method provides a very simple logging facility.
If you aren't seeing your messages, make sure you navigate to [[mozillazine:About:config|about:config]], and change the values of <code>javascript.options.showInConsole</code> and <code>javascript.options.strict</code> to true.


If you aren't seeing your messages, make sure you navigate to [[mozillazine:About:config|about:config]], and change the values of <code>javascript.options.showInConsole</code> and <code>javascript.options.strict</code> to true. This can be helpful for debugging.  
The output is displayed in the [https://developer.mozilla.org/en/Error_Console error console], and includes the namespace and name of the script before and the provided string value.
So, this script:


The output produces the namespace, the name of the script, and the string.
<pre class='sample'>
// ==UserScript==
// @name          GM_log Example
// @namespace     http://www.example.com/
// ==/UserScript==


For example:
GM_log("This is an example of GM_log");
<pre>
#namespace/myScript: Hello World!
</pre>
</pre>
Will produce a line in the error console like:
<nowiki>http://www.example.com/GM_log Example: This is an example of GM_log</nowiki>
Compatibility: [[Version_history#0.3_beta|Greasemonkey 0.3b+]]


== Syntax ==
== Syntax ==


'''GM_log(''' ''message'' ''')'''
{{Function|GM_log|message}}


:Value: Function
=== Arguments ===
:Returns: undefined
:Compatibility: [[Version_history#0.3_beta|Greasemonkey 0.3b+]]


:{| cellpadding="5" style="border-style:solid; background:#FFFFE0;"
; <code>message</code>
|+ Parameters
: <code>String</code> (or any object with a <code>.toString()</code> method) The message to display in the console.
!style="background:#CC9900;"|'''Properties'''
|-
| <code><span style="background:#FFFFE0;">[[#message |message]]</span></code>
|}
:* All properties are optional except [[#message|message]].


=== Properties ===
=== Returns ===
----
==== <code>message</code> ====
:Value: String, Integer<sup>[1]</sup> or Boolean<sup>[1]</sup>
:Usage: <code>'''message''' = "ARGHHH!";</code>


:* Message to send to the JavaScript error console.
<code>undefined</code>
:* <sup>[1]</sup> While the value may be a non-string, it will convert the argument to a string. There may also be some instances of visual clipping on large numerical values during conversion.  This is a feature present in Mozilla based JavaScript console logging.


== Examples ==
== Examples ==
<pre class='sample'>
<pre class='sample'>
GM_log("Hello, World!");
GM_log("Hello, World!");
Line 49: Line 48:


== GM_log and Firebug ==
== GM_log and Firebug ==
Since [https://addons.mozilla.org/firefox/addon/1843 Firebug] 1.0, <code>extensions.firebug.showChromeMessages</code> must be set to <code>true</code> for GM_log messages to show up in the Firebug console.
Since [https://addons.mozilla.org/firefox/addon/1843 Firebug] 1.0, <code>extensions.firebug.showChromeMessages</code> must be set to <code>true</code> for GM_log messages to show up in the Firebug console.


It is also recommended to enable <code>extensions.firebug.showChromeErrors</code>, as doing so will reveal syntax errors and script breaks.
It is also recommended to enable <code>extensions.firebug.showChromeErrors</code>, as doing so will reveal syntax errors and script breaks.
This can be done from <code>about:config</code>, or from the Firebug GUI. Go to the "Console" tab, then click the arrow next to the tab's name, and ensure that "Show Chrome Errors" and "Show Chrome Messages" are checked.
This can be done from <code>about:config</code>, or from the Firebug GUI. Go to the "Console" tab, then click the arrow next to the tab's name, and ensure that "Show Chrome Errors" and "Show Chrome Messages" are checked.


However, one should note that since <code>GM_log</code> converts any passed parameter to a string, and works from the Chrome, it would be more advisable to simply use <code>console.log</code>, which will allow you to take advantage of Firebug's DOM Inspector on the logged objects.
However, since <code>GM_log</code> will only display a single string at a time, users with Firebug installed may prefer to use <code>console.log</code> instead.


[[Category:API_Reference|L]]
[[Category:API_Reference|L]]

Revision as of 17:39, 8 February 2010


Description

This method provides a very simple logging facility. If you aren't seeing your messages, make sure you navigate to about:config, and change the values of javascript.options.showInConsole and javascript.options.strict to true.

The output is displayed in the error console, and includes the namespace and name of the script before and the provided string value. So, this script:

// ==UserScript==
// @name          GM_log Example
// @namespace     http://www.example.com/
// ==/UserScript==

GM_log("This is an example of GM_log");

Will produce a line in the error console like:

http://www.example.com/GM_log Example: This is an example of GM_log

Compatibility: Greasemonkey 0.3b+

Syntax

function GM_log( message )

Arguments

message
String (or any object with a .toString() method) The message to display in the console.

Returns

undefined

Examples

GM_log("Hello, World!");
GM_log("Warning, " + someInputField.value + "!");

GM_log and Firebug

Since Firebug 1.0, extensions.firebug.showChromeMessages must be set to true for GM_log messages to show up in the Firebug console.

It is also recommended to enable extensions.firebug.showChromeErrors, as doing so will reveal syntax errors and script breaks. This can be done from about:config, or from the Firebug GUI. Go to the "Console" tab, then click the arrow next to the tab's name, and ensure that "Show Chrome Errors" and "Show Chrome Messages" are checked.

However, since GM_log will only display a single string at a time, users with Firebug installed may prefer to use console.log instead.