Custom Object :
You have to alter your apex class to support the logging functionality. You basically start with a custom object with the fields you need and then log when you want it.
You have to alter your code blocks in such a way that the committing of logs is always called.
Src : http://succeedwithsalesforce.com/creating-persistent-logs-using-apex-and-a-custom-object/
https://medium.com/slalom-technology/without-a-debug-trace-easier-logging-in-apex-ad09c3ce97b
Logging Using Attachment: Custom Objects logs are good, But they have size limitations and large text field size limitation that can force you to truncate. Having an attachment instead helps you have large raw logs on integration JSON/XML or even your own debugging to full potential. I have a sample utility that logs in an attachment instead.
public without sharing class LoggerUtil {
private static Id parentId;
private Static String bodyString;
public static void initialize(Id parentIdToSCtore){
//Intitialzie things the parentID to save later
parentId = parentIdToSCtore;
bodyString ='';
}
public static void log(String loggedString){
bodyString = bodyString + loggedString +'\n';
}
public static void commitLog(){
Attachment attachment = new Attachment(ParentId = parentId, Body=Blob.valueOf(bodyString),Name='RawLogger'+System.now());
insert attachment;
}
}
Edit: Code in Trigger
Trigger AccountTrigger on Account(after insert){
ApexDebugLog.createLog(
'{"Type" : "Logging","ApexClass" : "AccountTrigger ","Method" : "createErrorLog","RecordId" : "","Message" : "On :"+Trigger.isInsert ,"StackTrace" : ""}'
);
}