Last active
May 1, 2018 15:15
-
-
Save sohalloran/822fcb19c3e031d2fb0112b68ca315bc to your computer and use it in GitHub Desktop.
Lightning Logging Service Component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<aura:component > | |
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/> | |
<aura:attribute name="newLogRec" type="Error_Log__c" /> | |
<aura:attribute name="newLog" type="Error_Log__c" /> | |
<aura:attribute name="logError" type="string" description="Error message returned by logging service" /> | |
<force:recordData aura:id="errorLogCreator" | |
layoutType="FULL" | |
targetRecord="{!v.newLogRec}" | |
targetFields="{!v.newLog}" | |
targetError="{!v.logError}" /> | |
<aura:method name="logMessage" action="{!c.logMessage}" | |
description="Used to log error message to the custom Error_Log__c object" > | |
<aura:attribute name="name" type="String" /> | |
<aura:attribute name="msg" type="String" /> | |
</aura:method> | |
</aura:component> | |
// CONTROLLER | |
({ | |
doInit : function(component, event, helper) { | |
helper.getNewRecord(component); | |
}, | |
logMessage : function(component, event, helper) { | |
helper.createNewLog(component, event); | |
} | |
}) | |
// HELPER | |
({ | |
getNewRecord : function(component) { | |
component.find("errorLogCreator").getNewRecord( | |
"Error_Log__c", | |
null, | |
false, | |
$A.getCallback(function() { | |
var rec = component.get("v.newLog"); | |
var error = component.get("v.logError"); | |
if(error || (rec === null)) { | |
console.log('Error trying to create log error:' + error) | |
} | |
}) | |
); | |
}, | |
createNewLog : function(component, event) { | |
var params = event.getParam('arguments'); | |
if (params) { | |
component.set("v.newLog.Name", params.name); | |
component.set("v.newLog.Message__c", params.msg); | |
component.set("v.newLog.Date_Time__c", new Date()); | |
} | |
component.find("errorLogCreator").saveRecord(function(saveResult) { | |
if (saveResult.state === "ERROR") { | |
console.log('Error trying to save log error'); | |
} | |
}); | |
} | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<aura:application > | |
<c:ErrorLoggingSvc aura:id="logService" /> | |
<lightning:button label="log" onclick="{!c.logMessage}"/> | |
</aura:application> | |
// CONTROLLER | |
({ | |
logMessage : function(component, event, helper) { | |
var log = component.find("logService"); | |
log.logMessage(component.getName(), 'testing the logger service') | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment