Custom Object to Hold Log Information
up vote
1
down vote
favorite
I need some help at least with the Logic to be able to do this.
Basically i need to create a Record on a custom Object everytime an Apex Class is runned on success or error, that Record should show information about the class that was runned, the method, the error message if it exists , etc...
If you could give me some guidelines i would be very appreciated, Thank you in advance.
apex custom-object create-records
add a comment |
up vote
1
down vote
favorite
I need some help at least with the Logic to be able to do this.
Basically i need to create a Record on a custom Object everytime an Apex Class is runned on success or error, that Record should show information about the class that was runned, the method, the error message if it exists , etc...
If you could give me some guidelines i would be very appreciated, Thank you in advance.
apex custom-object create-records
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I need some help at least with the Logic to be able to do this.
Basically i need to create a Record on a custom Object everytime an Apex Class is runned on success or error, that Record should show information about the class that was runned, the method, the error message if it exists , etc...
If you could give me some guidelines i would be very appreciated, Thank you in advance.
apex custom-object create-records
I need some help at least with the Logic to be able to do this.
Basically i need to create a Record on a custom Object everytime an Apex Class is runned on success or error, that Record should show information about the class that was runned, the method, the error message if it exists , etc...
If you could give me some guidelines i would be very appreciated, Thank you in advance.
apex custom-object create-records
apex custom-object create-records
asked 7 hours ago
Nuno Carvalho
254
254
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
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" : ""}'
);
}
On the first link you sended , how can i call the ApexDebugLog class on a Trigger? and pass the parameters. Thank you for your answer btw.
– Nuno Carvalho
6 hours ago
@Praynay Jaiswal
– Nuno Carvalho
4 hours ago
@NunoCarvalho I cant see any issue in calling it from Triggger.gist.github.com/miragedeb/ac39f97d622572cfdb8d
– Pranay Jaiswal
4 hours ago
@Praynay Jaiswal , but how can i call it even if there's no error on the process, i still want to create a record that shows what class and method was run, the time it was run, etc... can i do this without a try/catch?
– Nuno Carvalho
4 hours ago
Use that to be the first line of the trigger, and send exception as null, it will help you track it
– Pranay Jaiswal
4 hours ago
|
show 1 more comment
up vote
3
down vote
You can instantiate any standard exception and get its stack trace. Using regular expressions, you can from there determine the running class and method. I outlined that process here:
Get Currently Executing Class/Method Name?
I would be extremely wary of logging every method call. Each time you log would consume DML Statements, a fairly restrictive governor limit. Even if you cache your logs and attempt to flush the cache as near the end of your transaction as possible, it adds burden to the system which could cause failures. What's worse, LimitException
cannot be caught.
If you insist on incorporating this kind of logging, be absolutely certain you incorporate a configurable flag to suppress logging behavior. You can use Hierarchy Custom Setting
, Custom Permission
, etc. The point is, an administrator in your org should be able to turn off this logging at any time without needing a deployment.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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" : ""}'
);
}
On the first link you sended , how can i call the ApexDebugLog class on a Trigger? and pass the parameters. Thank you for your answer btw.
– Nuno Carvalho
6 hours ago
@Praynay Jaiswal
– Nuno Carvalho
4 hours ago
@NunoCarvalho I cant see any issue in calling it from Triggger.gist.github.com/miragedeb/ac39f97d622572cfdb8d
– Pranay Jaiswal
4 hours ago
@Praynay Jaiswal , but how can i call it even if there's no error on the process, i still want to create a record that shows what class and method was run, the time it was run, etc... can i do this without a try/catch?
– Nuno Carvalho
4 hours ago
Use that to be the first line of the trigger, and send exception as null, it will help you track it
– Pranay Jaiswal
4 hours ago
|
show 1 more comment
up vote
1
down vote
accepted
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" : ""}'
);
}
On the first link you sended , how can i call the ApexDebugLog class on a Trigger? and pass the parameters. Thank you for your answer btw.
– Nuno Carvalho
6 hours ago
@Praynay Jaiswal
– Nuno Carvalho
4 hours ago
@NunoCarvalho I cant see any issue in calling it from Triggger.gist.github.com/miragedeb/ac39f97d622572cfdb8d
– Pranay Jaiswal
4 hours ago
@Praynay Jaiswal , but how can i call it even if there's no error on the process, i still want to create a record that shows what class and method was run, the time it was run, etc... can i do this without a try/catch?
– Nuno Carvalho
4 hours ago
Use that to be the first line of the trigger, and send exception as null, it will help you track it
– Pranay Jaiswal
4 hours ago
|
show 1 more comment
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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" : ""}'
);
}
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" : ""}'
);
}
edited 4 hours ago
answered 7 hours ago
Pranay Jaiswal
12.7k32251
12.7k32251
On the first link you sended , how can i call the ApexDebugLog class on a Trigger? and pass the parameters. Thank you for your answer btw.
– Nuno Carvalho
6 hours ago
@Praynay Jaiswal
– Nuno Carvalho
4 hours ago
@NunoCarvalho I cant see any issue in calling it from Triggger.gist.github.com/miragedeb/ac39f97d622572cfdb8d
– Pranay Jaiswal
4 hours ago
@Praynay Jaiswal , but how can i call it even if there's no error on the process, i still want to create a record that shows what class and method was run, the time it was run, etc... can i do this without a try/catch?
– Nuno Carvalho
4 hours ago
Use that to be the first line of the trigger, and send exception as null, it will help you track it
– Pranay Jaiswal
4 hours ago
|
show 1 more comment
On the first link you sended , how can i call the ApexDebugLog class on a Trigger? and pass the parameters. Thank you for your answer btw.
– Nuno Carvalho
6 hours ago
@Praynay Jaiswal
– Nuno Carvalho
4 hours ago
@NunoCarvalho I cant see any issue in calling it from Triggger.gist.github.com/miragedeb/ac39f97d622572cfdb8d
– Pranay Jaiswal
4 hours ago
@Praynay Jaiswal , but how can i call it even if there's no error on the process, i still want to create a record that shows what class and method was run, the time it was run, etc... can i do this without a try/catch?
– Nuno Carvalho
4 hours ago
Use that to be the first line of the trigger, and send exception as null, it will help you track it
– Pranay Jaiswal
4 hours ago
On the first link you sended , how can i call the ApexDebugLog class on a Trigger? and pass the parameters. Thank you for your answer btw.
– Nuno Carvalho
6 hours ago
On the first link you sended , how can i call the ApexDebugLog class on a Trigger? and pass the parameters. Thank you for your answer btw.
– Nuno Carvalho
6 hours ago
@Praynay Jaiswal
– Nuno Carvalho
4 hours ago
@Praynay Jaiswal
– Nuno Carvalho
4 hours ago
@NunoCarvalho I cant see any issue in calling it from Triggger.gist.github.com/miragedeb/ac39f97d622572cfdb8d
– Pranay Jaiswal
4 hours ago
@NunoCarvalho I cant see any issue in calling it from Triggger.gist.github.com/miragedeb/ac39f97d622572cfdb8d
– Pranay Jaiswal
4 hours ago
@Praynay Jaiswal , but how can i call it even if there's no error on the process, i still want to create a record that shows what class and method was run, the time it was run, etc... can i do this without a try/catch?
– Nuno Carvalho
4 hours ago
@Praynay Jaiswal , but how can i call it even if there's no error on the process, i still want to create a record that shows what class and method was run, the time it was run, etc... can i do this without a try/catch?
– Nuno Carvalho
4 hours ago
Use that to be the first line of the trigger, and send exception as null, it will help you track it
– Pranay Jaiswal
4 hours ago
Use that to be the first line of the trigger, and send exception as null, it will help you track it
– Pranay Jaiswal
4 hours ago
|
show 1 more comment
up vote
3
down vote
You can instantiate any standard exception and get its stack trace. Using regular expressions, you can from there determine the running class and method. I outlined that process here:
Get Currently Executing Class/Method Name?
I would be extremely wary of logging every method call. Each time you log would consume DML Statements, a fairly restrictive governor limit. Even if you cache your logs and attempt to flush the cache as near the end of your transaction as possible, it adds burden to the system which could cause failures. What's worse, LimitException
cannot be caught.
If you insist on incorporating this kind of logging, be absolutely certain you incorporate a configurable flag to suppress logging behavior. You can use Hierarchy Custom Setting
, Custom Permission
, etc. The point is, an administrator in your org should be able to turn off this logging at any time without needing a deployment.
add a comment |
up vote
3
down vote
You can instantiate any standard exception and get its stack trace. Using regular expressions, you can from there determine the running class and method. I outlined that process here:
Get Currently Executing Class/Method Name?
I would be extremely wary of logging every method call. Each time you log would consume DML Statements, a fairly restrictive governor limit. Even if you cache your logs and attempt to flush the cache as near the end of your transaction as possible, it adds burden to the system which could cause failures. What's worse, LimitException
cannot be caught.
If you insist on incorporating this kind of logging, be absolutely certain you incorporate a configurable flag to suppress logging behavior. You can use Hierarchy Custom Setting
, Custom Permission
, etc. The point is, an administrator in your org should be able to turn off this logging at any time without needing a deployment.
add a comment |
up vote
3
down vote
up vote
3
down vote
You can instantiate any standard exception and get its stack trace. Using regular expressions, you can from there determine the running class and method. I outlined that process here:
Get Currently Executing Class/Method Name?
I would be extremely wary of logging every method call. Each time you log would consume DML Statements, a fairly restrictive governor limit. Even if you cache your logs and attempt to flush the cache as near the end of your transaction as possible, it adds burden to the system which could cause failures. What's worse, LimitException
cannot be caught.
If you insist on incorporating this kind of logging, be absolutely certain you incorporate a configurable flag to suppress logging behavior. You can use Hierarchy Custom Setting
, Custom Permission
, etc. The point is, an administrator in your org should be able to turn off this logging at any time without needing a deployment.
You can instantiate any standard exception and get its stack trace. Using regular expressions, you can from there determine the running class and method. I outlined that process here:
Get Currently Executing Class/Method Name?
I would be extremely wary of logging every method call. Each time you log would consume DML Statements, a fairly restrictive governor limit. Even if you cache your logs and attempt to flush the cache as near the end of your transaction as possible, it adds burden to the system which could cause failures. What's worse, LimitException
cannot be caught.
If you insist on incorporating this kind of logging, be absolutely certain you incorporate a configurable flag to suppress logging behavior. You can use Hierarchy Custom Setting
, Custom Permission
, etc. The point is, an administrator in your org should be able to turn off this logging at any time without needing a deployment.
answered 4 hours ago
Adrian Larson♦
104k19111234
104k19111234
add a comment |
add a comment |
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f242294%2fcustom-object-to-hold-log-information%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown