3

I have a Sails.js project that I would like to add LogEntries support for event logging. However I would like to use LogEntries only in some environments.

For example, in development I don't want to fill the logs with development garbage.

// config/log.js

var logentries = require('le_node');
var log = (sails.config.environment === 'development') ? sails.log : logentries.logger({
  token: 'YOUR_TOKEN'
});

module.exports.log = {
  log: {
    custom: log
  }
}

But with this I get an error that the sails is not defined. This is because sails object is not available in configuration files such as config/log.js. So how can use a custom log transport depending on the environment in this case?

jonathanwiesel
  • 1,036
  • 2
  • 16
  • 35
  • wrap a function around it so you can pass in config from a place that has it. `var logger=require("log").log("dev")`, which should return a function, the one you used to get without the extra trailing paren call – dandavis Mar 18 '15 at 00:07

2 Answers2

2

In the end what seems correct was not modify the config/log.js file directly but to modify the environment configuration file:

// config/env/production.js

var logentries = require('le_node');
var log = logentries.logger({
  token: 'YOUR_TOKEN'
});

module.exports.log = {
  log: {
    custom: log
  }
}

This way only the production environment will have the custom logging.

jonathanwiesel
  • 1,036
  • 2
  • 16
  • 35
0

In all config modules, I find that you can't just write sails.config.someModule. Just go with the typical var someModule = require(./someModule). Much more easier that way.

Bwaxxlo
  • 1,860
  • 13
  • 18
  • I've read that it's not recommended to require config files from other config files (http://stackoverflow.com/questions/25622955/accessing-config-variables-from-other-config-files) – jonathanwiesel Mar 18 '15 at 14:27