function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dr. WhoDr. Who 

Debug Lightning Controller

I referenced the documentation on writing debug messages to console.  Docs had this pattern for logging:
({
	actionFunction : function(component, event, helper) {
           $A.logger.subscribe("INFO", logCustom);
           $A.log("some message about function");

    	   //  code to perform action function here

           //helper methods 
           function logCustom(level, message, error) {
              console.log(getTimestamp(), "logCustom: ", arguments); 
    	   }
    	
           function getTimestamp() {
              return new Date().toJSON();
    	   }        
	}
})
This is fine.  But, I've got several functions in my controller and I don't want to cut and paste logCustom() and getTimeStamp() into every controller action.  Nor, do I want to have to initialize the logger by calling $A.logger.subscribe(...) in every method call.  This seems like something for Helper.  My initial attempt was to move logCustom() and getTimeStamp() from the Controller function to Helper and then just call
$A.logger.subscribe("INFO", helper.logCustom);

within the body of the controller action.  After doing this my component stopped working.  No log message in the Chrome developer console.  No log in the Force.com Developer Console to indicate an error.

Two questions:
What did I miss here regarding the appropriate use the the Helper within the framework?
Where else should I look for error messages to see why controller code is not executing?

Thanks
 
KristofferGreeneKristofferGreene

$A.logger.subscribe subscribes you to INFO level log statements, which are probably the default for $A.log() if not explicitly set. So you use $A.log() to actually log your output after subscribing once. You'd want to do the subscription only once as you mentioned, so you'd take care of this off the init event. You'd do that with something like this:

Component.cmp

<aura:component implements="force:appHostable">
	<aura:handler name="init" value="{!this}" action="{!c.init}"/>
	<!-- YOUR COMPONENT HERE -->
</aura:component>

ComponentController.js

({
	init: function(component, event, helper) {
		helper.init();
	},
	anotherAction: function(component, event, helper) {
		$A.log('log event!');
	}
})
ComponentHelper.js
({
	init: function() {
		$A.logger.subscribe("INFO", logCustom);
		$A.log('initialized logger');

		function logCustom(level, message, error) {
            console.log(getTimestamp(), "logCustom: ", arguments);
        }

        function getTimestamp() {
            return new Date().toJSON();
        }
    }
})
KristofferGreeneKristofferGreene

Or rather, to clarify, $A.logger.subscribe is subscribing you to INFO-level log statements in the above code.

You always could revert back to or at least test with console.log in a browser that does support it.