How to save winston log file into mysql database?

Issue

I’ve configured the winston logger like this :

import winston from "winston";
const { SqlTransport } = require("winston-sql-transport");

const transportConfig = {
  client: "mysql2",
  connection: {
    host: "localhost",
    user: "root",
    password: "Mahdi54321",
    database: "todos",
    // port: "3307",
  },
  tableName: "logs",
};

const alignColorsAndTime = winston.format.combine(
  winston.format.colorize({
    all: true,
  }),
  winston.format.label({
    label: "[LOGGER]",
  }),
  winston.format.timestamp({
    format: "YYYY-MM-DD HH:mm:ss",
  }),
  winston.format.printf(
    (info) => `${info.label} ${info.timestamp} ${info.level} : ${info.message}`
  )
);

export const logger = winston.createLogger({
  level: "debug",
  transports: [
    new winston.transports.Console({
      format: winston.format.combine(
        winston.format.colorize(),
        alignColorsAndTime
      ),
    }),
    new winston.transports.File({
      filename: "logs/example.log",
      format: winston.format.combine(
        winston.format.timestamp({
          format: "YYYY-MM-DD HH:mm:ss",
        }),
        winston.format.json()
      ),
    }),
    new SqlTransport(transportConfig),
  ],
});

It only saves the first log and the next one is only saved in the file and not the database .

I’m guessing the new SqlTransport(transportConfig), doesn’t run everytime so it can save every log to database .

How can I save every log one after another into mysql database ?

Solution

The package you use is deprecated, instead, you can use the winston-mysql package: https://www.npmjs.com/package/winston-mysql

implementation example as per documentation:

const options_default = {
    host: 'localhost',
    user: 'logger',
    password: 'logger*test',
    database: 'WinstonTest',
    table: 'sys_logs_default'
};

//custom log table fields
const options_custom = {
    host: 'localhost',
    user: 'logger',
    password: 'logger*test',
    database: 'WinstonTest',
    table: 'sys_logs_custom',
    fields: {level: 'mylevel', meta: 'metadata', message: 'source', timestamp: 'addDate'}
};

//meta json log table fields
const options_json = {
    host: 'localhost',
    user: 'logger',
    password: 'logger*test',
    database: 'WinstonTest',
    table: 'sys_logs_json'
};

const logger = winston.createLogger({
    level: 'debug',
    format: winston.format.json(),
    defaultMeta: { service: 'user-service' },
    transports: [
        new winston.transports.Console({
            format: winston.format.simple(),
        }),
        // or use: options_custom / options_json
        new winstonMysql(options_default),
    ],
});

const rnd = Math.floor(Math.random() * 1000);
const msg = `test message ${rnd}`;

logger.debug(msg, {message: msg, type: 'demo'});
logger.error(msg, {message: msg, type: 'demo'});
logger.info(msg, {message: msg, type: 'demo'});
logger.warn(msg, {message: msg, type: 'demo'});

Answered By – Alaindeseine

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published