Skip to content

Commit

Permalink
Improved logging when failing to generate a logger
Browse files Browse the repository at this point in the history
Application factory uses the logger when trying to log
  • Loading branch information
rob-baillie-ortoo committed Apr 5, 2022
1 parent 7c25516 commit ae77a3e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,17 @@ public virtual class fflib_Application
}

try {
System.debug( System.LoggingLevel.INFO, 'Using default implementation ' + defaultServiceName + ' for ' + serviceInterfaceName );
// TODO: test?
String message = 'Using default implementation ' + defaultServiceName + ' for ' + serviceInterfaceName;
if ( !serviceInterfaceName.endsWith( 'ILoggerService' ) )
{
LoggerService.log( LoggerService.LEVEL.INFO, message );
}
else
{
System.debug( System.LoggingLevel.INFO, message );
}

return defaultServiceType.newInstance();
}
catch ( Exception e ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,23 @@ public inherited sharing class LoggerService

private static ILoggerService service()
{
ILoggerService service;
try
{
return (ILoggerService)Application.SERVICE.newInstance( ILoggerService.class );
service = (ILoggerService)Application.SERVICE?.newInstance( ILoggerService.class );
}
catch ( Exception e )
{
System.debug( LoggingLevel.ERROR, 'Exception when trying to instantiate the logger - ' + e ); // Logger failure should not stop the system,
// so with nothing else available, instantiate a logger that does nothing
return new NullLoggerServiceImpl();
// so with nothing else available, instantiate a logger that does nothing
}

if ( service == null )
{
System.debug( LoggingLevel.WARN, 'Service factory did not build a logger, so NullLoggerServiceImpl is being used - this will not output any logs' );
service = new NullLoggerServiceImpl();
}

return service;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public with sharing class LoggerServiceImpl implements ILoggerService
{
// TODO: should we output more from the standard exceptions?
// TODO: what can we output in the UI? Flag on custom settings to decide
// TODO: search for any System.debug in the code
// TODO: notes on testing if the logger is used - you have to set up the custom metadata first - create a test utils for it

public void log( LoggerService.Level logLevel, String message )
{
Expand Down

0 comments on commit ae77a3e

Please sign in to comment.