The Sailscasts Blog

← Back to blog

Modifying Sails log prefix with prefix themes

Kelvin Omereshone

The de facto logger for Sails is captains-log, it accepts several configuration options but the well known option is level which can be set to any one of the following levels:

[
'silly',
'verbose',
'info',
'blank',
'debug',
'warn',
'error',
'crit',
'silent'
]

There is however an option that allows you to override the prefix for your logs. This option as any log related configuration can be set in config/log.js. Let’s have a look at that.

The prefix option

You can pass a prefix property in config/log.js to a string and Sails will use that string as the prefix for every log across your Sails application. For example if we do this:

module.exports.log = {
    prefix: 'my-custom-log-prefix: '
}

And you use the Sails log in say like an action:

sails.log('Sails is awesome')

The output will then be

my-custom-log-prefix: Sails is awesome

Okay, I guess we can already see the limitation of this because by default, Sails will use different prefix for different logs i.e error logs have a different prefix than an info log for example. Let’s look at a more robust built-in way to modify the logs prefix

The prefixTheme option

By default captains-log uses a shorthand to decide what prefix to use for each log types. This shorthand is called prefixTheme. The idea is that captains-log ships with built-in log themes that defaults to the traditional theme. Let’s look at the available themes and the prefix they expose.

traditional

traditional: {
    silly:   '     : ',
    verbose: 'verbo: ',
    info:    ' info: ',
    blank:   '',
    debug:   'debug: ',
    warn:    ' warn: ',
    error:   'error: ',
    crit:    ' crit: '
}

abbreviated

traditional: {
    silly:   '     : ',
    verbose: 'verbo: ',
    info:    ' info: ',
    blank:   '',
    debug:   'debug: ',
    warn:    ' warn: ',
    error:   'error: ',
    crit:    ' crit: '
}

moderate

moderate: {
    silly: '[silly] ',
    verbose: '[verbose] ',
    info: '    ',
    blank: '',
    debug: '[-] ',
    warn: '[!] ',
    error: '[err] ',
    crit: '[CRITICAL] '
}

aligned

aligned: {
    silly:   '   silly | ',
    verbose: ' verbose | ',
    info:    '    info | ',
    blank:   '',
    debug:   '   debug | ',
    warn:    '    warn | ',
    error:   '   error | ',
    crit:    'CRITICAL | '
 },

minimalist

minimalist: {
    silly:   ' | ',
    verbose: ' | ',
    info:    ' | ',
    blank:   '',
    debug:   ' | ',
    warn:    'warn: ',
    error:   'error: ',
    crit:    'CRITICAL: '
  }

bubbles

bubbles: {
    silly:   '   ',
    verbose: '˙˙  ',
    info:    '˙·  ',
    blank:   '',
    debug:   '•·  ',
    warn:    'warn: ',
    error:   'error: ',
    crit:    'CRITICAL: '
}

flowers

flowers: {
    silly:   '   ',
    verbose: '˙˘˙   ',
    info:    '~%°  ',
    blank:   '',
    debug:   '~∞%° ',
    warn:    'warn: ',
    error:   'error: ',
    crit:    'CRITICAL: '
 }

Using the built-in themes

Now that we know what themes are available to us we can pass in either one of the above theme name(defaults to traditional) to modify the prefix theme of our logs like so:

// config/log.js
module.exports.log = {
    prefixTheme: 'bubbles'
}

So now if we write something like:

sails.log.verbose('Ah! A verbose log')

You will get the following output

˙˙ Ah! A verbose log

Note: You will have to remove the prefix option if it’s set as its at a lower level than the prefixTheme option and will override it.

Pro tip

Did you know you can use sails.log.blank() for giving line breaks in your logs sort of like using <br/> in HTML?

Conclusion

Now you know about the possibility of modifying the Sails log prefix and how to modify it.

I think this can be useful if your decided you want less log prefix in your app(you can reach out for the minimalist theme!).

You can play with the different theme and have some fun(like I did when I discovered we can do this in Sails)