Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

185
Vistas
How to save winston log file into mysql database?

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 ?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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'});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda