Skip to content

Commit dad6a8d

Browse files
committed
use pretty
1 parent 54e28a7 commit dad6a8d

File tree

1 file changed

+31
-37
lines changed

1 file changed

+31
-37
lines changed

packages/utils/src/logger.ts

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import pino, {
22
DestinationStream,
3+
LogDescriptor,
34
LoggerOptions,
45
stdTimeFunctions,
5-
LogDescriptor,
66
} from "pino";
77
import pretty from "pino-pretty";
88

99
const env = process.env.NODE_ENV;
10-
const useJsonLogging = env === "production" || env === "staging";
1110

1211
const errorSerializer = (err: any) => {
1312
if (!err) return err;
@@ -100,43 +99,38 @@ export const createLogger = ({
10099

101100
let transport: DestinationStream;
102101

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]",
107117
};
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-
});
139118
}
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+
});
140134

141135
return pino(pinoOptions, transport);
142136
};

0 commit comments

Comments
 (0)