Skip to content

Commit 7e8548f

Browse files
authored
feat: allow to pretty-print in production (#109)
* feat: allow to pretty-print in production * chore: changen env name to LOG_FORMAT
1 parent ee7b585 commit 7e8548f

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Version: Used in the /about endpoint. Useful to expose the Docker image version.
2+
# LOG_FORMAT=pretty
23
# LOG_LEVEL=info
34
# VESION=1.0.0
45

apps/api/src/logger.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,25 @@ import pino from 'pino';
22

33
export type Logger = pino.Logger;
44

5-
const loggerConfigEnv =
6-
process.env.NODE_ENV === 'production'
7-
? {}
8-
: {
5+
function getLogger() {
6+
// Uses pretty print if env.LOG_FORMAT is set to 'pretty'. By default, it will also use it for non-production environments.
7+
// If the env.LOG_FORMAT is not 'pretty', it defaults to a JSON logger.
8+
const usePrettyPrint = process.env.LOG_FORMAT
9+
? process.env.LOG_FORMAT === 'pretty'
10+
: process.env.NODE_ENV !== 'production';
11+
12+
const loggerConfigEnv = usePrettyPrint
13+
? {
914
transport: {
1015
target: 'pino-pretty',
1116
},
12-
};
17+
}
18+
: {};
19+
20+
return pino({
21+
...loggerConfigEnv,
22+
level: process.env.LOG_LEVEL ?? 'info',
23+
});
24+
}
1325

14-
export const logger = pino({
15-
...loggerConfigEnv,
16-
level: process.env.LOG_LEVEL ?? 'info',
17-
});
26+
export const logger = getLogger();

0 commit comments

Comments
 (0)