sf-logger is a tool for logging and debugging Apex classes. Easy to use, sf-logger is avaible in two different instances: DebugLogger (writes logs to Debug Logs of your salesforce org) and ApexObjectLogger (writes logs into fields of custom SObject).
Swap logger instances and log levels at any time without need to change your code. Options are based on metadata configuration. Receive notifications by email.
Get logger instance in the beginning of your class using LoggerFactory.cls, like that
private ILogger log = LoggerFactory.getInstance(); Add logger's method calls where you need them in code using corresponding log type and use parameters, based on what info you need to log. See ILogger.cls for parameters reference.
Example:
...
} catch (Exception e) {
log.error(e, "Finding students by group id has failed");
throw new CustomException(e, "Finding students by group id has failed");
}
...Do not use ApexObjectLogger calls in constructor. Salesforce doesn't allow DML inserts in constructor.
You can setup Error Handling Configurations in your org in Custom Metadata Types section.
Examples:
Label: Production
Error Handling Configuration Name: Production
Log Level: Info - Info, Error
Logger: Object Logger
Email on Error: checked
Email Address: user@example.com
Corresponded file - Error_Handling_Configuration.Production.md-meta.xml
Label: Sandbox
Error Handling Configuration Name: Sandbox
Log Level: Finest - Debug, Info, Warning, Error
Logger: No Logs
Email on Error: unchecked
Email Address: user@example.com
Corresponded file - Error_Handling_Configuration.Sandbox.md-meta.xml
Label: Test
Error Handling Configuration Name: Test
Log Level: Fine - Info, Warning, Error
Logger: No Logs
Email on Error: checked
Email Address: user@example.com
Corresponded file - Error_Handling_Configuration.Test.md-meta.xml
Logger that writes logs into Error_Logs__c custom SObject. Before inserting newly created Error_Logs__c, checks whether the current user has permissions to write to fields of Error_Logs__c.
Logger that writes logs into Debug Logs in the org.
Interface on which all logger instances are based.
Methods
Each logger contains four types of methods, based on log type: debug, info, warning and error. Each method reloaded four times, based on parameters that need to be logged.
For example:
void debug(Exception ex, String source, String referenceId, String referenceInfo);
void debug(Exception ex);
void debug(String customMessage, String source, String referenceId, String referenceInfo);
void debug(String customMessage);Abstract class which all logger instances extend.
Methods
public void sendEmail()Sends email notifications to addresses selected in metadata configuration.
Class for creation instances of loggers.
Methods
public static ILogger getInstance()Returns new instance of logger based on metadata configuration.
Logger that makes no logs. Used to disable logging.
Custom metadata that contains logger configuration.
Email_Address__c
Type: email
Contains email address where to send notifications.
Email_on_Error__c
Type: checkbox
Defines whether to send notification by email.
Log_Level__c
Type: picklist
Defines which log levels will be used: Info, Fine or Finest
Log Levels
Info - only Info and Error log-types will be used.
Fine - Info, Warning and Error log-types will be used.
Finest - all log-types: Debug, Info, Warning, Error will be used.
Log Types
Debug - information that can be helpful while debugging (which method is running, variables values, etc).
Info - Generally useful information to log (service run/stop, configuration supposals, etc).
Warning - Any places in code that can potentially cause application malfunction.
Error - Critical events that requires developers attention.
Logger__c
Type: picklist
Defines which logger to use.
Object that contains log information.
Exception_Stack_Trace__c
Type: Long Text Area(32768)
Stack trace of the exception
Exception_Type__c
Type: Text(255)
Type of the exception
Log_Type__c
Type: Picklist
Debug, Info, Warning or Error
Message__c
Type: Long Text Area(32768)
Custom message
ProfileId__c
Type: Text(255)
Id of profile of the current user
Reference_Id__c
Type: Text(18)
The related record id
Reference_Info__c
Type: Text(255)
The related record info (e.g. Apex Batch Job Id, Contact etc)
RoleId__c
Type: Text(255)
Id of the current user's role.
Source__c
Type: Text(255)
Source related to the log, i.e. Class Name
Username__c
Type: Text(255)
Current user's name