Skip to content

Commit

Permalink
Set the default logging level in the logging service implementation
Browse files Browse the repository at this point in the history
Reduced WARNING to WARN in order to match System.debug
Added test for the null logger
  • Loading branch information
rob-baillie-ortoo committed Apr 6, 2022
1 parent ae77a3e commit 53caeec
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 19 deletions.
5 changes: 4 additions & 1 deletion TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ 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
Docs for Logger:
* Custom Setting - if it doesn't exist, will log everything.



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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,9 @@ public virtual class fflib_Application

try {
// 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 );
LoggerService.log( LoggerService.LEVEL.INFO, 'Using default implementation ' + defaultServiceName + ' for ' + serviceInterfaceName );
}

return defaultServiceType.newInstance();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
public interface ILoggerService
{
Boolean getDefaultLoggingEnabled( LoggerService.Level logLevel );
void log( LoggerService.Level logLevel, String message );
void log( LoggerService.Level logLevel, String message, Id relatedSobjectId );
void log( Exception exceptionToLog );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
public inherited sharing class LoggerService
{
public enum Level { INFO, WARNING, ERROR }
public enum Level { INFO, WARN, ERROR }

public static final Level EXCEPTION_LOG_LEVEL = Level.ERROR;

Expand Down Expand Up @@ -175,15 +175,16 @@ public inherited sharing class LoggerService

private static Boolean shouldLog( LoggerService.Level logLevel )
{
// If no config record exists, then get the whether the logging should be on from the service
switch on logLevel {
when INFO {
return config.Name != null ? config.Log_INFO_Level_Messages__c : false; // if no config record exists, do not log INFO
return config.Name != null ? config.Log_INFO_Level_Messages__c : service().getDefaultLoggingEnabled( logLevel );
}
when WARNING {
return config.Name != null ? config.Log_WARNING_Level_Messages__c : false; // if no config record exists, do not log WARNING
when WARN {
return config.Name != null ? config.Log_WARN_Level_Messages__c : service().getDefaultLoggingEnabled( logLevel );
}
}
return config.Name != null ? config.Log_ERROR_Level_Messages__c : true; // if no config record exists, still log ERROR
return config.Name != null ? config.Log_ERROR_Level_Messages__c : service().getDefaultLoggingEnabled( logLevel );
}

private static Boolean shouldLogExceptions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public with sharing class LoggerServiceImpl implements ILoggerService
// 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 Boolean getDefaultLoggingEnabled( LoggerService.Level logLevel )
{
return true;
}

public void log( LoggerService.Level logLevel, String message )
{
logMessage( logLevel, message );
Expand Down Expand Up @@ -113,7 +118,7 @@ public with sharing class LoggerServiceImpl implements ILoggerService
{
return LoggingLevel.INFO;
}
when WARNING
when WARN
{
return LoggingLevel.WARN;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
*/
public inherited sharing class NullLoggerServiceImpl implements ILoggerService
{
public Boolean getDefaultLoggingEnabled( LoggerService.Level logLevel )
{
return false;
}
public void log( LoggerService.Level logLevel, String message ) {} // NOPMD: is a null implementation
public void log( LoggerService.Level logLevel, String message, Id relatedSobjectId ) {} // NOPMD: is a null implementation
public void log( Exception exceptionToLog ) {} // NOPMD: is a null implementation
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
@isTest
private without sharing class NullLoggerServiceImplTest
{
@isTest
private static void getDefaultLoggingEnabled_returnsFalse() // NOPMD: Test method name format
{
ILoggerService loggerServiceImpl = new NullLoggerServiceImpl();

System.assertEquals( false, loggerServiceImpl.getDefaultLoggingEnabled( LoggerService.Level.ERROR ), 'getDefaultLoggingEnabled, when called with ERROR, returns false' );
System.assertEquals( false, loggerServiceImpl.getDefaultLoggingEnabled( LoggerService.Level.WARN ), 'getDefaultLoggingEnabled, when called with WARN, returns false' );
System.assertEquals( false, loggerServiceImpl.getDefaultLoggingEnabled( LoggerService.Level.INFO ), 'getDefaultLoggingEnabled, when called with INFO, returns false' );
}

@isTest
private static void log_whenGivenLogLevelAndMessage_doNothing() // NOPMD: Test method name format
{
Application.SERVICE.setMock( ILoggerService.class, new NullLoggerServiceImpl() );

LoggerService.Level logLevel = null;
String message = null;

Test.startTest();
LoggerService.log( logLevel, message );
Test.stopTest();

System.assert( true, 'log, when given a log level and message, will do nothing' );
}

@isTest
private static void log_whenGivenLogLevelMessageAndId_doNothing() // NOPMD: Test method name format
{
Application.SERVICE.setMock( ILoggerService.class, new NullLoggerServiceImpl() );

LoggerService.Level logLevel = null;
String message = null;
Id sobjectId = null;

Test.startTest();
LoggerService.log( logLevel, message, sobjectId );
Test.stopTest();

System.assert( true, 'log, when given a log level, message and sobject Id, will do nothing' );
}

@isTest
private static void log_whenGivenAnException_doNothing() // NOPMD: Test method name format
{
Application.SERVICE.setMock( ILoggerService.class, new NullLoggerServiceImpl() );

Exception e = null;

Test.startTest();
LoggerService.log( e );
Test.stopTest();

System.assert( true, 'log, when given an exception, will do nothing' );
}

@isTest
private static void log_whenGivenAnExceptionAndAnId_doNothing() // NOPMD: Test method name format
{
Application.SERVICE.setMock( ILoggerService.class, new NullLoggerServiceImpl() );

Exception e = null;
Id sobjectId = null;

Test.startTest();
LoggerService.log( e, sobjectId );
Test.stopTest();

System.assert( true, 'log, when given an exception and an sobject Id, will do nothing' );
}

@isTest
private static void log_whenGivenADmlExceptionAndAnId_doNothing() // NOPMD: Test method name format
{
Application.SERVICE.setMock( ILoggerService.class, new NullLoggerServiceImpl() );

DmlException e = null;
Id sobjectId = null;

Test.startTest();
LoggerService.log( e, sobjectId );
Test.stopTest();

System.assert( true, 'log, when given a dml exception and an sobject Id, will do nothing' );
}

@isTest
private static void log_whenGivenAnOrtooExceptionAndAnId_doNothing() // NOPMD: Test method name format
{
Application.SERVICE.setMock( ILoggerService.class, new NullLoggerServiceImpl() );

ortoo_Exception e = null;
Id sobjectId = null;

Test.startTest();
LoggerService.log( e, sobjectId );
Test.stopTest();

System.assert( true, 'log, when given an ortoo exception and an sobject Id, will do nothing' );
}
}
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>54.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Log_WARNING_Level_Messages__c</fullName>
<fullName>Log_WARN_Level_Messages__c</fullName>
<defaultValue>false</defaultValue>
<description>Should messages with the level of &apos;WARNING&apos; be logged?</description>
<description>Should messages with the level of &apos;WARN&apos; be logged?</description>
<externalId>false</externalId>
<inlineHelpText>Should messages with the level of &apos;WARNING&apos; be logged?</inlineHelpText>
<label>Log WARNING Level Messages</label>
<inlineHelpText>Should messages with the level of &apos;WARN&apos; be logged?</inlineHelpText>
<label>Log WARN Level Messages</label>
<trackTrending>false</trackTrending>
<type>Checkbox</type>
</CustomField>
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
public with sharing class PermissionsServiceImpl implements IPermissionsService
{
/**
* States if the user has the custom permission required to acces the core platform cache
*
* @return Boolean Does the current user have the platform cache
*/
public Boolean hasAccessToCorePlatformCache()
{
return hasCustomPermission( 'ProcessesCanAccessCache' );
Expand Down
4 changes: 2 additions & 2 deletions scripts/apex/illustrateSimpleLogger.apex
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ Id id = '0032D00000WOOux';
System.debug( 'Simple log messages' );
System.debug( '' );
LoggerService.log( LoggerService.Level.INFO, 'This is an info message' );
LoggerService.log( LoggerService.Level.WARNING, 'This is a warning message' );
LoggerService.log( LoggerService.Level.WARN, 'This is a warning message' );
LoggerService.log( LoggerService.Level.ERROR, 'This is an error message' );

System.debug( '' );
System.debug( 'Log messages with a related SObject' );
System.debug( '' );
LoggerService.log( LoggerService.Level.INFO, 'This is an info message', id );
LoggerService.log( LoggerService.Level.WARNING, 'This is a warning message', id );
LoggerService.log( LoggerService.Level.WARN, 'This is a warning message', id );
LoggerService.log( LoggerService.Level.ERROR, 'This is an error message', id );

System.debug( '' );
Expand Down

0 comments on commit 53caeec

Please sign in to comment.