Bootstrap FreeKB - Sails - Winston logger
Sails - Winston logger

Updated:   |  Sails articles

Let's say you have used the sails new command to create an app named foo-app. The log.js file will control logging. By default, log.js should contain something like this. With this configuration, since the log level is commented out, by default, logging will occur at log level info.

module.exports.log = {
  // level: 'info'
}

 

When you want to adjust the logging, it is farily common to use the Winston logger. First you will need to install Winston.

npm install winston

 

Then the log.js file can be updated to contain the following. Notice in this example that /var/log/foo.log is used. This will make it so that events are appended to the /var/log/foo.log in the container.

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;
}

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(),
          new transports.File( { filename: '/var/log/foo.log' } )
  ]
});

module.exports.log = {
  custom: customLogger,
  inspect: false,
  level: 'silly'
};

 

Let's say you use the sails lift command to start foo-app. Something like this should be appended to /var/log/foo.log in the container.

info: Starting app...

 info: Initializing project hook... (`api/hooks/custom/`)
 info: Initializing `apianalytics` hook...  (requests to monitored routes will be logged!)
 info: ·• Auto-migrating...  (alter)
 info:    Hold tight, this could take a moment.
 info:  ✓ Auto-migration complete.

debug: Running v0 bootstrap script...  (looks like this is the first time the bootstrap has run on this computer)
 info:
 info:                .-..-.
 info:
 info:    Sails              <|    .-..-.
 info:    v1.4.3              |\
 info:                       /|.\
 info:                      / || \
 info:                    ,'  |'  \
 info:                 .-'.-==|/_--'
 info:                 `--'-------'
 info:    __---___--___---___--___---___--___
 info:  ____---___--___---___--___---___--___-__
 info:
 info: Server lifted in `/opt/foo-app`
 info: To shut down Sails, press <CTRL> + C at any time.
 info: Read more at https://sailsjs.com/support.

debug: -------------------------------------------------------
debug: :: Wed Jul 21 2021 23:02:10 GMT-0500 (Central Daylight Time)

debug: Environment : development
debug: Port        : 1337
debug: -------------------------------------------------------

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 25f1ee in the box below so that we can be sure you are a human.