javascriptlocomotivejs

How to obtain logs in locomotiveJS?


I am new to the locomotiveJS framework. I want to test my code and log some data. What is the proper way of loggin info in locomotiveJS?


Solution

  • You can just use console.log() in your code, it will appear on the console.

    If you want to log data to a file, Locomotive doesn't have any solutions for that itself. But you can use any of the available log packages, like log, for that:

    // config/initializers/10_log.js
    var fs  = require('fs');
    var Log = require('log');
    
    module.exports = function() {
      this.logger = new Log('debug', fs.createWriteStream('my.log'));
    };
    
    // in your controller
    MyController.main = function() {
      this.app.logger.info('HELLO WORLD');
      ...
    };
    

    Explanation: 'initializers' are files that are executed by Locomotive when it starts up. This particular initializer configures the log package to write to a file my.log in the directory where you start your server. It will attach the logger object to the Locomotive application object.

    In your controller methods, you can access that logger object as this.app.logger.