Skip to content

Commit

Permalink
Added 'transaction entry number' for log entry (#127)
Browse files Browse the repository at this point in the history
* Added log entry field TransactionEntryNumber__c  (populated by Logger)
  • Loading branch information
jongpie authored Apr 2, 2021
1 parent e73942d commit 0d5b6db
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public without sharing class LogEntryEventHandler {
RecordSObjectTypeNamespace__c = logEntryEvent.RecordSObjectTypeNamespace__c,
StackTrace__c = logEntryEvent.StackTrace__c,
Timestamp__c = timestamp,
TransactionEntryNumber__c = logEntryEvent.TransactionEntryNumber__c,
TriggerIsExecuting__c = logEntryEvent.TriggerIsExecuting__c,
TriggerOperationType__c = logEntryEvent.TriggerOperationType__c,
TriggerSObjectType__c = logEntryEvent.TriggerSObjectType__c
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@
<fieldItem>Record.Log__c</fieldItem>
</fieldInstance>
</itemInstances>
<itemInstances>
<fieldInstance>
<fieldInstanceProperties>
<name>uiBehavior</name>
<value>readonly</value>
</fieldInstanceProperties>
<fieldItem>Record.TransactionEntryNumber__c</fieldItem>
</fieldInstance>
</itemInstances>
<itemInstances>
<fieldInstance>
<fieldInstanceProperties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<behavior>Required</behavior>
<field>Log__c</field>
</layoutItems>
<layoutItems>
<behavior>Readonly</behavior>
<field>TransactionEntryNumber__c</field>
</layoutItems>
<layoutItems>
<behavior>Required</behavior>
<field>Name</field>
Expand Down Expand Up @@ -246,7 +250,7 @@
<showRunAssignmentRulesCheckbox>false</showRunAssignmentRulesCheckbox>
<showSubmitAndAttachButton>false</showSubmitAndAttachButton>
<summaryLayout>
<masterLabel>00h1g000002BBzm</masterLabel>
<masterLabel>00h1D000003W7GS</masterLabel>
<sizeX>4</sizeX>
<sizeY>0</sizeY>
<summaryLayoutStyle>Default</summaryLayoutStyle>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>TransactionEntryNumber__c</fullName>
<description>The sequential number of this log entry within the transaction</description>
<externalId>false</externalId>
<inlineHelpText>The sequential number of this log entry within the transaction</inlineHelpText>
<label>Transaction Entry Number</label>
<precision>10</precision>
<required>false</required>
<scale>0</scale>
<trackFeedHistory>false</trackFeedHistory>
<trackTrending>false</trackTrending>
<type>Number</type>
<unique>false</unique>
</CustomField>
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,11 @@
<field>LogEntry__c.Timestamp__c</field>
<readable>true</readable>
</fieldPermissions>
<fieldPermissions>
<editable>false</editable>
<field>LogEntry__c.TransactionEntryNumber__c</field>
<readable>true</readable>
</fieldPermissions>
<fieldPermissions>
<editable>false</editable>
<field>LogEntry__c.TriggerIsExecuting__c</field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@
<field>LogEntry__c.Timestamp__c</field>
<readable>true</readable>
</fieldPermissions>
<fieldPermissions>
<editable>false</editable>
<field>LogEntry__c.TransactionEntryNumber__c</field>
<readable>true</readable>
</fieldPermissions>
<fieldPermissions>
<editable>false</editable>
<field>LogEntry__c.TriggerIsExecuting__c</field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,11 @@
<field>LogEntry__c.Timestamp__c</field>
<readable>true</readable>
</fieldPermissions>
<fieldPermissions>
<editable>false</editable>
<field>LogEntry__c.TransactionEntryNumber__c</field>
<readable>true</readable>
</fieldPermissions>
<fieldPermissions>
<editable>false</editable>
<field>LogEntry__c.TriggerIsExecuting__c</field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,11 @@
<field>LogEntry__c.Timestamp__c</field>
<readable>true</readable>
</fieldPermissions>
<fieldPermissions>
<editable>false</editable>
<field>LogEntry__c.TransactionEntryNumber__c</field>
<readable>true</readable>
</fieldPermissions>
<fieldPermissions>
<editable>false</editable>
<field>LogEntry__c.TriggerIsExecuting__c</field>
Expand Down
5 changes: 3 additions & 2 deletions nebula-logger/main/logger-engine/classes/Logger.cls
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ global with sharing class Logger {
private static final Boolean USER_HAS_LOGGING_ENABLED = LoggerSettings__c.getInstance().IsEnabled__c;
private static final LoggingLevel USER_LOGGING_LEVEL = getLoggingLevel(LoggerSettings__c.getInstance().LoggingLevel__c);

private static Integer currentTransactionEntryNumber = 1;
private static String parentLogTransactionId;
private static Boolean suspendSaving = false;
@testVisible
Expand Down Expand Up @@ -4896,8 +4897,8 @@ global with sharing class Logger {
shouldSave = shouldSave && USER_CAN_CREATE_LOG_ENTRY_EVENTS;

LogEntryEventBuilder logEntryEventBuilder = new LogEntryEventBuilder(LoggingLevel, shouldSave);

if (logEntryEventBuilder.shouldSave() == true) {
logEntryEventBuilder.getLogEntryEvent().TransactionEntryNumber__c = currentTransactionEntryNumber++;
LOG_ENTRIES_BUFFER.add(logEntryEventBuilder);
}

Expand Down Expand Up @@ -4933,7 +4934,7 @@ global with sharing class Logger {
when ANONYMOUS {
savingLogMessage += 'Anonymous Apex';
}
when Aura, VF {
when AURA, VF {
savingLogMessage += 'Aura/Lightning/VF component';
}
when BATCH_ACS, BATCH_APEX, BATCH_CHUNK_PARALLEL, BATCH_CHUNK_SERIAL {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>TransactionEntryNumber__c</fullName>
<description>The sequential number of this log entry within the transaction</description>
<externalId>false</externalId>
<isFilteringDisabled>false</isFilteringDisabled>
<isNameField>false</isNameField>
<isSortingDisabled>false</isSortingDisabled>
<label>Transaction Entry Number</label>
<precision>10</precision>
<required>true</required>
<scale>0</scale>
<type>Number</type>
<unique>false</unique>
</CustomField>
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ private class LogEntryEventHandler_Tests {
RecordSObjectTypeNamespace__c,
StackTrace__c,
Timestamp__c,
TransactionEntryNumber__c,
TriggerIsExecuting__c,
TriggerOperationType__c,
TriggerSObjectType__c
Expand Down Expand Up @@ -252,6 +253,7 @@ private class LogEntryEventHandler_Tests {
System.assertEquals(logEntryEvent.RecordSObjectTypeNamespace__c, logEntry.RecordSObjectTypeNamespace__c);
System.assertEquals(logEntryEvent.StackTrace__c, logEntry.StackTrace__c);
System.assertEquals(logEntryEvent.Timestamp__c, logEntry.Timestamp__c);
System.assertEquals(logEntryEvent.TransactionEntryNumber__c, logEntry.TransactionEntryNumber__c);
System.assertEquals(logEntryEvent.TriggerIsExecuting__c, logEntry.TriggerIsExecuting__c);
System.assertEquals(logEntryEvent.TriggerOperationType__c, logEntry.TriggerOperationType__c);
System.assertEquals(logEntryEvent.TriggerSObjectType__c, logEntry.TriggerSObjectType__c);
Expand All @@ -261,7 +263,12 @@ private class LogEntryEventHandler_Tests {
static void it_should_normalize_simple_event_data_into_log_and_log_entry() {
String transactionId = '123-456-789-0';

LogEntryEvent__e logEntryEvent = new LogEntryEvent__e(Message__c = 'my message', Timestamp__c = System.now(), TransactionId__c = transactionId);
LogEntryEvent__e logEntryEvent = new LogEntryEvent__e(
Message__c = 'my message',
Timestamp__c = System.now(),
TransactionEntryNumber__c = 1,
TransactionId__c = transactionId
);

Test.startTest();
Database.SaveResult saveResult = EventBus.publish(logEntryEvent);
Expand Down Expand Up @@ -345,6 +352,7 @@ private class LogEntryEventHandler_Tests {
Timestamp__c = System.now(),
TimeZoneId__c = 'fake-id',
TimeZoneName__c = 'My time zone name',
TransactionEntryNumber__c = 1,
TransactionId__c = transactionId,
TriggerIsExecuting__c = false,
TriggerOperationType__c = 'SomeOperation',
Expand Down Expand Up @@ -379,6 +387,7 @@ private class LogEntryEventHandler_Tests {
LogEntryEvent__e logEntryEvent = new LogEntryEvent__e(
Message__c = 'my message',
Timestamp__c = System.now(),
TransactionEntryNumber__c = i,
TransactionId__c = transactionIds.get(i)
);

Expand Down Expand Up @@ -417,6 +426,7 @@ private class LogEntryEventHandler_Tests {
Message__c = 'my message',
Timestamp__c = System.now(),
Topics__c = String.join(topics, ','),
TransactionEntryNumber__c = 1,
TransactionId__c = transactionId
);

Expand Down Expand Up @@ -451,7 +461,12 @@ private class LogEntryEventHandler_Tests {
upsert orgDefaults;

String transactionId = '123-456-789-0';
LogEntryEvent__e logEntryEvent = new LogEntryEvent__e(Message__c = 'my message', Timestamp__c = System.now(), TransactionId__c = transactionId);
LogEntryEvent__e logEntryEvent = new LogEntryEvent__e(
Message__c = 'my message',
Timestamp__c = System.now(),
TransactionEntryNumber__c = 1,
TransactionId__c = transactionId
);

Database.SaveResult saveResult;

Expand All @@ -471,7 +486,6 @@ private class LogEntryEventHandler_Tests {
System.assertEquals(1, log.LogEntries__r.size());
LogEntry__c logEntry = log.LogEntries__r.get(0);

System.assertEquals(logEntryEvent.TransactionId__c, log.TransactionId__c);
System.assertEquals(MOCK_RELEASE_NUMBER, log.ApiReleaseNumber__c);
System.assertEquals(MOCK_RELEASE_VERSION, log.ApiReleaseVersion__c);
validateLogFields(logEntryEvent, log);
Expand All @@ -488,7 +502,12 @@ private class LogEntryEventHandler_Tests {
upsert orgDefaults;

String transactionId = '123-456-789-0';
LogEntryEvent__e logEntryEvent = new LogEntryEvent__e(Message__c = 'my message', Timestamp__c = System.now(), TransactionId__c = transactionId);
LogEntryEvent__e logEntryEvent = new LogEntryEvent__e(
Message__c = 'my message',
Timestamp__c = System.now(),
TransactionEntryNumber__c = 1,
TransactionId__c = transactionId
);

Database.SaveResult saveResult;

Expand Down Expand Up @@ -527,7 +546,12 @@ private class LogEntryEventHandler_Tests {
upsert orgDefaults;

String transactionId = '123-456-789-0';
LogEntryEvent__e logEntryEvent = new LogEntryEvent__e(Message__c = 'my message', Timestamp__c = System.now(), TransactionId__c = transactionId);
LogEntryEvent__e logEntryEvent = new LogEntryEvent__e(
Message__c = 'my message',
Timestamp__c = System.now(),
TransactionEntryNumber__c = 1,
TransactionId__c = transactionId
);

Database.SaveResult saveResult;
try {
Expand Down
12 changes: 11 additions & 1 deletion nebula-logger/tests/logger-engine/classes/Logger_Tests.cls
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,17 @@ private class Logger_Tests {
@isTest
static void it_should_return_transaction_id() {
String transactionId = Logger.getTransactionId();
System.assert(!String.isBlank(transactionId));
System.assert(String.isNotBlank(transactionId));
System.assertEquals(System.Request.getCurrent().getRequestId(), transactionId);
}

@isTest
static void it_should_set_transaction_entry_number() {
for (Integer i = 0; i < 10; i++) {
LogEntryEventBuilder builder = Logger.info('my log entry');

System.assertEquals(i + 1, builder.getLogEntryEvent().TransactionEntryNumber__c);
}
}

@isTest
Expand Down

0 comments on commit 0d5b6db

Please sign in to comment.