### Install streamroller via NPM Source: https://github.com/log4js-node/streamroller/blob/master/README.md The command to install the streamroller package into a Node.js project. ```shell npm install streamroller ``` -------------------------------- ### RollingFileStream with Combined Options in Node.js Source: https://context7.com/log4js-node/streamroller/llms.txt An example combining multiple options for RollingFileStream, including compression (`compress`), preserving file extensions (`keepFileExt`), and a custom filename separator (`fileNameSep`). This provides comprehensive control over log file naming and management. ```javascript const { RollingFileStream } = require('streamroller'); const stream = new RollingFileStream('application.log', 30, 2, { compress: true, keepFileExt: true, fileNameSep: '_' }); stream.write('First log entry for demonstration\n'); stream.write('Second entry causes first rotation\n'); stream.write('Third entry triggers another roll\n'); stream.end(); // Files created: application.log, application_1.log.gz, application_2.log.gz ``` -------------------------------- ### RollingFileStream Error Handling and Events Source: https://context7.com/log4js-node/streamroller/llms.txt Shows how to properly handle errors and stream lifecycle events when using RollingFileStream. Includes examples of writing log entries with callbacks and ending the stream gracefully. ```javascript const { RollingFileStream } = require('streamroller'); const stream = new RollingFileStream('events.log', 1024, 3); stream.on('error', (err) => { console.error('Write error:', err.message); }); // Write with callback stream.write('Log entry\n', 'utf8', (err) => { if (err) { console.error('Write failed:', err); } else { console.log('Write successful'); } }); // End stream with callback stream.end('Final entry\n', 'utf8', () => { console.log('Stream closed successfully'); }); ``` -------------------------------- ### RollingFileStream with Custom File Mode in Node.js Source: https://context7.com/log4js-node/streamroller/llms.txt Demonstrates setting custom Unix file permissions for created log files using the `mode` option in RollingFileStream. This example sets the mode to `0600`, allowing only the owner to read and write the file. ```javascript const { RollingFileStream } = require('streamroller'); const stream = new RollingFileStream('secure.log', 1024, 3, { mode: parseInt('0600', 8), // Owner read/write only (default) flags: 'a', // Append mode (default) encoding: 'utf8' // Character encoding (default) }); stream.write('Sensitive data logged securely\n'); stream.end(); ``` -------------------------------- ### RollingFileStream with Custom Filename Separator in Node.js Source: https://context7.com/log4js-node/streamroller/llms.txt Shows how to customize the separator used between filename parts during log file rotation using the `fileNameSep` option. This example uses an underscore (`_`) instead of the default dot (`.`) as the separator. ```javascript const { RollingFileStream } = require('streamroller'); // Use underscore instead of dot as separator const stream = new RollingFileStream('app.log', 30, 2, { fileNameSep: '_' }); stream.write('Message that triggers rotation\n'); stream.write('Another message\n'); stream.end(); // Files created: app.log, app.log_1, app.log_2 ``` -------------------------------- ### Configure advanced stream options Source: https://context7.com/log4js-node/streamroller/llms.txt Demonstrates a full configuration using multiple options including custom separators, file permissions, and stream flags for production-grade logging. ```javascript const { DateRollingFileStream } = require('streamroller'); const stream = new DateRollingFileStream('production.log', 'yyyy-MM-dd', { compress: true, keepFileExt: true, fileNameSep: '_', alwaysIncludePattern: true, numBackups: 14, maxSize: 5242880, mode: parseInt('0640', 8), flags: 'a', encoding: 'utf8' }); stream.write('Production log entry\n'); stream.end(); ``` -------------------------------- ### RollingFileWriteStream with Tilde Expansion Source: https://context7.com/log4js-node/streamroller/llms.txt Illustrates how RollingFileWriteStream supports tilde expansion (~) in file paths for Unix-like systems, allowing logs to be written to the user's home directory. Includes basic configuration for size-based rolling. ```javascript const { RollingFileWriteStream } = require('streamroller'); // Writes to /home/username/logs/app.log const stream = new RollingFileWriteStream('~/logs/app.log', { maxSize: 1048576, numBackups: 3 }); stream.write('Log to home directory\n'); stream.end(); ``` -------------------------------- ### Initialize and use RollingFileStream Source: https://github.com/log4js-node/streamroller/blob/master/README.md Demonstrates how to require the library and instantiate a RollingFileStream. This stream behaves like a standard Node.js WritableStream but handles file rotation automatically. ```javascript var rollers = require('streamroller'); var stream = new rollers.RollingFileStream('myfile', 1024, 3); stream.write("stuff"); stream.end(); ``` -------------------------------- ### Log4js Integration with Streamroller Source: https://context7.com/log4js-node/streamroller/llms.txt Demonstrates how log4js-node utilizes streamroller internally for its file and dateFile appenders. Shows the configuration for setting up file-based logging with size and date rotation, including compression. ```javascript const log4js = require('log4js'); // log4js uses streamroller internally for these appenders log4js.configure({ appenders: { file: { type: 'file', filename: 'logs/app.log', maxLogSize: 10485760, // 10MB backups: 5, compress: true }, dateFile: { type: 'dateFile', filename: 'logs/app.log', pattern: 'yyyy-MM-dd', numBackups: 14, compress: true } }, categories: { default: { appenders: ['file', 'dateFile'], level: 'info' } } }); const logger = log4js.getLogger(); logger.info('This uses streamroller under the hood'); ``` -------------------------------- ### Configure custom date patterns for rotation Source: https://context7.com/log4js-node/streamroller/llms.txt Shows how to define specific rotation intervals such as hourly, minute-level, or numeric-only date patterns by passing a pattern string to the constructor. ```javascript const { DateRollingFileStream } = require('streamroller'); // Hourly rotation const hourlyStream = new DateRollingFileStream('hourly.log', 'yyyy-MM-dd.hh'); // Minute-level rotation const minuteStream = new DateRollingFileStream('minute.log', 'yyyy-MM-dd-hh-mm'); // Date-only pattern (digits only) const digitStream = new DateRollingFileStream('digit.log', 'yyyyMMdd'); ``` -------------------------------- ### Implement hybrid size and date-based rotation Source: https://context7.com/log4js-node/streamroller/llms.txt Combines date-based rotation with 'maxSize' limits to handle high-volume logging, allowing multiple files per day if the size limit is reached. ```javascript const { DateRollingFileStream } = require('streamroller'); const stream = new DateRollingFileStream('hybrid.log', 'yyyy-MM-dd', { maxSize: 1048576, numBackups: 10, compress: true }); stream.write('High volume log entry\n'); stream.end(); ``` -------------------------------- ### Advanced RollingFileWriteStream Configuration Source: https://context7.com/log4js-node/streamroller/llms.txt Demonstrates direct usage of RollingFileWriteStream with extensive options for size-based rolling, date-based rolling, backup management, compression, and file naming conventions. This provides full control over log file behavior. ```javascript const { RollingFileWriteStream } = require('streamroller'); const stream = new RollingFileWriteStream('advanced.log', { maxSize: 1048576, // 1MB - triggers size-based rolling numBackups: 5, // Keep 5 backup files pattern: 'yyyy-MM-dd', // Also roll on date change compress: true, // Gzip backups keepFileExt: true, // Preserve .log extension alwaysIncludePattern: true, // Include date in initial filename fileNameSep: '.', // Separator character encoding: 'utf8', mode: parseInt('0600', 8), flags: 'a' // Append mode }); stream.write('Advanced logging with full options\n'); // Handle stream errors stream.on('error', (err) => { console.error('Stream error:', err); }); stream.end(() => { console.log('Stream closed'); }); ``` -------------------------------- ### Enable log file compression Source: https://context7.com/log4js-node/streamroller/llms.txt Enables Gzip compression for rotated log files by setting the 'compress' option to true, which helps save disk space for long-term storage. ```javascript const { DateRollingFileStream } = require('streamroller'); const stream = new DateRollingFileStream('compressed.log', 'yyyy-MM-dd', { compress: true, numBackups: 30 }); stream.write('Log entry that will be compressed when rotated\n'); stream.end(); ``` -------------------------------- ### Initialize DateRollingFileStream with basic rotation Source: https://context7.com/log4js-node/streamroller/llms.txt Demonstrates the basic usage of DateRollingFileStream for daily log rotation. It uses the default 'yyyy-MM-dd' pattern to rename files upon date changes. ```javascript const { DateRollingFileStream } = require('streamroller'); // Basic daily rotation using default pattern 'yyyy-MM-dd' const stream = new DateRollingFileStream('daily.log'); stream.write('Log entry for today\n'); stream.end(); ``` -------------------------------- ### Basic RollingFileStream in Node.js Source: https://context7.com/log4js-node/streamroller/llms.txt Demonstrates the basic usage of RollingFileStream to create a writable stream that rolls over to a new file when a specified size limit is reached. Old files are renamed with incrementing indices, and the oldest files are deleted if the backup limit is exceeded. ```javascript const { RollingFileStream } = require('streamroller'); // Basic usage: roll when file exceeds 1KB, keep 3 backups const stream = new RollingFileStream('application.log', 1024, 3); stream.write('Log message 1\n'); stream.write('Log message 2\n'); stream.end(); // Files created when rolling: application.log, application.log.1, application.log.2, application.log.3 ``` -------------------------------- ### Manage backup retention with numBackups Source: https://context7.com/log4js-node/streamroller/llms.txt Configures the stream to maintain a specific number of historical log files, automatically deleting files that exceed the defined limit. ```javascript const { DateRollingFileStream } = require('streamroller'); const stream = new DateRollingFileStream('limited.log', 'yyyy-MM-dd', { numBackups: 7, alwaysIncludePattern: true }); stream.write('Daily log entry\n'); stream.end(); ``` -------------------------------- ### RollingFileStream with Gzip Compression in Node.js Source: https://context7.com/log4js-node/streamroller/llms.txt Shows how to configure RollingFileStream to compress backup log files using gzip. This helps save disk space, while the active file remains uncompressed for real-time writing. The stream rolls over at 30 bytes and keeps 2 backups. ```javascript const { RollingFileStream } = require('streamroller'); // Roll at 30 bytes, keep 2 backups, compress old files const stream = new RollingFileStream('compressed.log', 30, 2, { compress: true }); stream.write('This is the first log message.\n'); stream.write('This is the second log message.\n'); stream.write('This is the third log message.\n'); stream.write('This is the fourth log message.\n'); stream.end(); // Files created: compressed.log, compressed.log.1.gz, compressed.log.2.gz ``` -------------------------------- ### Include date pattern in filename Source: https://context7.com/log4js-node/streamroller/llms.txt Demonstrates the 'alwaysIncludePattern' option, which ensures the date pattern is appended to the filename immediately upon file creation rather than only after rotation. ```javascript const { DateRollingFileStream } = require('streamroller'); const stream = new DateRollingFileStream('app', 'yyyy-MM-dd.log', { alwaysIncludePattern: true }); stream.write('First message\n'); stream.end(); ``` -------------------------------- ### RollingFileStream with keepFileExt Option in Node.js Source: https://context7.com/log4js-node/streamroller/llms.txt Illustrates using the `keepFileExt` option with RollingFileStream to preserve the original file extension when rotating log files. Instead of `file.log.1`, the rotated files will be named like `file.1.log`. ```javascript const { RollingFileStream } = require('streamroller'); const stream = new RollingFileStream('server.log', 30, 2, { keepFileExt: true }); stream.write('First message - this will rotate\n'); stream.write('Second message - triggers rotation\n'); stream.write('Third message - in new file\n'); stream.end(); // Files created: server.log, server.1.log, server.2.log ``` -------------------------------- ### Preserve file extensions during rotation Source: https://context7.com/log4js-node/streamroller/llms.txt Uses the 'keepFileExt' option to ensure that the original file extension remains at the end of the filename after the date pattern is applied. ```javascript const { DateRollingFileStream } = require('streamroller'); const stream = new DateRollingFileStream('server.log', 'yyyy-MM-dd', { keepFileExt: true }); stream.write('Server startup\n'); stream.end(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.