Search code examples
loggingecmascript-6aurelia

Send Aurelia Error logs to server and notify the user


I would like to know how to access Aurelia logger, catch the errors only and trigger a function that send that data to some server. Also I have to trigger Alert box, to inform the User that some error occur. That need to works for my entire App.

So far, I found that I can get logs from LogManager:

import {LogManager} from 'aurelia-framework'; in main.js,

but I don't know how to access it and create a custom logger that will send that error data. If someone know from where I can find more info about that LogManager and how interact with it, that will be helpful as well. Thanks!


Solution

  • You'll need to write a custom logger to do what you want. It's fairly simple. Just use the ConsoleAppender as a starting point: https://github.com/aurelia/logging-console/blob/master/src/index.js

    Then you'll need to register it with Aurelia at startup. This is done with the following code:

    import {LogManager} from 'aurelia-framework';
    import {CustomAppender} from './your-custom-code;
    
    LogManager.addAppender(new CustomAppender());
    LogManager.setLevel(LogManager.logLevel.debug);
    
    export function configure(aurelia) {
      aurelia.use
        .defaultBindingLanguage()
        .defaultResources()
        .history()
        .router()
        .eventAggregator();
    
      aurelia.start().then(() => aurelia.setRoot('app', document.body));
    }
    

    This is in our docs (though admittedly, we can improve searching for this) here: http://aurelia.io/docs.html#/aurelia/framework/1.0.0-beta.1.2.4/doc/article/cheat-sheet just Ctrl-F for LogManager.