feat: allow disable logger by .env
This commit is contained in:
parent
5749b85323
commit
f4723e4335
1 changed files with 26 additions and 17 deletions
|
|
@ -1,16 +1,29 @@
|
|||
import winston from "winston";
|
||||
import { ElasticsearchTransport } from "winston-elasticsearch";
|
||||
import elasticsearch from "../services/elasticsearch";
|
||||
import { Client } from "@elastic/elasticsearch";
|
||||
|
||||
const logger = winston.createLogger({
|
||||
levels: winston.config.syslog.levels,
|
||||
defaultMeta: { serviceName: "jws-sos" },
|
||||
transports: [
|
||||
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: elasticsearch,
|
||||
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 {
|
||||
|
|
@ -24,17 +37,13 @@ const logger = winston.createLogger({
|
|||
};
|
||||
},
|
||||
}),
|
||||
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)}`,
|
||||
),
|
||||
),
|
||||
}),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
const logger = winston.createLogger({
|
||||
levels: winston.config.syslog.levels,
|
||||
defaultMeta: { serviceName: "jws-sos" },
|
||||
transports,
|
||||
});
|
||||
|
||||
export default logger;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue