FileStreamRotator.js 22.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
'use strict';

/*!
 * FileStreamRotator
 * Copyright(c) 2012-2017 Holiday Extras.
 * Copyright(c) 2017 Roger C.
 * MIT Licensed
 */

/**
 * Module dependencies.
 */
var fs = require('fs');
var path = require('path');
var moment = require('moment');
var crypto = require('crypto');

var EventEmitter = require('events');

/**
 * FileStreamRotator:
 *
 * Returns a file stream that auto-rotates based on date.
 *
 * Options:
 *
 *   - `filename`       Filename including full path used by the stream
 *
 *   - `frequency`      How often to rotate. Options are 'daily', 'custom' and 'test'. 'test' rotates every minute.
 *                      If frequency is set to none of the above, a YYYYMMDD string will be added to the end of the filename.
 *
 *   - `verbose`        If set, it will log to STDOUT when it rotates files and name of log file. Default is TRUE.
 *
 *   - `date_format`    Format as used in moment.js http://momentjs.com/docs/#/displaying/format/. The result is used to replace
 *                      the '%DATE%' placeholder in the filename.
 *                      If using 'custom' frequency, it is used to trigger file change when the string representation changes.
 *
 *   - `size`           Max size of the file after which it will rotate. It can be combined with frequency or date format.
 *                      The size units are 'k', 'm' and 'g'. Units need to directly follow a number e.g. 1g, 100m, 20k.
 *
 *   - `max_logs`       Max number of logs to keep. If not set, it won't remove past logs. It uses its own log audit file
 *                      to keep track of the log files in a json format. It won't delete any file not contained in it.
 *                      It can be a number of files or number of days. If using days, add 'd' as the suffix.
 *
 *   - `audit_file`     Location to store the log audit file. If not set, it will be stored in the root of the application.
 * 
 *   - `end_stream`     End stream (true) instead of the default behaviour of destroy (false). Set value to true if when writing to the
 *                      stream in a loop, if the application terminates or log rotates, data pending to be flushed might be lost.                    
 *
 *   - `file_options`   An object passed to the stream. This can be used to specify flags, encoding, and mode.
 *                      See https://nodejs.org/api/fs.html#fs_fs_createwritestream_path_options. Default `{ flags: 'a' }`.
 * 
 *   - `utc`            Use UTC time for date in filename. Defaults to 'FALSE'
 * 
 *   - `extension`      File extension to be appended to the filename. This is useful when using size restrictions as the rotation
 *                      adds a count (1,2,3,4,...) at the end of the filename when the required size is met.
 * 
 *   - `watch_log`      Watch the current file being written to and recreate it in case of accidental deletion. Defaults to 'FALSE'
 *
 *   - `create_symlink` Create a tailable symlink to the current active log file. Defaults to 'FALSE'
 * 
 *   - `symlink_name`   Name to use when creating the symbolic link. Defaults to 'current.log'
 * 
 *   - `audit_hash_type` Use specified hashing algorithm for audit. Defaults to 'md5'. Use 'sha256' for FIPS compliance.
 *
 * To use with Express / Connect, use as below.
 *
 * var rotatingLogStream = require('FileStreamRotator').getStream({filename:"/tmp/test.log", frequency:"daily", verbose: false})
 * app.use(express.logger({stream: rotatingLogStream, format: "default"}));
 *
 * @param {Object} options
 * @return {Object}
 * @api public
 */
var FileStreamRotator = {};

module.exports = FileStreamRotator;

var staticFrequency = ['daily', 'test', 'm', 'h', 'custom'];
var DATE_FORMAT = ('YYYYMMDDHHmm');


/**
 * Returns frequency metadata for minute/hour rotation
 * @param type
 * @param num
 * @returns {*}
 * @private
 */
var _checkNumAndType = function (type, num) {
    if (typeof num == 'number') {
        switch (type) {
            case 'm':
                if (num < 0 || num > 60) {
                    return false;
                }
                break;
            case 'h':
                if (num < 0 || num > 24) {
                    return false;
                }
                break;
        }
        return {type: type, digit: num};
    }
}

/**
 * Returns frequency metadata for defined frequency
 * @param freqType
 * @returns {*}
 * @private
 */
var _checkDailyAndTest = function (freqType) {
    switch (freqType) {
        case 'custom':
        case 'daily':
            return {type: freqType, digit: undefined};
            break;
        case 'test':
            return {type: freqType, digit: 0};
    }
    return false;
}


/**
 * Returns frequency metadata
 * @param frequency
 * @returns {*}
 */
FileStreamRotator.getFrequency = function (frequency) {
    var _f = frequency.toLowerCase().match(/^(\d+)([mh])$/)
    if(_f){
        return _checkNumAndType(_f[2], parseInt(_f[1]));
    }

    var dailyOrTest = _checkDailyAndTest(frequency);
    if (dailyOrTest) {
        return dailyOrTest;
    }

    return false;
}

/**
 * Returns a number based on the option string
 * @param size
 * @returns {Number}
 */
FileStreamRotator.parseFileSize = function (size) {
    if(size && typeof size == "string"){
        var _s = size.toLowerCase().match(/^((?:0\.)?\d+)([kmg])$/);
        if(_s){
            switch(_s[2]){
                case 'k':
                    return _s[1]*1024
                case 'm':
                    return _s[1]*1024*1024
                case 'g':
                    return _s[1]*1024*1024*1024
            }
        }
    }
    return null;
};

/**
 * Returns date string for a given format / date_format
 * @param format
 * @param date_format
 * @param {boolean} utc
 * @returns {string}
 */
FileStreamRotator.getDate = function (format, date_format, utc) {
    date_format = date_format || DATE_FORMAT;
    let currentMoment = utc ? moment.utc() : moment().local()
    if (format && staticFrequency.indexOf(format.type) !== -1) {
        switch (format.type) {
            case 'm':
                var minute = Math.floor(currentMoment.minutes() / format.digit) * format.digit;
                return currentMoment.minutes(minute).format(date_format);
                break;
            case 'h':
                var hour = Math.floor(currentMoment.hour() / format.digit) * format.digit;
                return currentMoment.hour(hour).format(date_format);
                break;
            case 'daily':
            case 'custom':
            case 'test':
                return currentMoment.format(date_format);
        }
    }
    return currentMoment.format(date_format);
}

/**
 * Read audit json object from disk or return new object or null
 * @param max_logs
 * @param audit_file
 * @param log_file
 * @returns {Object} auditLogSettings
 * @property {Object} auditLogSettings.keep
 * @property {Boolean} auditLogSettings.keep.days
 * @property {Number} auditLogSettings.keep.amount
 * @property {String} auditLogSettings.auditLog
 * @property {Array} auditLogSettings.files
 * @property {String} auditLogSettings.hashType
 */
FileStreamRotator.setAuditLog = function (max_logs, audit_file, log_file){
    var _rtn = null;
    if(max_logs){
        var use_days = max_logs.toString().substr(-1);
        var _num = max_logs.toString().match(/^(\d+)/);

        if(Number(_num[1]) > 0) {
            var baseLog = path.dirname(log_file.replace(/%DATE%.+/,"_filename"));
            try{
                if(audit_file){
                    var full_path = path.resolve(audit_file);
                    _rtn = JSON.parse(fs.readFileSync(full_path, { encoding: 'utf-8' }));
                }else{
                    var full_path = path.resolve(baseLog + "/" + ".audit.json")
                    _rtn = JSON.parse(fs.readFileSync(full_path, { encoding: 'utf-8' }));
                }
            }catch(e){
                if(e.code !== "ENOENT"){
                    return null;
                }
                _rtn = {
                    keep: {
                        days: false,
                        amount: Number(_num[1])
                    },
                    auditLog: audit_file || baseLog + "/" + ".audit.json",
                    files: []
                };
            }

            _rtn.keep = {
                days: use_days === 'd',
                amount: Number(_num[1])
            };

        }
    }
    return _rtn;
};

/**
 * Write audit json object to disk
 * @param {Object} audit
 * @param {Object} audit.keep
 * @param {Boolean} audit.keep.days
 * @param {Number} audit.keep.amount
 * @param {String} audit.auditLog
 * @param {Array} audit.files
 * @param {String} audit.hashType
 * @param {Boolean} verbose 
 */
FileStreamRotator.writeAuditLog = function(audit, verbose){
    try{
        mkDirForFile(audit.auditLog);
        fs.writeFileSync(audit.auditLog, JSON.stringify(audit,null,4));
    }catch(e){
        if (verbose) {
            console.error(new Date(),"[FileStreamRotator] Failed to store log audit at:", audit.auditLog,"Error:", e);
        }
    }
};


/**
 * Removes old log file
 * @param file
 * @param file.hash
 * @param file.name
 * @param file.date
 * @param file.hashType
 * @param {Boolean} verbose 
 */
function removeFile(file, verbose){
    if(file.hash === crypto.createHash(file.hashType).update(file.name + "LOG_FILE" + file.date).digest("hex")){
        try{
            if (fs.existsSync(file.name)) {
                fs.unlinkSync(file.name);
            }
        }catch(e){
            if (verbose) {
                console.error(new Date(), "[FileStreamRotator] Could not remove old log file: ", file.name);
            }
        }
    }
}

/**
 * Create symbolic link to current log file
 * @param {String} logfile 
 * @param {String} name Name to use for symbolic link 
 * @param {Boolean} verbose 
 */
function createCurrentSymLink(logfile, name, verbose) {
    let symLinkName = name || "current.log"
    let logPath = path.dirname(logfile)
    let logfileName = path.basename(logfile)
    let current = logPath + "/" + symLinkName
    try {
        let stats = fs.lstatSync(current)
        if(stats.isSymbolicLink()){
            fs.unlinkSync(current)
            fs.symlinkSync(logfileName, current)
        }
    } catch (err) {
        if(err && err.code == "ENOENT") {
            try {
                fs.symlinkSync(logfileName, current)
            } catch (e) {
                if (verbose) {
                    console.error(new Date(), "[FileStreamRotator] Could not create symlink file: ", current, ' -> ', logfileName);
                }
            }
        }
    }
}

/**
 * 
 * @param {String} logfile 
 * @param {Boolean} verbose 
 * @param {function} cb 
 */
function createLogWatcher(logfile, verbose, cb){
    if(!logfile) return null
    // console.log("Creating log watcher")
    try {
        let stats = fs.lstatSync(logfile)
        return fs.watch(logfile, function(event,filename){
            // console.log(Date(), event, filename)
            if(event == "rename"){
                try {
                    let stats = fs.lstatSync(logfile)
                    // console.log("STATS:", stats)
                }catch(err){
                    // console.log("ERROR:", err)
                    cb(err,logfile)
                }                    
            }
        })
    }catch(err){
        if(verbose){
            console.log(new Date(),"[FileStreamRotator] Could not add watcher for " + logfile);
        }
    }                    
}

/**
 * Write audit json object to disk
 * @param {String} logfile
 * @param {Object} audit
 * @param {Object} audit.keep
 * @param {Boolean} audit.keep.days
 * @param {Number} audit.keep.amount
 * @param {String} audit.auditLog
 * @param {String} audit.hashType
 * @param {Array} audit.files
 * @param {EventEmitter} stream
 * @param {Boolean} verbose 
 */
FileStreamRotator.addLogToAudit = function(logfile, audit, stream, verbose){
    if(audit && audit.files){
        // Based on contribution by @nickbug - https://github.com/nickbug
        var index = audit.files.findIndex(function(file) {
            return (file.name === logfile);
        });
        if (index !== -1) {
            // nothing to do as entry already exists.
            return audit;
        }
        var time = Date.now();
        audit.files.push({
            date: time,
            name: logfile,
            hash: crypto.createHash(audit.hashType).update(logfile + "LOG_FILE" + time).digest("hex")
        });

        if(audit.keep.days){
            var oldestDate = moment().subtract(audit.keep.amount,"days").valueOf();
            var recentFiles = audit.files.filter(function(file){
                if(file.date > oldestDate){
                    return true;
                }
                file.hashType = audit.hashType
                removeFile(file, verbose);
                stream.emit("logRemoved", file)
                return false;
            });
            audit.files = recentFiles;
        }else{
            var filesToKeep = audit.files.splice(-audit.keep.amount);
            if(audit.files.length > 0){
                audit.files.filter(function(file){
                    file.hashType = audit.hashType
                    removeFile(file, verbose);
                    stream.emit("logRemoved", file)
                    return false;
                })
            }
            audit.files = filesToKeep;
        }

        FileStreamRotator.writeAuditLog(audit, verbose);
    }

    return audit;
}

/**
 *
 * @param options
 * @param options.filename
 * @param options.frequency
 * @param options.verbose
 * @param options.date_format
 * @param options.size
 * @param options.max_logs
 * @param options.audit_file
 * @param options.file_options
 * @param options.utc
 * @param options.extension File extension to be added at the end of the filename
 * @param options.watch_log
 * @param options.create_symlink
 * @param options.symlink_name
 * @param options.audit_hash_type Hash to be used to add to the audit log (md5, sha256)
 * @returns {Object} stream
 */
FileStreamRotator.getStream = function (options) {
    var frequencyMetaData = null;
    var curDate = null;
    var self = this;

    if (!options.filename) {
        console.error(new Date(),"[FileStreamRotator] No filename supplied. Defaulting to STDOUT");
        return process.stdout;
    }

    if (options.frequency) {
        frequencyMetaData = self.getFrequency(options.frequency);
    }

    let auditLog = self.setAuditLog(options.max_logs, options.audit_file, options.filename);
    // Thanks to Means88 for PR.
    if (auditLog != null) {
        auditLog.hashType = (options.audit_hash_type !== undefined ? options.audit_hash_type : 'md5');
    }
    self.verbose = (options.verbose !== undefined ? options.verbose : true);

    var fileSize = null;
    var fileCount = 0;
    var curSize = 0;
    if(options.size){
        fileSize = FileStreamRotator.parseFileSize(options.size);
    }

    var dateFormat = (options.date_format || DATE_FORMAT);
    if(frequencyMetaData && frequencyMetaData.type == "daily"){
        if(!options.date_format){
            dateFormat = "YYYY-MM-DD";
        }
        if(moment().format(dateFormat) != moment().endOf("day").format(dateFormat) || moment().format(dateFormat) == moment().add(1,"day").format(dateFormat)){
            if(self.verbose){
                console.log(new Date(),"[FileStreamRotator] Changing type to custom as date format changes more often than once a day or not every day");
            }
            frequencyMetaData.type = "custom";
        }
    }

    if (frequencyMetaData) {
        curDate = (options.frequency ? self.getDate(frequencyMetaData,dateFormat, options.utc) : "");
    }

    options.create_symlink = options.create_symlink || false;
    options.extension = options.extension || ""
    var filename = options.filename;
    var oldFile = null;
    var logfile = filename + (curDate ? "." + curDate : "");
    if(filename.match(/%DATE%/)){
        logfile = filename.replace(/%DATE%/g,(curDate?curDate:self.getDate(null,dateFormat, options.utc)));
    }

    if(fileSize){
        var lastLogFile = null;
        var t_log = logfile;
        var f = null;
        if(auditLog && auditLog.files && auditLog.files instanceof Array && auditLog.files.length > 0){
            var lastEntry = auditLog.files[auditLog.files.length - 1].name;
            if(lastEntry.match(t_log)){
                var lastCount = lastEntry.match(t_log + "\\.(\\d+)");
                // Thanks for the PR contribution from @andrefarzat - https://github.com/andrefarzat
                if(lastCount){                    
                    t_log = lastEntry;
                    fileCount = lastCount[1];
                }
            }
        }

        if (fileCount == 0 && t_log == logfile) {
            t_log += options.extension
        }

        while(f = fs.existsSync(t_log)){
            lastLogFile = t_log;
            fileCount++;
            t_log = logfile + "." + fileCount + options.extension;
        }
        if(lastLogFile){
            var lastLogFileStats = fs.statSync(lastLogFile);
            if(lastLogFileStats.size < fileSize){
                t_log = lastLogFile;
                fileCount--;
                curSize = lastLogFileStats.size;
            }
        }
        logfile = t_log;
    } else {
        logfile += options.extension
    }

    if (self.verbose) {
        console.log(new Date(),"[FileStreamRotator] Logging to: ", logfile);
    }

    mkDirForFile(logfile);

    var file_options = options.file_options || {flags: 'a'};
    var rotateStream = fs.createWriteStream(logfile, file_options);
    if ((curDate && frequencyMetaData && (staticFrequency.indexOf(frequencyMetaData.type) > -1)) || fileSize > 0) {
        if (self.verbose) {
            console.log(new Date(),"[FileStreamRotator] Rotating file: ", frequencyMetaData?frequencyMetaData.type:"", fileSize?"size: " + fileSize:"");
        }
        var stream = new EventEmitter();
        stream.auditLog = auditLog;
        stream.end = function(){
            rotateStream.end.apply(rotateStream,arguments);
        };
        BubbleEvents(rotateStream,stream);

        stream.on('close', function(){
            if (logWatcher) {
                logWatcher.close()
            }
        })

        stream.on("new",function(newLog){
            // console.log("new log", newLog)
            stream.auditLog = self.addLogToAudit(newLog,stream.auditLog, stream, self.verbose)
            if(options.create_symlink){
                createCurrentSymLink(newLog, options.symlink_name, self.verbose)
            }
            if(options.watch_log){
                stream.emit("addWatcher", newLog)
            }
        });
        
        var logWatcher;
        stream.on("addWatcher", function(newLog){
            if (logWatcher) {
                logWatcher.close()
            }
            if(!options.watch_log){
                return
            }
            // console.log("ADDING WATCHER", newLog)
            logWatcher = createLogWatcher(newLog, self.verbose, function(err,newLog){
                stream.emit('createLog', newLog)
            })        
        })

        stream.on("createLog",function(file){
            try {
                let stats = fs.lstatSync(file)
            }catch(err){
                if(rotateStream && rotateStream.end == "function"){
                    rotateStream.end();
                }
                rotateStream = fs.createWriteStream(file, file_options);
                stream.emit('new',file);
                BubbleEvents(rotateStream,stream);
            }
        });


        stream.write = (function (str, encoding) {
            var newDate = frequencyMetaData ? this.getDate(frequencyMetaData, dateFormat, options.utc) : curDate;
            if (newDate != curDate || (fileSize && curSize > fileSize)) {
                var newLogfile = filename + (curDate && frequencyMetaData ? "." + newDate : "");
                if(filename.match(/%DATE%/) && curDate){
                    newLogfile = filename.replace(/%DATE%/g,newDate);
                }

                if(fileSize && curSize > fileSize){
                    fileCount++;
                    newLogfile += "." + fileCount + options.extension;
                }else{
                    // reset file count
                    fileCount = 0;
                    newLogfile += options.extension
                }
                curSize = 0;

                if (self.verbose) {
                    console.log(new Date(),require('util').format("[FileStreamRotator] Changing logs from %s to %s", logfile, newLogfile));
                }
                curDate = newDate;
                oldFile = logfile;
                logfile = newLogfile;
                // Thanks to @mattberther https://github.com/mattberther for raising it again.
                if(options.end_stream === true){
                    rotateStream.end();
                }else{
                    rotateStream.destroy();
                }

                mkDirForFile(logfile);

                rotateStream = fs.createWriteStream(newLogfile, file_options);
                stream.emit('new',newLogfile);
                stream.emit('rotate',oldFile, newLogfile);
                BubbleEvents(rotateStream,stream);
            }
            rotateStream.write(str, encoding);
            // Handle length of double-byte characters
            curSize += Buffer.byteLength(str, encoding);
        }).bind(this);
        process.nextTick(function(){
            stream.emit('new',logfile);
        })
        stream.emit('new',logfile)
        return stream;
    } else {
        if (self.verbose) {
            console.log(new Date(),"[FileStreamRotator] File won't be rotated: ", options.frequency, options.size);
        }
        process.nextTick(function(){
            rotateStream.emit('new',logfile);
        })
        return rotateStream;
    }
}

/**
 * Check and make parent directory
 * @param pathWithFile
 */
var mkDirForFile = function(pathWithFile){
    var _path = path.dirname(pathWithFile);
    _path.split(path.sep).reduce(
        function(fullPath, folder) {
            fullPath += folder + path.sep;
            // Option to replace existsSync as deprecated. Maybe in a future release.
            // try{
            //     var stats = fs.statSync(fullPath);
            //     console.log('STATS',fullPath, stats);
            // }catch(e){
            //     fs.mkdirSync(fullPath);
            //     console.log("STATS ERROR",e)
            // }
            if (!fs.existsSync(fullPath)) {
                try{
                    fs.mkdirSync(fullPath);
                }catch(e){
                    if(e.code !== 'EEXIST'){
                        throw e;
                    }
                }
            }
            return fullPath;
        },
        ''
    );
};


/**
 * Bubbles events to the proxy
 * @param emitter
 * @param proxy
 * @constructor
 */
var BubbleEvents = function BubbleEvents(emitter,proxy){
    emitter.on('close',function(){
        proxy.emit('close');
    })
    emitter.on('finish',function(){
        proxy.emit('finish');
    })
    emitter.on('error',function(err){
        proxy.emit('error',err);
    })
    emitter.on('open',function(fd){
        proxy.emit('open',fd);
    })
}