Newer Version Available

This content describes an older version of this product. View Latest

Log Messages

To help debug your client-side code, you can write output to the JavaScript console of a web browser.

Use the $A.log(string, [error]) method to output a log message to the JavaScript console.

The first parameter is the string to log.

The optional second parameter is an error object that can include more detail.

  • $A is the shorthand in JavaScript code for the Aura object.
  • $A.log() doesn't output in PROD or PRODDEBUG modes.

Note

For example, $A.log("This is a log message") outputs to the JavaScript console:

1This is a log message

If you put $A.log("The name of the action is: " + this.getDef().getName()) inside an action called openNote in a client-side controller, it outputs to the JavaScript console:

1The name of the action is: openNote

For instructions on using the JavaScript console, refer to the instructions for your web browser.

Logging in Production Modes

To log messages in PROD or PRODDEBUG modes, you can write a custom logging function. You must use $A.logger.subscribe(String level, function callback) to subscribe to log messages at a certain severity level.

The first parameter is the severity level you're subscribing to. The valid values are:

  • ASSERT
  • ERROR
  • INFO
  • WARNING

The second parameter is the callback function that will be called when a message at the subscribed severity level is logged. Note that $A.log() logs a message at the INFO severity level.

Let's look at some sample JavaScript code in a client-side controller.

1({
2    sampleControllerAction: function(cmp) {
3        // subscribe to severity levels
4        $A.logger.subscribe("INFO", logCustom);
5        // Following subscriptions not exercised here but shown for completeness
6        //$A.logger.subscribe("WARNING", logCustom);
7        //$A.logger.subscribe("ASSERT", logCustom);
8        //$A.logger.subscribe("ERROR", logCustom);
9
10        $A.log("log one arg");
11        $A.log("log two args", {message: "drat and double drat"});
12
13        function logCustom(level, message, error) {
14            console.log(getTimestamp(), "logCustom: ", arguments); 
15        }
16
17        function getTimestamp() {
18            return new Date().toJSON();
19        }
20    }
21
22})

$A.logger.subscribe("INFO", logCustom) subscribes so that messages logged at the INFO severity level will call the logCustom() function. In this case, logCustom() simply logs the message to the console with a timestamp.

The $A.log() calls log messages at the INFO severity level, which matches the subscription and invokes the logCustom() callback.