Skip to content

Commit b7f1052

Browse files
committed
fix: pino pretty not working correctly in cli logs
1 parent aec9ac6 commit b7f1052

File tree

3 files changed

+27
-29
lines changed

3 files changed

+27
-29
lines changed

lib/bin/cli.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
11
import path from "node:path";
22
import { pathToFileURL } from "node:url";
33

4+
import pino from "pino";
5+
import pretty from "pino-pretty";
6+
47
import { parseCliArgs, usageText } from "./cli-args";
58

9+
import { setLogger } from "@/shared/logger";
610
import { startWatch } from "@/runtime/watch-runner";
711

12+
function createCliLogger() {
13+
const stream = pretty({
14+
colorize: true,
15+
sync: true,
16+
translateTime: "SYS:HH:MM:ss",
17+
ignore: "pid,hostname",
18+
levelFirst: true,
19+
});
20+
21+
return pino(
22+
{
23+
name: "snuts",
24+
level: process.env["LOG_LEVEL"] ?? "info",
25+
},
26+
stream,
27+
);
28+
}
29+
830
function writeOutput(message: string, target: "stdout" | "stderr"): void {
931
const content = `${message}\n`;
1032

@@ -17,6 +39,8 @@ function writeOutput(message: string, target: "stdout" | "stderr"): void {
1739
}
1840

1941
export async function runCli(argv: string[]): Promise<number> {
42+
setLogger(createCliLogger());
43+
2044
const parsedCommand = parseCliArgs(argv);
2145

2246
if (parsedCommand.type === "help") {

lib/shared/logger/index.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
1-
import { createRequire } from "module";
2-
31
import pino from "pino";
42

5-
function hasPinoPretty(): boolean {
6-
try {
7-
const require = createRequire(process.cwd() + "/index.js");
8-
require.resolve("pino-pretty");
9-
return true;
10-
} catch {
11-
return false;
12-
}
13-
}
14-
153
/**
164
* Minimal logger interface used throughout the library.
175
* Matches the subset of pino's API that is actually called internally,
@@ -54,23 +42,9 @@ export const silentLogger: ILogger = {
5442
};
5543

5644
function createDefaultLogger(): ILogger {
57-
const isDev = process.env["NODE_ENV"] !== "production";
58-
const usePretty = isDev && hasPinoPretty();
59-
6045
return pino({
61-
name: "test-smell-watcher",
46+
name: "snuts",
6247
level: process.env["LOG_LEVEL"] ?? "info",
63-
transport: usePretty
64-
? {
65-
target: "pino-pretty",
66-
options: {
67-
colorize: true,
68-
translateTime: "SYS:HH:MM:ss",
69-
ignore: "pid,hostname",
70-
levelFirst: true,
71-
},
72-
}
73-
: undefined,
7448
});
7549
}
7650

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
"jest": "^29.7.0",
9595
"jest-environment-jsdom": "^29.7.0",
9696
"lint-staged": "^16.2.4",
97-
"pino-pretty": "^13.1.2",
9897
"prettier": "^3.6.2",
9998
"rimraf": "^6.0.1",
10099
"ts-jest": "^29.2.0",
@@ -111,6 +110,7 @@
111110
"esquery": "^1.6.0",
112111
"glob": "^11.0.3",
113112
"lodash": "^4.17.21",
114-
"pino": "^10.1.0"
113+
"pino": "^10.1.0",
114+
"pino-pretty": "^13.1.2"
115115
}
116116
}

0 commit comments

Comments
 (0)