Sails log prefix themes

Summary

In this Sailscasts screencast, Kelvin Omereshone demonstrates how to modify the log prefix in a Sails.js application. Sails uses a logger called CaptainsLog, which provides two ways to modify log prefixes: setting a static prefix or using a prefix theme.

Ways to Modify Sails Log Prefix:

  1. Setting a Static Prefix:

    • Modify config/log.js by adding a prefix key with a string value.

    • Example:

      module.exports.log = {
        prefix: 'funny-log'
      };
    • This applies the same prefix to all log levels.

  2. Using a Prefix Theme:

    • Instead of a static prefix, use prefixTheme to apply predefined styles.

    • Available themes:

      • traditional (default)

      • abbreviated (shortened log levels: I for info, D for debug)

      • moderate (minimal indicators)

      • aligned (right-aligned log labels)

      • minimalist (even fewer labels)

      • bubbles (decorative bubble-styled log indicators)

      • flowers (fancy log style)

    • Example:

      module.exports.log = {
        prefixTheme: 'bubbles'
      };
    • This ensures a consistent but styled log prefix.

  3. Prefix vs. Prefix Theme Priority:

    • If both prefix and prefixTheme are set, prefix takes precedence.

    • Example:

      module.exports.log = {
        prefix: 'overriding-prefix',
        prefixTheme: 'flowers'
      };
    • The logs will use Overriding Prefix instead of the flowers theme.

Kelvin suggests that modifying log prefixes can be useful for debugging, styling, and personalizing logs in a Sails.js project. While he hasn't used it in production yet, he finds it a fun and useful feature.

Transcript

Kelvin Omereshone (00:00)
Hello there, welcome to another Sailscasts. In this Sailscasts, we are going to be looking at how you can modify the Sails log prefix.

So if you don't know what the Sails log prefix is, let me show you. Right here, I have a Sails project in my terminal. If I run sails l to lift the Sails server, you’ll notice prefixes like info and debug. These are log prefixes in Sails.

By default, Sails provides a way to modify log prefixes, and we are going to look at how to do that. So, I’m opening this project in Visual Studio Code and navigating to config/log.js.

Sails uses a logger called CaptainsLog, which exposes two ways to modify log prefixes. These log prefixes help distinguish the type of logs, such as info, debug, error, or warn.

Setting a Static Prefix

If you want all logs to have the same prefix, you can add a prefix key in config/log.js. For example:

module.exports.log = {
  prefix: 'funny log'
};

If I restart the server with sails l, you’ll see the logs now start with funny log.

However, this isn’t always useful since it applies to all log levels uniformly. Instead, we can use prefix themes.

Using a Prefix Theme

Instead of setting a static prefix, Sails lets you use different themes. These themes change how the log prefixes are displayed.

For example, setting:

module.exports.log = {
  prefixTheme: 'traditional'
};

won’t change anything because it’s the default. But there are other themes we can try:

  • abbreviated – Shortens log levels (I for info, D for debug).

  • moderate – Shows minimal indicators.

  • aligned – Aligns prefixes to the right.

  • minimalist – Even more minimal.

  • bubbles – Adds bubbles to the prefix.

  • flowers – Uses a flowery style.

If we change to the Abbreviated theme:

module.exports.log = {
  prefixTheme: 'abbreviated'
};

and restart the server, the logs now show I for info, D for debug, and so on.

Checking Out All Themes

Let’s try a few more themes:

  1. Moderate:

module.exports.log = {
  prefixTheme: 'moderate'
};

The info prefix disappears, and debug gets a dash.

  1. Aligned:

module.exports.log = {
  prefixTheme: 'aligned'
};

It right-aligns the prefixes.

  1. Minimalist:

module.exports.log = {
  prefixTheme: 'minimalist'
};

Even fewer indicators.

  1. Bubbles:

module.exports.log = {
  prefixTheme: 'bubbles'
};

This adds bubbles before the log labels.

  1. Flowers:

module.exports.log = {
  prefixTheme: 'flowers'
};

This theme uses decorative styling for log prefixes.

Prefix vs. Prefix Theme Priority

One last thing: if you define both prefix and prefixTheme, the prefix key overrides the theme. For example:

module.exports.log = {
  prefix: 'overriding-prefix',
  prefixTheme: 'flowers'
};

Even though we set prefixTheme to flowers, the logs will use Overriding Prefix.

Conclusion

That’s how you modify log prefixes in Sails! I found this feature fun and useful, and I’d love to hear if you plan to use it in your day-to-day development. Let me know in the comments, and catch you in another Sailscasts!

Lastest courses

course.title

Build 50 Products in 50 Days

Focus on shipping full-stack JavaScript web apps instead of chasing trends. A course bundled with 50 ready-to-ship products.

course.title

Getting started with Waterline

Master the fundamentals of the built-in Sails ORM - Waterline

course.title

Getting started with Sails

Learning everything you need to know to get up and running with Sails

Browse All Courses