Skip to content

Commit

Permalink
Starting to build a simple logger
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-baillie-ortoo committed Apr 5, 2022
1 parent acdf375 commit 81f916b
Show file tree
Hide file tree
Showing 11 changed files with 374 additions and 15 deletions.
17 changes: 2 additions & 15 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,14 @@ Licenses that are needed with the source code and binary:
* Amoss - https://github.com/bobalicious/amoss/blob/main/LICENSE
* SObject Fabricator - https://github.com/bobalicious/SObjectFabricator/blob/master/LICENSE

Remove the rest of the custom metadata for services

* Move all the Jest LWc tests over to create the component in the before

Look at the use of 'MockDatabase' in fflib
Look at:
SobjectUtils.getSobjectName

* Look at implementing ordering in the datatable example, so you can define the standards more clearly

* Build a filter + results page for Contacts, allowing search on
* Salutation
* Name
* Account Name
* Account Rating


* LWCs
* Standards for:
Expand All @@ -31,14 +25,7 @@ Look at:
* Edit Forms / Validation
* Edit Forms with Child Records

* Try parent and datatable referencing a DML Service


* To finalise the core architecture:
* Do we need to have a non all-or-nothing version of commitWork?

Add to documentation
* Wrapping exceptions on the way out of services
* Query builder - add it to the architectural diagram - after more investigation

DmlServices documentation:
Expand Down
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 );
}
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 framework/default/modules/logger/services/logger/LoggerService.cls
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 );
}
}
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>
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 );
}
}
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>
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@ public class StackTrace {
return Integer.valueOf( stackTraceLine.substringAfter( ', column ') );
}

// TODO: test
public override String toString() {
return getClassName() + '.' + getMethodName() + ' on Line: ' + getLineNumber() + ', Column ' + getColumnNumber();
}

public Boolean getIsCallingAService()
{
return Application.SERVICE.isRegisteredAsAnImplementation( getClassName() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public inherited sharing class ObjectUtils
String result = 'DateTime';
try
{
System.debug( 'If "System.TypeException: Invalid conversion ... to DateTime" is seen in the logs, this may be due to an attempt to find the class name of an object' );
DateTime typeCheck = (DateTime)obj;
}
catch ( System.TypeException te )
Expand Down
124 changes: 124 additions & 0 deletions scripts/apex/illustrateSimpleLogger.apex
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' )
});
}
}
Loading

0 comments on commit 81f916b

Please sign in to comment.