Source: models/logtail.js

const EventEmitter = require('events');
const fs = require('fs');
const readLastLines = require('read-last-lines');
const glob = require('glob');

/**
 * Class offers tail of logfile
 * @class
 */
class LogTail extends EventEmitter {

    /**
     * @constructor
     */
    constructor(logger) {
        super();
        this.logger = logger;
    }


    tailNumberOfLines(logFilePath, numLines, callback) {
        readLastLines.read(logFilePath, numLines)
            .then((lines) => {
                lines = this.formatLogLines(lines);
                callback(lines);
            })
    }

    formatLogLines(lines) {
        var retVal = lines;

        retVal = retVal.replace(/\n/g, '<br>');
        retVal = retVal.replace(/INFO/g, '<span style="color:green;">INFO</span>');
        retVal = retVal.replace(/DEBUG/g, '<span style="color:orange;">INFO</span>');
        retVal = retVal.replace(/ERROR/g, '<span style="color:red;">INFO</span>');
        retVal = retVal.replace(/KATSYS/g, '<span style="color:cyan;">KATSYS</span>');
        retVal = retVal.replace(/ALARM/g, '<span style="color:red;">ALARM</span>');

        retVal = '<span style="color:white">' + retVal + "</span>";
        return retVal;
    }


    /**
     * returns list of logfiles in a given folder
     * @param {string} folder folder to look for logfiles
     * @param {*} callback list of files in folder as array
     */
    getListOfLogfilesInFolder(folder, callback) {

        var fulllogpath = global.appRoot + '/logs/' + folder + '/*.log';
        console.log('fullogpath: ' + fulllogpath);
        glob(fulllogpath, null, (er, files) => {
            if (er) {
                console.log('CORE | MODELS | LOGTAIL | Error getting list of logfiles from filesystem: ' + er.message);
                return callback(null);
            } else {
                return callback(files);
            }
        })
    }



}

module.exports = LogTail;