Skip to content

Getting Started with the Log Timer

Johannes Fischer edited this page Jan 22, 2022 · 1 revision

Using the Log Timer in a Lightning Web Component

Import the log factory using the following import:

import { createLogger, startLogTimer } from 'c/rflibLogger';

Then declare a property in your module for the actual logger.

export default class MyModule extends LightningElement {

    logger = createLogger('MyModule');

    ...
}

Last, start start a log timer and mark it as done.

handleSomeEvent(event) {
    // The threshold is in milliseconds
    let logTimer = startLogTimer(this.logger, 750, 'handle some event timer', 'WARN' );
    
    //... more business logic

    logTimer.done(); // will check the time span between start and finish and log a statement with the duration
}

Using the log timer in a Lightning Aura Component

Insert the logger factory and logger component into any Lightning Component, preferably at the top of the markup.

<c:rflibLogger aura:id="loggerFactory" />
<c:rflibLoggerCmp aura:id="logger" name="MyCustomContext" appendComponentId="false" />

Then retrieve the logger from your controller or helper code. Also retrieve the logger factory to start the log timer.

({
    doInit: function(component, event, helper) {
        var logger = component.find('logger');
        var loggerFactory = component.find('loggerFactory');

        var logTimer = loggerFactory.startLogTimer(logger, '300', 'Do Init');
        helper.callServerFunc(component, function(response) {
            // .. do something with response
            logTimer.done();
        });
    }
})

Logging in Apex

Create a logger in your Apex class using the following command:

rflib_LogTimer logTimer = rflib_LoggerUtil.startLogTimer(LOGGER, 300, 'My Timer Name');

Then execute your business logic, followed by completing the log timer.

logTimer.done();

It is best practice to call the done() method within a finally block. For example:

rflib_LogTimer logTimer = rflib_LoggerUtil.startLogTimer(LOGGER, 300, 'Handle Bot Request');
try {
    LOGGER.info('submit({0}, {1})', new Object[] { utterance, fileName});
            
    // some business logic here
} catch (Exception e) {
    LOGGER.fatal('Failed to handle bot submission', e);  
    throw new AuraHandledException('Oops, something went wrong invoking that command');            
} finally {
    logTimer.done();
}
Clone this wiki locally