Error Logging Framework in Salesforce
In Salesforce, to check for the errors, we do use debug logs. But the issue arises because we don't have access to logs when there is error in production. Many of the errors are even hard to replicate in production. Also the standard debug log hits the storage limit in production. Also if we delete some logs, it might delete those logs which would have been important to others. So here we are going to see a way to store the errors in SFDC so that users at any point of time in future can see and work upon them, without facing any of the issues mentioned above.
So to start with we will create a custom object named : Log__c
Then we will create the following custom fields of this object :
1. Log_Number__c : Auto Number
2. Exception_Cause__c : Long Text Area
3. Exception_Message__c : Long Text Area
4. Exception_Type__c : Text
5. Line_Number__c : Number
6. Stack_Trace__c : Long Text Area
7. Execution_Context__c : Text
Then create custom Metadata in SFDC, with the following details :
Then in above custom Metadata create the following records :
Then we will write the following apex class to store the exception:
public class ExceptionLogger{
//Method to insert Log__c records
public static void insertLogRecord(List e)
{
List logList = new List();
for(Exception ex:e)
{
Log__c log = new Log__c ();
log.Stack_Trace__c= ex.getStackTraceString();
log.Line_Number__c= ex.getLineNumber();
log.Exception_Type__c = ex.getTypeName();
log.Exception_Cause__c = String.valueOf(ex.getCause());
log.Exception_Message__c = ex.getMessage();
logList.add(log);
}
database.insert(logList,false);
}
}
After completing this, now wheresoever we want to capture our logs we will just use the code below in our apex classes :
catch(Exception e){
List eList = new List();
eList.add(e);
ExceptionLogger.insertLogRecord(eList);
}
Now you can go in SFDC, search for Log object and see the error being logged in form of records with all the neccesary information.
So to start with we will create a custom object named : Log__c
Then we will create the following custom fields of this object :
1. Log_Number__c : Auto Number
2. Exception_Cause__c : Long Text Area
3. Exception_Message__c : Long Text Area
4. Exception_Type__c : Text
5. Line_Number__c : Number
6. Stack_Trace__c : Long Text Area
7. Execution_Context__c : Text
Then create custom Metadata in SFDC, with the following details :
Then in above custom Metadata create the following records :
Then we will write the following apex class to store the exception:
public class ExceptionLogger{
//Method to insert Log__c records
public static void insertLogRecord(List
{
List
for(Exception ex:e)
{
Log__c log = new Log__c ();
log.Stack_Trace__c= ex.getStackTraceString();
log.Line_Number__c= ex.getLineNumber();
log.Exception_Type__c = ex.getTypeName();
log.Exception_Cause__c = String.valueOf(ex.getCause());
log.Exception_Message__c = ex.getMessage();
logList.add(log);
}
database.insert(logList,false);
}
}
After completing this, now wheresoever we want to capture our logs we will just use the code below in our apex classes :
catch(Exception e){
List
eList.add(e);
ExceptionLogger.insertLogRecord(eList);
}
Comments
Post a Comment