GeezerVison is showing github.com — feat(handler): route the framework messages through a log hook by dschach · Pull Request #372 · dschach/salesforce-trigger-framework
Conversation
dschach
commented
Oct 30, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
The framework wrote its messages with System.debug, so an org that sends its logs to a logging framework such as Nebula Logger could not capture them without a fork. Consumers also had no documented way to unit-test a handler without inserting records. This pull request adds a protected log hook for the framework's messages and a protected test seam, both additive, and documents both in the README. It covers item 46 of the 2026-10-29 quality analysis (S6 and S9).
Changes
Log hook
- The new
protected virtual void log(LoggingLevel level, String message) holds the only System.debug left in TriggerHandler. Its default writes to the debug log.
- The 4 framework messages go through it: the entry line, the exit line, the limits line, and the loop limit warning. Each keeps its old wording and its old condition: the first 3 only when debug or limits output is on, and the warning always.
- The name and the order of the arguments follow common practice: apex-rollup names its pluggable logger method
log, and System.debug and Nebula Logger's Logger.newEntry take the level first.
- The README shows a generic override and a Nebula Logger example, with a link to Nebula Logger. The example tags each entry, saves each
WARN entry at once because andFinally() does not run after the loop limit stops run(), and leaves the DEBUG entries for the next Logger.saveLog() in the transaction.
Test seam
- The new
protected TriggerHandler setTestContext(System.TriggerOperation operation), visible to tests, makes a handler run as if a trigger fired the operation and returns the handler, so a test can write handler.setTestContext(System.TriggerOperation.BEFORE_INSERT).run().
- It passes
Test.isRunningTest() to the private setTriggerContext, so outside a test it changes nothing and leaves the context of the real trigger. setTriggerContext stays private, so its testMode flag can still be removed in a later cleanup.
- The README's new section "Test a Handler Without DML" shows a handler with a field visible to tests and a test that uses the seam.
Tests
- 6 new tests: the seam returns the handler and runs the simulated operation; with debug on, the exact entry and exit lines reach the hook; with debug and limits off, nothing does; with limits on, the limits line does; the loop limit warning does; and the base class's default
log runs without an error.
- The test handler records each message it receives in its
log override, and the factory newHandler() now uses setTestContext(), so every test uses the seam.
Behavior notes
- No message, condition, or result changes. A handler that does not override
log writes the same debug lines as before.
- Compatibility risk: a consumer's handler that already declares
log(LoggingLevel, String) or setTestContext(System.TriggerOperation) stops compiling until it renames the method or marks it override. A method of that name with other parameters is a legal overload and is not affected.
- The new calls use
this., as the rest of the class does. Removing this. across the class is left for 1 later cleanup.
Test plan
- Scratch org at API 67.0: 46 of 46 local tests pass, with 100 percent coverage and no uncovered lines on
TriggerHandler, AccountSampleTriggerHandler, and AccountSampleTrigger.
- A validation-only deploy compiled the new README examples and the new ApexDoc examples, and ran the README's example test, which passed.
- The Nebula Logger example, deployed as an override in the sample handler to a scratch org with Nebula Logger 4.19.5 (unlocked package), compiled and logged. With debug and limits on, 1 insert and a
Logger.saveLog() gave 6 DEBUG entries: the entry, limits, and exit lines for before insert and after insert. 51 inserts passed the sample's loop limit of 100 and gave 2 WARN entries, which the override saved by itself. All 8 entries carry the tag TriggerHandler. The override is not part of this pull request.
- Code Analyzer reports the same 7 findings as on main, none at severity 1 or 2.
- The Prettier check passes.
BEGIN_NESTED_COMMIT
feat(handler): let a test run a handler without DML
The new protected method setTestContext, visible to tests, makes a handler run as if a trigger fired the given operation and changes nothing outside a test, and the README shows a handler and a test that use it.
END_NESTED_COMMIT
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
The entry, exit, and limits lines and the loop limit warning now go through the new protected virtual method log(LoggingLevel level, String message), whose default writes to the debug log, so a handler can send them to the logging framework of its org without a fork. The README has an example for Nebula Logger.
The new protected method setTestContext(System.TriggerOperation operation), visible to tests, makes a handler run as if a trigger fired the operation, so that a test can call run() without DML. Outside a test it changes nothing, and the private setTriggerContext stays free to change. The README has an example handler and test.
Co-Authored-By: Claude <[email protected]>
|
✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (d3c42d2) to head (d7f619a).
⚠️ Report is 22 commits behind head on main.
✅ All tests successful. No failed tests found.
Additional details and impacted files
@@ Coverage Diff @@
## main #372 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 2 2
Lines 214 252 +38
=========================================
+ Hits 214 252 +38
| Flag |
Coverage Δ |
|
| Apex |
100.00% <100.00%> (ø) |
|
Flags with carried forward coverage won't be shown. Click here to find out more.
☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|