|
1 | 1 | import pino, { |
2 | 2 | DestinationStream, |
| 3 | + LogDescriptor, |
3 | 4 | LoggerOptions, |
4 | 5 | stdTimeFunctions, |
5 | | - LogDescriptor, |
6 | 6 | } from "pino"; |
7 | 7 | import pretty from "pino-pretty"; |
8 | 8 |
|
9 | 9 | const env = process.env.NODE_ENV; |
10 | | -const useJsonLogging = env === "production" || env === "staging"; |
11 | 10 |
|
12 | 11 | const errorSerializer = (err: any) => { |
13 | 12 | if (!err) return err; |
@@ -100,43 +99,38 @@ export const createLogger = ({ |
100 | 99 |
|
101 | 100 | let transport: DestinationStream; |
102 | 101 |
|
103 | | - if (useJsonLogging) { |
104 | | - pinoOptions.timestamp = stdTimeFunctions.isoTime; |
105 | | - pinoOptions.formatters = { |
106 | | - level: (label) => ({ level: label }), |
| 102 | + pinoOptions.timestamp = stdTimeFunctions.isoTime; |
| 103 | + pinoOptions.formatters = { |
| 104 | + level: (label) => ({ level: label }), |
| 105 | + }; |
| 106 | + if (env === "production") { |
| 107 | + pinoOptions.redact = { |
| 108 | + paths: [ |
| 109 | + "*.password", |
| 110 | + "*.token", |
| 111 | + "*.key", |
| 112 | + "*.secret", |
| 113 | + "req.headers.authorization", |
| 114 | + "Authorization", |
| 115 | + ], |
| 116 | + censor: "[REDACTED]", |
107 | 117 | }; |
108 | | - if (env === "production") { |
109 | | - pinoOptions.redact = { |
110 | | - paths: [ |
111 | | - "*.password", |
112 | | - "*.token", |
113 | | - "*.key", |
114 | | - "*.secret", |
115 | | - "req.headers.authorization", |
116 | | - "Authorization", |
117 | | - ], |
118 | | - censor: "[REDACTED]", |
119 | | - }; |
120 | | - } |
121 | | - return pino(pinoOptions); |
122 | | - } else { |
123 | | - // For local development, use pino-pretty |
124 | | - transport = pretty({ |
125 | | - colorize: true, |
126 | | - translateTime: "HH:MM:ss", |
127 | | - ignore: "pid,hostname,service,component", // pid,hostname,service are in base, component is per-log |
128 | | - messageFormat: (log: LogDescriptor, messageKey: string) => { |
129 | | - const currentService = log.service as string; |
130 | | - const currentComponent = log.component as string | undefined; |
131 | | - const message = (log[messageKey] || log.msg || "") as string; |
132 | | - |
133 | | - if (currentComponent) { |
134 | | - return `[${currentService}] (${currentComponent}) ${message}`; |
135 | | - } |
136 | | - return `[${currentService}] ${message}`; |
137 | | - }, |
138 | | - }); |
139 | 118 | } |
| 119 | + transport = pretty({ |
| 120 | + colorize: true, |
| 121 | + translateTime: "HH:MM:ss", |
| 122 | + ignore: "pid,hostname,service,component", // pid,hostname,service are in base, component is per-log |
| 123 | + messageFormat: (log: LogDescriptor, messageKey: string) => { |
| 124 | + const currentService = log.service as string; |
| 125 | + const currentComponent = log.component as string | undefined; |
| 126 | + const message = (log[messageKey] || log.msg || "") as string; |
| 127 | + |
| 128 | + if (currentComponent) { |
| 129 | + return `[${currentService}] (${currentComponent}) ${message}`; |
| 130 | + } |
| 131 | + return `[${currentService}] ${message}`; |
| 132 | + }, |
| 133 | + }); |
140 | 134 |
|
141 | 135 | return pino(pinoOptions, transport); |
142 | 136 | }; |
0 commit comments