jongpie / NebulaLogger

The most robust observability solution for Salesforce experts. Built 100% natively on the platform, and designed to work seamlessly with Apex, Lightning Components, Flow, Process Builder & integrations.

Home Page:https://nebulalogger.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Create a single log for a batch execution which contains a series of log entries for all the batches

stephenpstanley opened this issue · comments

New Feature Summary

I'd like to be able to have a single log record created when a batch job is executed containing a series of log entry records , but it seems that the mechanism documented as below creates a new log record for the start, each execute and the finish, resulting in hundreds (or thousands) of log records when there are many batches to process

==== Code from the readme ======
public with sharing class BatchableLoggerExample implements Database.Batchable, Database.Stateful {
private String originalTransactionId;

public Database.QueryLocator start(Database.BatchableContext batchableContext) {
    // Each batchable method runs in a separate transaction,
    // so store the first transaction ID to later relate the other transactions
    this.originalTransactionId = Logger.getTransactionId();

    Logger.info('Starting BatchableLoggerExample');
    Logger.saveLog();

    // Just as an example, query all accounts
    return Database.getQueryLocator([SELECT Id, Name, RecordTypeId FROM Account]);
}

public void execute(Database.BatchableContext batchableContext, List<Account> scope) {
    // One-time call (per transaction) to set the parent log
    Logger.setParentLogTransactionId(this.originalTransactionId);

    for (Account account : scope) {
        // Add your batch job's logic here

        // Then log the result
        Logger.info('Processed an account record', account);
    }

    Logger.saveLog();
}

public void finish(Database.BatchableContext batchableContext) {
    // The finish method runs in yet-another transaction, so set the parent log again
    Logger.setParentLogTransactionId(this.originalTransactionId);

    Logger.info('Finishing running BatchableLoggerExample');
    Logger.saveLog();
}

}

I'm running into the same issue

@stephenpstanley this sounds very similar to issue #499 that you opened last year - is this a duplicate?

Ah yes, I think you are right. When I raised it I thought that I remembered something similar but I couldn't find it in the issues list. I expect it's because this is a completely different project at a VERY different scale where I'm getting hundreds of batch Apex executions which makes the noise factor much more significant

How is your thinking progressing on a solution? Might there be an option to get transaction ID rather then the parent transactionID and set that for all subsequent batches?

Feel free to merge or close as a duplicate

@stephengoodcloud no worries, I'll close this as a duplicate & will keep #499 to track this.

At the moment, I still don't have a solution designed for this, but I'm hoping to revisit it later this year. I don't plan to change the the behavior of when/how Log__c records are created, but hope to provide some better reporting & viewing capabilities so you can see all of the LogEntry__c records across related Log__c records.