|
1 |
| -import winston from "@nodefactory/winston"; |
2 |
| -import LokiTransport from "winston-loki"; |
3 |
| -import Transports from "winston-transport"; |
| 1 | +import pino from "pino"; |
4 | 2 |
|
5 |
| -const format = winston.format.printf( |
6 |
| - ({ level, message, labels, timestamp, requestId }) => { |
7 |
| - message = winston.format |
8 |
| - .colorize({ all: false, message: true }) |
9 |
| - .colorize(level, message); |
10 |
| - let log = `${timestamp} [${ |
11 |
| - labels.module |
12 |
| - }] ${level.toUpperCase()}: ${message}`; |
13 |
| - if (requestId) { |
14 |
| - log += " RequestId: " + requestId; |
15 |
| - } |
16 |
| - return log; |
17 |
| - } |
18 |
| -); |
| 3 | +export let logger: pino.Logger; |
19 | 4 |
|
20 |
| -const transportsConfig: Transports[] = [ |
21 |
| - new winston.transports.Console({ |
22 |
| - format: winston.format.combine( |
23 |
| - winston.format.timestamp(), |
24 |
| - winston.format.align(), |
25 |
| - format |
26 |
| - ), |
27 |
| - }), |
28 |
| -]; |
29 |
| - |
30 |
| -if (process.env.WINSTON_LOKI_URL) { |
31 |
| - transportsConfig.push( |
32 |
| - new LokiTransport({ |
33 |
| - host: process.env.WINSTON_LOKI_URL, |
34 |
| - batching: process.env.WINSTON_LOKI_BATCHING == "true", |
35 |
| - basicAuth: process.env.WINSTON_LOKI_BASIC_AUTH ?? undefined, |
36 |
| - labels: { |
37 |
| - app: process.env.WINSTON_LOKI_APP_NAME ?? "", |
38 |
| - }, |
39 |
| - }) |
| 5 | +if (process.env.LOG_FILE) { |
| 6 | + logger = pino({ |
| 7 | + level: process.env.LOG_LEVEL || "debug", |
| 8 | + }); |
| 9 | +} else { |
| 10 | + logger = pino( |
| 11 | + { |
| 12 | + level: process.env.LOG_LEVEL || "debug", |
| 13 | + prettyPrint: true, |
| 14 | + }, |
| 15 | + process.stdout |
40 | 16 | );
|
41 | 17 | }
|
42 |
| - |
43 |
| -export const logger = winston.createLogger({ |
44 |
| - level: process.env.LOG_LEVEL || "debug", |
45 |
| - |
46 |
| - format: winston.format.json({}), |
47 |
| - defaultMeta: { |
48 |
| - labels: { |
49 |
| - module: "default", |
50 |
| - }, |
51 |
| - }, |
52 |
| - transports: transportsConfig, |
53 |
| -}); |
0 commit comments