{ sailsit }

Sails Js Tutorials for Beginners and Forum

Have a Question?

If you have any question you can ask below or enter what you are looking for!

File Logging with LogRotate

Sails comes with winston package preinstalled, which we will be using with some other packages in this tutorial to enable file logging in sails.

Let’s get started

1. Creating Project

Create a new SailsJs project.
Follow this post on Setting up sails project

2. Logging directory

Create a directory log inside project directory
Give 777 permission to the directory

mkdir log

chmod -R 777 log

 

3. Installing packages

Install wintson-logrotate and moment package using below commands

npm install winston-logrotate --save

npm install moment --save

 

4. Create Custom Logger

Create a custom logger (you can use the below code) and add it into config/log.js

var customLogger = new winston.Logger({
    transports: [
        new winston.transports.Rotate({
            file: './log/' + date + '.log', // this path needs to be absolute
            colorize: false,
            timestamp: true,
            json: false,
            size: '100m', //File size limit
            keep: 5,
            compress: true,
            level: 'silly'
        })
    ],
});

Then enable customLogger for sails logging as below

custom: customLogger

This will enable file logging in your sails application and new file will be created every time current file size reaches 100mb

4. Lift Sails

Start your sails project using below command

sails lift

This will enable file logging in your sails application and new file will be created everytime current file size reaches 100mb

5. Transferring Logs to S3 (AWS)

You can use the below bash script to S3 bucket in AWS
This script will move the files in the S3 in “YEAR/MONTH/DATE” structure
AWSS3BashScript

You can get the complete source code of this tutorial in below url
SailsFileLogRotate

* Credits to Raj Soni for the above AWS S3 bash script

(2) Comments

    • Raj Soni

      Hi Ankur,
      It just a suggestion, you can use 666 for production. If you have any concern or suggestion, do let us know.

Comments are closed.