如何在node.js中使用winston将日志存储到mysql数据库

js4nwp54  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(511)

我正在使用winston为我的应用程序做日志记录。我使用以下方法完成了文件传输:

class LoggerHelper extends BaseHelper {

constructor(_cApp) {
    super(_cApp);
    this._props = {};
}

initialize() {
    this._prepareConfigs();
    this._createTransportObj();
    this._createLoggerObj();
}

_prepareConfigs() {
    this._props.dirname = this._configs.logsFolder;
    this._props.filename = this._configs.filenameConvention;
    this._props.datePattern = this._configs.datePattern;
    this._props.maxSize = this._configs.maxSize;
    this._props.level = this._configs.level;
    this._props.timestamp = this._configs.timestamp;
    this._props.prettyPrint = this._configs.prettyPrint;
}

_createTransportObj() {
    var DailyRotateFile = winston.transports.DailyRotateFile;
    this._transport = new DailyRotateFile(this._props);
}

_createLoggerObj() {
    this._logger = winston.createLogger({
        transports: [this._transport],   
        exitOnError: false
    });
}

_log(type, error, description, stage, vars) {
    var logMsg = {};

    var msg = '';
    var fileIndex = 3;
    if(this._isError(error)) {
        var err = error;
        msg = error.message;
        fileIndex = 1;
    } else {
        var err = new Error();
        msg = error;
    }
    var caller_line = err.stack.split("at ")[fileIndex];

    var index = caller_line.indexOf("(");
    var lastIndex = caller_line.lastIndexOf(")");
    index = caller_line.slice(index + 1, lastIndex);

    var line = index.match(/:[0-9]+:/).toLocaleString();
    line = line.replace(/[^0-9]/g, '');

    var curTime = new FE.utils.date();
    var timestamp = curTime.format('YYYY-MM-DD HH:MM:SS');

    logMsg.level = type || 'info';
    logMsg.time = timestamp || '';
    logMsg.msg = msg || '';
    logMsg.desc = description || '';
    logMsg.stg = stage || '000';
    logMsg.file = index || 'Not Found';
    logMsg.stack = err.stack || 'Not Found';
    logMsg.line = line || 'Not Found';
    var logStr = JSON.stringify(logMsg);
    this._logger.log(type, logMsg);
}

info(error, description, stage, vars) {
    return this._log('info', error, description, stage, vars);
}

error(error, description, stage, vars) {
    return this._log('error', error, description, stage, vars);
}

warn(error, description, stage, vars) {
    return this._log('warn', error, description, stage, vars);
}

verbose(error, description, stage, vars) {
    return this._log('verbose', error, description, stage, vars);
}

debug(error, description, stage, vars) {
    return this._log('debug', error, description, stage, vars);
}

silly(error, description, stage, vars) {
    return this._log('silly', error, description, stage, vars);
}

/**
 * Checks if value is an Error or Error-like object
 * @static
 * @param  {Any}     val Value to test
 * @return {Boolean}     Whether the value is an Error or Error-like object
 */
_isError(val) {
    return !!val && typeof val === 'object' && (
        val instanceof Error || (
            val.hasOwnProperty('message') && val.hasOwnProperty('stack')
        )
    );
}
}

module.exports = LoggerHelper;

现在我也想将日志存储到mysql db表中。我确实遇到了一个用于mongo的winston插件,但是我没有看到任何支持将它存储到mysql数据库中。我有办法做到这一点吗?提前谢谢。

6bc51xsx

6bc51xsx1#

我最近也面临同样的问题。在做了一些研究之后,我发现了一个包“winstonsqltransport”,可以将日志保存到mysql中。看这个:

const { Logger } = require('winston');
const { SQLTransport } = require('./../lib/winston-sql-transport');

const logger = new Logger({
  transports: [
    new SQLTransport({
      tableName: 'winston_logs',
    })]
});

module.exports = logger;

相关问题