sails.config.log
Configuration for the logger in your Sails app. These settings apply whenever you call functions like sails.log.debug()
or sails.log.error()
in your app code, as well as when Sails logs a message to the console automatically. The options here are conventionally specified in the config/log.js configuration file.
Property | Type | Default | Details |
---|---|---|---|
level | 'info' |
Set the level of detail to be shown in your app's log. | |
inspect | true |
Set to false to disable captain's log's handling of logging, logs will instead be passed to the configured custom logger. | |
custom | undefined |
Specify a reference to an instance of a custom logger (such as Winston). If provided, instead of logging directly to the console, the functions exposed by the custom logger will be called, and log messages from Sails will be passed through. For more information, see captains-log. |
It is sometimes useful to configure a custom logger, particularly for regulatory compliance and organizational requirements (e.g. if your company is using a particular logger in other apps). In the context of Sails, configuring a custom logger also allows you to intercept all log messages automatically created by the framework, which is handy for setting up email notifications about errors and warnings.
Don't feel like you have to use a custom logger if you want these sorts of notifications! In fact, there are usually more straightforward ways to implement features like automated Slack, SMS, or email notifications when errors occur. One approach is to customize your app's default server error response (
responses/serverError.js
). Another popular option is to use a product like Papertrail, or a monitoring service like AppDynamics or NewRelic.
Here's an example configuring Winston as a custom logger, defining both a console transport and file transport.
First of all, add winston
as a dependency of your project:
npm install winston
Then, replace the content of config/log.js
with the following:
// config/log.js
const { version } = require('../package');
const { createLogger, format, transports } = require('winston');
const { combine, timestamp, colorize, label, printf, align } = format;
const { SPLAT } = require('triple-beam');
const { isObject } = require('lodash');
function formatObject(param) {
if (isObject(param)) {
return JSON.stringify(param);
}
return param;
}
// Ignore log messages if they have { private: true }
const all = format((info) => {
const splat = info[SPLAT] || [];
const message = formatObject(info.message);
const rest = splat.map(formatObject).join(' ');
info.message = `${message} ${rest}`;
return info;
});
const customLogger = createLogger({
format: combine(
all(),
label({ label: version }),
timestamp(),
colorize(),
align(),
printf(info => `${info.timestamp} [${info.label}] ${info.level}: ${formatObject(info.message)}`)
),
transports: [new transports.Console()]
});
module.exports.log = {
custom: customLogger,
inspect: false
// level: 'info'
};