Skip to content

Commit

Permalink
Initial changes for #235 - Added new CMDT LoggerScenarioRule__mdt to …
Browse files Browse the repository at this point in the history
…override the user's logging level for a particular scenario
  • Loading branch information
jongpie committed Oct 26, 2021
1 parent ab7dc20 commit 4c629e5
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomMetadata xmlns="http://soap.sforce.com/2006/04/metadata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<label>some scenario</label>
<protected>false</protected>
<values>
<field>UserLoggingLevelOverride__c</field>
<value xsi:type="xsd:string">INFO</value>
</values>
</CustomMetadata>
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<Layout xmlns="http://soap.sforce.com/2006/04/metadata">
<layoutSections>
<customLabel>false</customLabel>
<detailHeading>true</detailHeading>
<editHeading>true</editHeading>
<label>Information</label>
<layoutColumns>
<layoutItems>
<behavior>Required</behavior>
<field>MasterLabel</field>
</layoutItems>
<layoutItems>
<behavior>Required</behavior>
<field>DeveloperName</field>
</layoutItems>
</layoutColumns>
<layoutColumns>
<layoutItems>
<behavior>Edit</behavior>
<field>IsEnabled__c</field>
</layoutItems>
<layoutItems>
<behavior>Required</behavior>
<field>UserLoggingLevelOverride__c</field>
</layoutItems>
</layoutColumns>
<style>TwoColumnsTopToBottom</style>
</layoutSections>
<layoutSections>
<customLabel>false</customLabel>
<detailHeading>true</detailHeading>
<editHeading>true</editHeading>
<label>System Information</label>
<layoutColumns>
<layoutItems>
<behavior>Required</behavior>
<field>NamespacePrefix</field>
</layoutItems>
<layoutItems>
<behavior>Readonly</behavior>
<field>CreatedById</field>
</layoutItems>
</layoutColumns>
<layoutColumns>
<layoutItems>
<behavior>Edit</behavior>
<field>IsProtected</field>
</layoutItems>
<layoutItems>
<behavior>Readonly</behavior>
<field>LastModifiedById</field>
</layoutItems>
</layoutColumns>
<style>TwoColumnsTopToBottom</style>
</layoutSections>
<layoutSections>
<customLabel>true</customLabel>
<detailHeading>true</detailHeading>
<editHeading>false</editHeading>
<label>Custom Links</label>
<layoutColumns/>
<layoutColumns/>
<layoutColumns/>
<style>CustomLinks</style>
</layoutSections>
<showEmailCheckbox>false</showEmailCheckbox>
<showHighlightsPanel>false</showHighlightsPanel>
<showInteractionLogPanel>false</showInteractionLogPanel>
<showRunAssignmentRulesCheckbox>false</showRunAssignmentRulesCheckbox>
<showSubmitAndAttachButton>false</showSubmitAndAttachButton>
<summaryLayout>
<masterLabel>00h17000007yeDt</masterLabel>
<sizeX>4</sizeX>
<sizeY>0</sizeY>
<summaryLayoutStyle>Default</summaryLayoutStyle>
</summaryLayout>
</Layout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
<label>Logger Scenario Rule</label>
<pluralLabel>Logger Scenario Rules</pluralLabel>
<visibility>Public</visibility>
</CustomObject>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>IsEnabled__c</fullName>
<defaultValue>true</defaultValue>
<externalId>false</externalId>
<fieldManageability>SubscriberControlled</fieldManageability>
<label>Is Enabled</label>
<type>Checkbox</type>
</CustomField>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>UserLoggingLevelOverride__c</fullName>
<externalId>false</externalId>
<fieldManageability>SubscriberControlled</fieldManageability>
<inlineHelpText>Overrides the user&apos;s logging level (normally controlled via LoggerSettings__c.LoggingLevel__c) for any transactions with the specified Scenario</inlineHelpText>
<label>User Logging Level Override</label>
<required>true</required>
<type>Picklist</type>
<valueSet>
<restricted>true</restricted>
<valueSetDefinition>
<sorted>false</sorted>
<value>
<fullName>ERROR</fullName>
<default>true</default>
<label>ERROR</label>
</value>
<value>
<fullName>WARN</fullName>
<default>false</default>
<label>WARN</label>
</value>
<value>
<fullName>INFO</fullName>
<default>false</default>
<label>INFO</label>
</value>
<value>
<fullName>DEBUG</fullName>
<default>false</default>
<label>DEBUG</label>
</value>
<value>
<fullName>FINE</fullName>
<default>false</default>
<label>FINE</label>
</value>
<value>
<fullName>FINER</fullName>
<default>false</default>
<label>FINER</label>
</value>
<value>
<fullName>FINEST</fullName>
<default>false</default>
<label>FINEST</label>
</value>
</valueSetDefinition>
</valueSet>
</CustomField>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<ListView xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>All</fullName>
<columns>MasterLabel</columns>
<columns>DeveloperName</columns>
<columns>IsEnabled__c</columns>
<columns>UserLoggingLevelOverride__c</columns>
<filterScope>Everything</filterScope>
<label>All</label>
</ListView>
27 changes: 26 additions & 1 deletion nebula-logger/main/logger-engine/classes/Logger.cls
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ global with sharing class Logger {
private static final String CURRENT_VERSION_NUMBER = 'v4.6.11';
private static final LoggingLevel DEFAULT_LOGGING_LEVEL = LoggingLevel.DEBUG;
private static final List<LogEntryEventBuilder> LOG_ENTRIES_BUFFER = new List<LogEntryEventBuilder>();
@TestVisible
private static final Map<String, LoggerScenarioRule__mdt> SCENARIO_TO_MOCK_SCENARIO_RULE = new Map<String, LoggerScenarioRule__mdt>();
private static final String TRANSACTION_ID = setTransactionId();
private static final Quiddity TRANSACTION_QUIDDITY = setTransactionQuiddity();

Expand Down Expand Up @@ -2451,8 +2453,31 @@ global with sharing class Logger {
* and `Log__c.Scenario__c`, and can be used to filter & group logs
* @param scenario The name to use for the current transaction's scenario
*/
public static void setScenario(String scenario) {
global static void setScenario(String scenario) {
transactionScenario = scenario;

// LoggerScenarioRule__mdt scenarioRule = LoggerScenarioRule__mdt.getInstance(scenario);
LoggerScenarioRule__mdt matchingScenarioRule;
for (LoggerScenarioRule__mdt scenarioRule : [
SELECT UserLoggingLevelOverride__c
FROM LoggerScenarioRule__mdt
WHERE IsEnabled__c = TRUE AND Scenario__c = :scenario
]) {
matchingScenarioRule = scenarioRule;
}

if (Test.isRunningTest() == true) {
matchingScenarioRule = SCENARIO_TO_MOCK_SCENARIO_RULE.get(scenario);
}

if (matchingScenarioRule != null) {
getUserSettings().LoggingLevel__c = matchingScenarioRule.UserLoggingLevelOverride__c;
}
}

@TestVisible
private static void setMockScenarioRule(LoggerScenarioRule__mdt scenarioRule) {
SCENARIO_TO_MOCK_SCENARIO_RULE.put(scenarioRule.Scenario__c, scenarioRule);
}

/**
Expand Down
27 changes: 27 additions & 0 deletions nebula-logger/tests/logger-engine/classes/Logger_Tests.cls
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,33 @@ private class Logger_Tests {
System.assertEquals(transactionScenarioName, log.Scenario__c, 'Scenario__c was not properly set on the Log__c record');
}

@IsTest
static void it_should_set_scenario_and_apply_scenario_rule_logging_level() {
String transactionId = Logger.getTransactionId();
String transactionScenarioName = 'some test scenario for this transaction';
LoggerScenarioRule__mdt scenarioRule = new LoggerScenarioRule__mdt(
Scenario__c = transactionScenarioName,
UserLoggingLevelOverride__c = LoggingLevel.FINER.name()
);
Logger.setMockScenarioRule(scenarioRule);
Logger.getUserSettings().LoggingLevel__c = LoggingLevel.ERROR.name();
System.assertNotEquals(Logger.getUserSettings().LoggingLevel__c, scenarioRule.UserLoggingLevelOverride__c);

Logger.setScenario(transactionScenarioName);
for (Integer i = 0; i < 5; i++) {
Logger.finer('my log entry');
}
Logger.saveLog();
Test.getEventBus().deliver();

Log__c log = [SELECT Id, Scenario__c FROM Log__c WHERE TransactionId__c = :transactionId];
System.assertEquals(transactionScenarioName, log.Scenario__c, 'Scenario__c was not properly set on the Log__c record');
List<LogEntry__c> logEntries = [SELECT Id, LoggingLevel__c FROM LogEntry__c WHERE Log__c = :log.Id];
for (LogEntry__c logEntry : logEntries) {
System.assertEquals(LoggingLevel.FINER.name(), logEntry.LoggingLevel__c, 'All log entries should have FINER as the logging level');
}
}

@IsTest
static void it_should_use_last_scenario_when_multiple_scenarios_specified() {
String transactionId = Logger.getTransactionId();
Expand Down

0 comments on commit 4c629e5

Please sign in to comment.