Comprehensive Guide to Using logrotate-stream for Efficient Log Management

Introduction to logrotate-stream

logrotate-stream is a powerful utility designed to simplify and optimize log management in Node.js applications. It allows for the seamless rotation, compression, and management of log files, ensuring your logs are handled efficiently and do not consume excessive disk space.

In this guide, we’ll explore the various APIs offered by logrotate-stream, complete with code snippets to help you get started and a complete application example utilizing these APIs.

Installation

  npm install logrotate-stream

Basic Usage

  
    const logrotate = require('logrotate-stream');
    const fs = require('fs');

    const logStream = logrotate({ file: 'application.log', size: '10M', keep: 3 });

    logStream.write('This is a log message.');
    
    // Piping logs from another stream
    const dataStream = fs.createReadStream('data.log');
    dataStream.pipe(logStream);
  

Advanced Configuration

  
    const logStream = logrotate({
      file: 'application.log',
      size: '10M',
      keep: 5,
      compress: true,
      rotate: (oldFile, newFile, callback) => {
        console.log(`Rotating file ${oldFile} to ${newFile}`);
        callback();
      }
    });
  

Using Custom Streams and Handlers

  
    const myCustomRotate = (oldFile, newFile, cb) => {
      // Custom rotation logic
      console.log('Custom rotation');
      cb();
    };

    const logStream = logrotate({
      file: 'app.log',
      size: '5M',
      keep: 2,
      rotate: myCustomRotate
    });

    logStream.write('Logging with custom rotation handling.');
  

Complete Application Example

Now that we’ve covered the basics and advanced configurations, let’s put it all together in a complete application example.

  
    const logrotate = require('logrotate-stream');
    const express = require('express');
    const fs = require('fs');
    const app = express();
    const PORT = 3000;

    const logStream = logrotate({
      file: 'server.log',
      size: '10M',
      keep: 3,
      compress: true,
      rotate: (oldFile, newFile, callback) => {
        console.log(`Rotating log file from ${oldFile} to ${newFile}`);
        callback();
      }
    });

    app.use((req, res, next) => {
      logStream.write(`Received request: ${req.method} ${req.url}\n`);
      next();
    });

    app.get('/', (req, res) => {
      res.send('Hello, World!');
    });

    app.listen(PORT, () => {
        console.log(`Server is running on port ${PORT}`);
    });
    
    // Read data from another source and pipe to log
    const externalLogStream = fs.createReadStream('external.log');
    externalLogStream.pipe(logStream);
  

Conclusion

logrotate-stream is an essential tool for Node.js developers who need to manage application logs efficiently. With its flexible API and easy-to-use configuration options, logrotate-stream can be seamlessly integrated into your application to handle log rotation, compression, and more.

By following this guide, you should now have a solid understanding of how to use logrotate-stream to enhance your log management strategy.

Hash: e0f90ba8b20c81850844df6bf94404ca0d5fdb52e54e4bc8d75932279f2c88eb

Leave a Reply

Your email address will not be published. Required fields are marked *