Nebula Logger Code Examples
Overview
Nebula Logger is useful when System.debug is insufficient for production observability.
Use it to capture structured context, correlation IDs, and error details across synchronous and asynchronous flows.
Example 1: Service-Level Structured Logging
public with sharing class ExampleService {
public static void doWork() {
Logger.info('ExampleService.doWork start');
try {
List<Contact> contacts = [SELECT Id, LastName FROM Contact LIMIT 10];
Logger.info('Loaded contacts', new Map<String, Object>{ 'count' => contacts.size() });
} catch (Exception e) {
Logger.error('ExampleService.doWork failed', e);
throw e;
}
}
}
Example 2: Correlation ID Pattern
public with sharing class IntegrationService {
public static void syncAccount(Id accountId, String correlationId) {
Logger.info('syncAccount request', new Map<String, Object>{
'accountId' => accountId,
'correlationId' => correlationId
});
// callout and mapping logic
}
}
Use the same correlation ID in outbound headers and logs to trace cross-system requests.
Example 3: Async Job Logging
public class ReconciliationJob implements Queueable {
public void execute(QueueableContext context) {
Logger.info('ReconciliationJob started', new Map<String, Object>{
'jobId' => context.getJobId()
});
try {
// async processing
Logger.info('ReconciliationJob completed');
} catch (Exception e) {
Logger.error('ReconciliationJob failed', e);
throw e;
}
}
}
Operational Guardrails
- Log business keys and correlation IDs, not sensitive payload dumps.
- Enforce log level policy by environment.
- Define retention and purge strategy for log objects.
- Use alerts on repeated high-severity error signatures.