Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
stephenpstanley opened this issue May 28, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@stephenpstanley
Copy link

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();
}

}

@stephenpstanley stephenpstanley added the enhancement New feature or request label May 28, 2024
@nwenslaff
Copy link

I'm running into the same issue

@jongpie
Copy link
Owner

jongpie commented Jun 12, 2024

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

@stephengoodcloud
Copy link

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

@jongpie
Copy link
Owner

jongpie commented Jun 18, 2024

@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.

@jongpie jongpie closed this as completed Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants