diff --git a/nebula-logger/core/main/logger-engine/classes/LogEntryEventBuilder.cls b/nebula-logger/core/main/logger-engine/classes/LogEntryEventBuilder.cls index ef5a8f332..3a963f3f0 100644 --- a/nebula-logger/core/main/logger-engine/classes/LogEntryEventBuilder.cls +++ b/nebula-logger/core/main/logger-engine/classes/LogEntryEventBuilder.cls @@ -32,6 +32,10 @@ global with sharing class LogEntryEventBuilder { @TestVisible private String debugMessage = ''; + + @TestVisible + private static Boolean isApexSystemDebugSupportedOverride = true; // default to true for backwards compatibility with existing tests + private Boolean detailsAreSet = false; private Boolean shouldSave; private Set tags = new Set(); @@ -774,8 +778,10 @@ global with sharing class LogEntryEventBuilder { try { this.logEntryEvent.put(field, value); } catch (System.Exception ex) { - LogMessage logMessage = new LogMessage('Could not set field {0} with value {1}', field, value); - System.debug(System.LoggingLevel.WARN, logMessage.getMessage()); + if (isApexSystemDebugSupported()) { + LogMessage logMessage = new LogMessage('Could not set field {0} with value {1}', field, value); + System.debug(System.LoggingLevel.WARN, logMessage.getMessage()); + } } } @@ -922,7 +928,7 @@ global with sharing class LogEntryEventBuilder { @SuppressWarnings('PMD.AvoidDebugStatements') private void logToApexDebug(String message) { - if (this.userSettings.IsApexSystemDebugLoggingEnabled__c == false) { + if (this.userSettings.IsApexSystemDebugLoggingEnabled__c == false || isApexSystemDebugSupported() == false) { return; } @@ -1340,6 +1346,17 @@ global with sharing class LogEntryEventBuilder { return securityDecision.getRecords(); } + /** + * @description Returns whether Apex System.debug() is supported, supports an override for testing purposes. + * @return `Boolean` true if Apex System.debug() is supported, false otherwise + */ + private static Boolean isApexSystemDebugSupported() { + if (System.Test.isRunningTest()) { + return isApexSystemDebugSupportedOverride; + } + return String.isBlank(NAMESPACE_PREFIX); + } + @SuppressWarnings('PMD.ApexDoc') public class LoggingContext { public String currentEntryScenario; diff --git a/nebula-logger/core/tests/logger-engine/classes/LogEntryEventBuilder_Tests.cls b/nebula-logger/core/tests/logger-engine/classes/LogEntryEventBuilder_Tests.cls index 1c9ad3387..8b234c9b3 100644 --- a/nebula-logger/core/tests/logger-engine/classes/LogEntryEventBuilder_Tests.cls +++ b/nebula-logger/core/tests/logger-engine/classes/LogEntryEventBuilder_Tests.cls @@ -2496,6 +2496,44 @@ private class LogEntryEventBuilder_Tests { System.Assert.isNull(builder.getLogEntryEvent().get(datetimeField)); } + @IsTest + static void it_should_skip_apex_system_debug_when_not_supported() { + Test.startTest(); + LogEntryEventBuilder.isApexSystemDebugSupportedOverride = false; + DebugStringExample example = new DebugStringExample(); + LoggerSettings__c userSettings = getUserSettings(); + userSettings.IsApexSystemDebugLoggingEnabled__c = true; + LogEntryEventBuilder builder = example.myMethod(userSettings); + Test.stopTest(); + + System.Assert.areEqual( + '', + builder.debugMessage, + 'Debug message should be empty when Apex System.debug() is not supported, ie: executed in Managed Package context' + ); + System.Assert.areEqual(example.loggingString, builder.getLogEntryEvent().Message__c, 'Message should be set on the log entry event'); + } + + @IsTest + static void it_should_use_apex_system_debug_when_supported() { + Test.startTest(); + LoggerParameter.setMock( + new LoggerParameter__mdt(DeveloperName = 'SystemDebugMessageFormat', Value__c = '{OriginLocation__c}\n{Message__c}: {LoggingLevel__c}') + ); + + DebugStringExample example = new DebugStringExample(); + LoggerSettings__c userSettings = getUserSettings(); + userSettings.IsApexSystemDebugLoggingEnabled__c = true; + LogEntryEventBuilder builder = example.myMethod(userSettings); + Test.stopTest(); + + System.Assert.areEqual( + DebugStringExample.class.getName() + '.myMethod' + '\n' + example.loggingString + ': ' + System.LoggingLevel.DEBUG.name(), + builder.debugMessage, + 'Debug message should be set when Apex System.debug() is supported, ie: executed in Unmanaged Package context and IsApexSystemDebugLoggingEnabled__c is true' + ); + } + @IsTest static void it_should_use_configured_log_entry_event_fields_for_debug_string() { // Don't bother testing stack trace logic when using a namespace prefix - there are