From 0d5b6dbfc46d70a8437f5766ef7b64fd8c2e7f86 Mon Sep 17 00:00:00 2001 From: Jonathan Gillespie Date: Thu, 1 Apr 2021 22:53:47 -0700 Subject: [PATCH] Added 'transaction entry number' for log entry (#127) * Added log entry field TransactionEntryNumber__c (populated by Logger) --- .../classes/LogEntryEventHandler.cls | 1 + .../LogEntryRecordPage.flexipage-meta.xml | 9 +++++ ...gEntry__c-Log Entry Layout.layout-meta.xml | 6 +++- .../TransactionEntryNumber__c.field-meta.xml | 15 ++++++++ .../LoggerAdmin.permissionset-meta.xml | 5 +++ .../LoggerEndUser.permissionset-meta.xml | 5 +++ .../LoggerLogViewer.permissionset-meta.xml | 5 +++ .../profiles/Admin.profile-meta.xml | 5 +++ .../main/logger-engine/classes/Logger.cls | 5 +-- .../TransactionEntryNumber__c.field-meta.xml | 15 ++++++++ .../classes/LogEntryEventHandler_Tests.cls | 34 ++++++++++++++++--- .../logger-engine/classes/Logger_Tests.cls | 12 ++++++- 12 files changed, 108 insertions(+), 9 deletions(-) create mode 100644 nebula-logger/main/log-management/objects/LogEntry__c/fields/TransactionEntryNumber__c.field-meta.xml create mode 100644 nebula-logger/main/logger-engine/objects/LogEntryEvent__e/fields/TransactionEntryNumber__c.field-meta.xml diff --git a/nebula-logger/main/log-management/classes/LogEntryEventHandler.cls b/nebula-logger/main/log-management/classes/LogEntryEventHandler.cls index 033bbbe22..1a4a28465 100644 --- a/nebula-logger/main/log-management/classes/LogEntryEventHandler.cls +++ b/nebula-logger/main/log-management/classes/LogEntryEventHandler.cls @@ -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 diff --git a/nebula-logger/main/log-management/flexipages/LogEntryRecordPage.flexipage-meta.xml b/nebula-logger/main/log-management/flexipages/LogEntryRecordPage.flexipage-meta.xml index bbbd47e4b..57e2b9c77 100644 --- a/nebula-logger/main/log-management/flexipages/LogEntryRecordPage.flexipage-meta.xml +++ b/nebula-logger/main/log-management/flexipages/LogEntryRecordPage.flexipage-meta.xml @@ -52,6 +52,15 @@ Record.Log__c + + + + uiBehavior + readonly + + Record.TransactionEntryNumber__c + + diff --git a/nebula-logger/main/log-management/layouts/LogEntry__c-Log Entry Layout.layout-meta.xml b/nebula-logger/main/log-management/layouts/LogEntry__c-Log Entry Layout.layout-meta.xml index 11dd9496b..2a63a583a 100644 --- a/nebula-logger/main/log-management/layouts/LogEntry__c-Log Entry Layout.layout-meta.xml +++ b/nebula-logger/main/log-management/layouts/LogEntry__c-Log Entry Layout.layout-meta.xml @@ -17,6 +17,10 @@ Required Log__c + + Readonly + TransactionEntryNumber__c + Required Name @@ -246,7 +250,7 @@ false false - 00h1g000002BBzm + 00h1D000003W7GS 4 0 Default diff --git a/nebula-logger/main/log-management/objects/LogEntry__c/fields/TransactionEntryNumber__c.field-meta.xml b/nebula-logger/main/log-management/objects/LogEntry__c/fields/TransactionEntryNumber__c.field-meta.xml new file mode 100644 index 000000000..1461cd0fe --- /dev/null +++ b/nebula-logger/main/log-management/objects/LogEntry__c/fields/TransactionEntryNumber__c.field-meta.xml @@ -0,0 +1,15 @@ + + + TransactionEntryNumber__c + The sequential number of this log entry within the transaction + false + The sequential number of this log entry within the transaction + + 10 + false + 0 + false + false + Number + false + diff --git a/nebula-logger/main/log-management/permissionsets/LoggerAdmin.permissionset-meta.xml b/nebula-logger/main/log-management/permissionsets/LoggerAdmin.permissionset-meta.xml index 7bc09fde0..2484b0bb9 100644 --- a/nebula-logger/main/log-management/permissionsets/LoggerAdmin.permissionset-meta.xml +++ b/nebula-logger/main/log-management/permissionsets/LoggerAdmin.permissionset-meta.xml @@ -439,6 +439,11 @@ LogEntry__c.Timestamp__c true + + false + LogEntry__c.TransactionEntryNumber__c + true + false LogEntry__c.TriggerIsExecuting__c diff --git a/nebula-logger/main/log-management/permissionsets/LoggerEndUser.permissionset-meta.xml b/nebula-logger/main/log-management/permissionsets/LoggerEndUser.permissionset-meta.xml index 4cbc11a4c..d3a57baa8 100644 --- a/nebula-logger/main/log-management/permissionsets/LoggerEndUser.permissionset-meta.xml +++ b/nebula-logger/main/log-management/permissionsets/LoggerEndUser.permissionset-meta.xml @@ -417,6 +417,11 @@ LogEntry__c.Timestamp__c true + + false + LogEntry__c.TransactionEntryNumber__c + true + false LogEntry__c.TriggerIsExecuting__c diff --git a/nebula-logger/main/log-management/permissionsets/LoggerLogViewer.permissionset-meta.xml b/nebula-logger/main/log-management/permissionsets/LoggerLogViewer.permissionset-meta.xml index ea3bacb2c..d5042161d 100644 --- a/nebula-logger/main/log-management/permissionsets/LoggerLogViewer.permissionset-meta.xml +++ b/nebula-logger/main/log-management/permissionsets/LoggerLogViewer.permissionset-meta.xml @@ -403,6 +403,11 @@ LogEntry__c.Timestamp__c true + + false + LogEntry__c.TransactionEntryNumber__c + true + false LogEntry__c.TriggerIsExecuting__c diff --git a/nebula-logger/main/log-management/profiles/Admin.profile-meta.xml b/nebula-logger/main/log-management/profiles/Admin.profile-meta.xml index 80f543cf6..e62f46808 100644 --- a/nebula-logger/main/log-management/profiles/Admin.profile-meta.xml +++ b/nebula-logger/main/log-management/profiles/Admin.profile-meta.xml @@ -981,6 +981,11 @@ LogEntry__c.Timestamp__c true + + false + LogEntry__c.TransactionEntryNumber__c + true + false LogEntry__c.TriggerIsExecuting__c diff --git a/nebula-logger/main/logger-engine/classes/Logger.cls b/nebula-logger/main/logger-engine/classes/Logger.cls index c18db517c..a34d213c8 100644 --- a/nebula-logger/main/logger-engine/classes/Logger.cls +++ b/nebula-logger/main/logger-engine/classes/Logger.cls @@ -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 @@ -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); } @@ -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 { diff --git a/nebula-logger/main/logger-engine/objects/LogEntryEvent__e/fields/TransactionEntryNumber__c.field-meta.xml b/nebula-logger/main/logger-engine/objects/LogEntryEvent__e/fields/TransactionEntryNumber__c.field-meta.xml new file mode 100644 index 000000000..d734c89ad --- /dev/null +++ b/nebula-logger/main/logger-engine/objects/LogEntryEvent__e/fields/TransactionEntryNumber__c.field-meta.xml @@ -0,0 +1,15 @@ + + + TransactionEntryNumber__c + The sequential number of this log entry within the transaction + false + false + false + false + + 10 + true + 0 + Number + false + diff --git a/nebula-logger/tests/log-management/classes/LogEntryEventHandler_Tests.cls b/nebula-logger/tests/log-management/classes/LogEntryEventHandler_Tests.cls index 8349fd74d..c47155a9e 100644 --- a/nebula-logger/tests/log-management/classes/LogEntryEventHandler_Tests.cls +++ b/nebula-logger/tests/log-management/classes/LogEntryEventHandler_Tests.cls @@ -117,6 +117,7 @@ private class LogEntryEventHandler_Tests { RecordSObjectTypeNamespace__c, StackTrace__c, Timestamp__c, + TransactionEntryNumber__c, TriggerIsExecuting__c, TriggerOperationType__c, TriggerSObjectType__c @@ -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); @@ -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); @@ -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', @@ -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) ); @@ -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 ); @@ -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; @@ -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); @@ -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; @@ -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 { diff --git a/nebula-logger/tests/logger-engine/classes/Logger_Tests.cls b/nebula-logger/tests/logger-engine/classes/Logger_Tests.cls index 772c1772a..1afebb77d 100644 --- a/nebula-logger/tests/logger-engine/classes/Logger_Tests.cls +++ b/nebula-logger/tests/logger-engine/classes/Logger_Tests.cls @@ -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