forked from apex-enterprise-patterns/fflib-apex-common
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
acdf375
commit 81f916b
Showing
11 changed files
with
374 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
9 changes: 9 additions & 0 deletions
9
framework/default/modules/logger/services/logger/ILoggerService.cls
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
public interface ILoggerService | ||
{ | ||
void log( LoggerService.LEVEL logLevel, String reference, String message ); | ||
void log( LoggerService.LEVEL logLevel, String reference, Id relatedSobjectId, String message ); | ||
void log( String reference, Exception exceptionToLog ); | ||
void log( String reference, Id relatedSobject, Exception exceptionToLog ); | ||
void log( String reference, DmlException exceptionToLog ); | ||
void log( String reference, ortoo_Exception exceptionToLog ); | ||
} |
5 changes: 5 additions & 0 deletions
5
framework/default/modules/logger/services/logger/ILoggerService.cls-meta.xml
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>52.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
47 changes: 47 additions & 0 deletions
47
framework/default/modules/logger/services/logger/LoggerService.cls
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
public inherited sharing class LoggerService | ||
{ | ||
public enum LEVEL { Info, Warning, Error } | ||
|
||
public static void log( LoggerService.LEVEL logLevel, String reference, String message ) | ||
{ | ||
service().log( logLevel, reference, message ); | ||
} | ||
|
||
public static void log( LoggerService.LEVEL logLevel, String reference, Id relatedSobjectId, String message ) | ||
{ | ||
service().log( logLevel, reference, relatedSobjectId, message ); | ||
} | ||
|
||
public static void log( String reference, Exception exceptionToLog ) | ||
{ | ||
if ( exceptionToLog instanceOf DmlException ) | ||
{ | ||
service().log( reference, (DmlException) exceptionToLog ); | ||
} | ||
if ( exceptionToLog instanceOf ortoo_Exception ) | ||
{ | ||
service().log( reference, (ortoo_Exception)exceptionToLog ); | ||
} | ||
service().log( reference, exceptionToLog ); | ||
} | ||
|
||
public static void log( String reference, DmlException exceptionToLog ) | ||
{ | ||
service().log( reference, exceptionToLog ); | ||
} | ||
|
||
public static void log( String reference, ortoo_Exception exceptionToLog ) | ||
{ | ||
service().log( reference, exceptionToLog ); | ||
} | ||
|
||
public static void log( String reference, Id relatedSobjectId, Exception exceptionToLog ) | ||
{ | ||
service().log( reference, relatedSobjectId, exceptionToLog ); | ||
} | ||
|
||
private static ILoggerService service() | ||
{ | ||
return (ILoggerService)Application.SERVICE.newInstance( ILoggerService.class ); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
framework/default/modules/logger/services/logger/LoggerService.cls-meta.xml
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>52.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
77 changes: 77 additions & 0 deletions
77
framework/default/modules/logger/services/simple-logger/SimpleLoggerServiceImpl.cls
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* Provides a mechanism for performing a windowed, ordered search against an object as | ||
* defined by the passed searchConfigurationType. | ||
*/ | ||
public with sharing class SimpleLoggerServiceImpl implements ILoggerService | ||
{ | ||
// TODO: add a stack trace in there | ||
// TODO: add logging level to custom settings - implement choice of log into static methods | ||
// TODO: should we have a custom metadata record? Or should this be LoggerServiceImpl? | ||
// TODO: add resolving of the correct method to call in the static methods? | ||
// TODO: should we output more from the standard exceptions? | ||
// TODO: what can we output in the UI? Flag on custom settings to decide | ||
// | ||
public void log( LoggerService.LEVEL logLevel, String reference, String message ) | ||
{ | ||
logMessage( logLevel, reference, message ); | ||
} | ||
|
||
public void log( LoggerService.LEVEL logLevel, String reference, Id relatedSobjectId, String message ) | ||
{ | ||
logMessage( logLevel, reference, relatedSobjectId, message ); | ||
} | ||
|
||
public void log( String reference, Exception exceptionToLog ) | ||
{ | ||
logMessage( LoggerService.LEVEL.Error, reference, ObjectUtils.getClassName( exceptionToLog ) + ': ' + exceptionToLog.getMessage() ); | ||
logStackTrace( LoggerService.LEVEL.Error, reference, exceptionToLog.getStackTraceString() ); | ||
} | ||
|
||
public void log( String reference, DmlException exceptionToLog ) | ||
{ | ||
logMessage( LoggerService.LEVEL.Error, reference, ObjectUtils.getClassName( exceptionToLog ) + ': ' + exceptionToLog.getDmlId(0), exceptionToLog.getMessage() ); | ||
logStackTrace( LoggerService.LEVEL.Error, reference, exceptionToLog.getStackTraceString() ); | ||
} | ||
|
||
public void log( String reference, ortoo_Exception exceptionToLog ) | ||
{ | ||
logMessage( LoggerService.LEVEL.Error, reference, ObjectUtils.getClassName( exceptionToLog ) + ' - ' + exceptionToLog.getErrorCode() + ': ' + exceptionToLog.getMessage() ); | ||
|
||
ortoo_Exception.Contexts contexts = exceptionToLog.getContexts(); | ||
while ( contexts.hasNext() ) | ||
{ | ||
ortoo_Exception.Context thisContext = contexts.next(); | ||
// Put this into toString on Context? | ||
logMessage( LoggerService.LEVEL.Error, reference + '.context', thisContext.getName() + ' = ' + thisContext.getValue() + ' @ ' + thisContext.getRecordPoint() ); | ||
} | ||
|
||
for ( MessageDetail thisMessageDetail : exceptionToLog.getMessageDetails() ) | ||
{ | ||
// Put this into toString on MessageDetail? | ||
logMessage( LoggerService.LEVEL.Error, reference + '.messageDetail', thisMessageDetail.getFieldContext() + ' => "' + thisMessageDetail.getContent() + '" on ' + thisMessageDetail.getObjectContext() ); | ||
} | ||
|
||
logStackTrace( LoggerService.LEVEL.Error, reference, exceptionToLog.getStackTraceString() ); | ||
} | ||
|
||
public void log( String reference, Id relatedSobjectId, Exception exceptionToLog ) | ||
{ | ||
logMessage( LoggerService.LEVEL.Error, reference, relatedSobjectId, ObjectUtils.getClassName( exceptionToLog ) + ' - ' + exceptionToLog.getMessage() ); | ||
logStackTrace( LoggerService.LEVEL.Error, reference, exceptionToLog.getStackTraceString() ); | ||
} | ||
|
||
private void logMessage( LoggerService.LEVEL logLevel, String reference, String message ) | ||
{ | ||
System.debug( logLevel + ' - ' + reference + ': ' + message ); | ||
} | ||
|
||
private void logMessage( LoggerService.LEVEL logLevel, String reference, String relatedSobjectId, String message ) | ||
{ | ||
System.debug( logLevel + ' - ' + reference + ': ' + relatedSobjectId + ' - ' + message ); | ||
} | ||
|
||
private void logStackTrace( LoggerService.LEVEL logLevel, String reference, String stackTraceString ) | ||
{ | ||
System.debug( logLevel + ' - ' + reference + '.stackTrace: ' + stackTraceString ); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
framework/default/modules/logger/services/simple-logger/SimpleLoggerServiceImpl.cls-meta.xml
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>52.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
System.debug( 'Simple log messages' ); | ||
System.debug( '' ); | ||
LoggerService.log( LoggerService.LEVEL.Info, 'SimpleLog', 'This is an info message' ); | ||
LoggerService.log( LoggerService.LEVEL.Warning, 'SimpleLog', 'This is a warning message' ); | ||
LoggerService.log( LoggerService.LEVEL.Error, 'SimpleLog', 'This is an error message' ); | ||
|
||
System.debug( '' ); | ||
System.debug( 'Log messages with a related SObject' ); | ||
System.debug( '' ); | ||
LoggerService.log( LoggerService.LEVEL.Info, 'LogWSobj', '0032D00000WOOux', 'This is an info message' ); | ||
LoggerService.log( LoggerService.LEVEL.Warning, 'LogWSobj', '0032D00000WOOux', 'This is a warning message' ); | ||
LoggerService.log( LoggerService.LEVEL.Error, 'LogWSobj', '0032D00000WOOux', 'This is an error message' ); | ||
|
||
System.debug( '' ); | ||
System.debug( 'Log of DML Exception with an Id' ); | ||
System.debug( '' ); | ||
try | ||
{ | ||
update new Contact( Id = '0032D00000WOOux' ); | ||
} | ||
catch ( DmlException e ) | ||
{ | ||
LoggerService.log( 'DmlExLog', e ); | ||
} | ||
|
||
System.debug( '' ); | ||
System.debug( 'Log of DML Exceptionm, handled with Exception with an Id' ); | ||
System.debug( '' ); | ||
try | ||
{ | ||
update new Contact( Id = '0032D00000WOOux' ); | ||
} | ||
catch ( Exception e ) | ||
{ | ||
LoggerService.log( 'DmlExAsExLog', e ); | ||
} | ||
|
||
System.debug( '' ); | ||
System.debug( 'Log of DML Exception without an Id' ); | ||
System.debug( '' ); | ||
try | ||
{ | ||
insert new Contact(); | ||
} | ||
catch ( DmlException e ) | ||
{ | ||
LoggerService.log( 'DmlExLogNoId', e ); | ||
} | ||
|
||
System.debug( '' ); | ||
System.debug( 'Log of DML Exception as Exception without an Id' ); | ||
System.debug( '' ); | ||
try | ||
{ | ||
insert new Contact(); | ||
} | ||
catch ( Exception e ) | ||
{ | ||
LoggerService.log( 'DmlExAsExLogNoId', e ); | ||
} | ||
|
||
System.debug( '' ); | ||
System.debug( 'Log of System Exception without an Id' ); | ||
System.debug( '' ); | ||
try | ||
{ | ||
String value = new List<String>()[1]; | ||
} | ||
catch ( Exception e ) | ||
{ | ||
LoggerService.log( 'SystemEx', e ); | ||
} | ||
|
||
System.debug( '' ); | ||
System.debug( 'Log of System Exception with an Id' ); | ||
System.debug( '' ); | ||
try | ||
{ | ||
String value = new List<String>()[1]; | ||
} | ||
catch ( Exception e ) | ||
{ | ||
LoggerService.log( 'SystemExWithId', '0032D00000WOOux', e ); | ||
} | ||
|
||
System.debug( '' ); | ||
System.debug( 'Log of Ortoo Exception' ); | ||
System.debug( '' ); | ||
try | ||
{ | ||
OrtooExceptionThrower thrower = new OrtooExceptionThrower(); | ||
thrower.throwException(); | ||
} | ||
catch ( ortoo_Exception e ) | ||
{ | ||
LoggerService.log( 'OrtooEx', e ); | ||
} | ||
|
||
System.debug( '' ); | ||
System.debug( 'Log of Ortoo Exception as Exception' ); | ||
System.debug( '' ); | ||
try | ||
{ | ||
OrtooExceptionThrower thrower = new OrtooExceptionThrower(); | ||
thrower.throwException(); | ||
} | ||
catch ( Exception e ) | ||
{ | ||
LoggerService.log( 'OrtooExAsEx', e ); | ||
} | ||
|
||
class OrtooExceptionThrower | ||
{ | ||
public void throwException() | ||
{ | ||
throw new Exceptions.DeveloperException( 'This is a developer exception' ) | ||
.setErrorCode( '12345' ) | ||
.addContext( 'id', '0032D00000WOOux' ) | ||
.addContext( 'otherThing', 'thingvalue' ) | ||
.setMessageDetails( new List<MessageDetail>{ | ||
new MessageDetail( new Contact( LastName = 'thingthatwentwrong' ), Contact.LastName, 'The last name went wrong' ) | ||
}); | ||
} | ||
} |
Oops, something went wrong.