import winston from "winston"; import { ElasticsearchTransport } from "winston-elasticsearch"; import { Client } from "@elastic/elasticsearch"; const transports: winston.transport[] = [ new winston.transports.Console({ format: winston.format.combine( winston.format.colorize(), winston.format.timestamp(), winston.format.printf( ({ level, timestamp, logData, responseBody, requestBody, ...payload }) => `${level} ${timestamp} ${JSON.stringify(Object.assign(payload, logData), null, 4)}`, ), ), }), ]; if (process.env.LOG_ENABLED !== undefined && process.env.LOG_ENABLED === "true") { transports.push( new ElasticsearchTransport({ level: "info", index: "app-log-test-winston-index", format: winston.format.combine(winston.format.timestamp(), winston.format.json()), client: new Client({ node: `${process.env.ELASTICSEARCH_PROTOCOL}://${process.env.ELASTICSEARCH_HOST}:${process.env.ELASTICSEARCH_PORT}`, }), transformer: (payload) => { const { logData: additional, ...rest } = payload.meta; return { level: payload.level, ...rest, ...additional, requestBody: process.env.LOG_LEVEL === "debug" ? JSON.stringify(rest.requestBody) : undefined, responseBody: process.env.LOG_LEVEL === "debug" ? JSON.stringify(rest.responseBody) : undefined, }; }, }), ); } const logger = winston.createLogger({ levels: winston.config.syslog.levels, defaultMeta: { serviceName: "jws-sos" }, transports, }); export default logger;