forcelog icon indicating copy to clipboard operation
forcelog copied to clipboard

A structured, extensible logger for Salesforce Apex

ForceLog

ForceLog is a structured logger for Salesforce Apex that is extensible to suit various log formats and providers. It provides two loggers, ForceLog.Logger (for handling logs on an individual level) and ForceLog.BulkLogger (for handling logs in bulk).

License: MIT CircleCI

ForceLog.Logger Example

private ForceLog.Logger log = new ForceLog.Logger('myClassName');

public String getContactNameById(String id) {
    log.withField('id', id).info('querying contact');

    try {
        Contact c = [
            SELECT Name
            FROM Contact
            WHERE ID =: id
        ];

        log.withField('contactName', c.Name).info('queried contact');

        return c.Name;
    } catch(QueryException ex) {
        log.withException(ex).error('failed to query contact');
        return null;
    }
}

ForceLog.BulkLogger Example

private ForceLog.Logger log = new ForceLog.BulkLogger('myClassName');

public String getContactNameById(String id) {
    log.withField('id', id).info('querying contact');

    String name;
    try {
        Contact c = [
            SELECT Name
            FROM Contact
            WHERE ID =: id
        ];

        log.withField('contactName', c.Name).info('queried contact');

        name = c.Name;
    } catch(QueryException ex) {
        log.withException(ex).error('failed to query contact');
    }

    log.dispose();
    return name;
}

Please see the wiki for full implementation details and documentation