Skip to content

Commit 8f5cd99

Browse files
committed
fix: refactor initPinoLogger to use createRequire for synchronous import and enhance pino-pretty availability check
1 parent 5358385 commit 8f5cd99

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

packages/core/src/logger.ts

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import { createRequire } from 'module';
12
import type { LoggerConfig, LogLevel } from '@objectstack/spec/system';
23
import type { Logger } from '@objectstack/spec/contracts';
34

5+
const require = createRequire(import.meta.url);
6+
47
/**
58
* Universal Logger Implementation
69
*
@@ -49,14 +52,12 @@ export class ObjectLogger implements Logger {
4952
/**
5053
* Initialize Pino logger for Node.js
5154
*/
52-
private async initPinoLogger() {
55+
private initPinoLogger() {
5356
if (!this.isNode) return;
5457

5558
try {
56-
// Dynamic import for Pino (Node.js only)
57-
// Use dynamic import for ESM compatibility
58-
const pinoModule = await import('pino');
59-
const pino = pinoModule.default || pinoModule;
59+
// Synchronous import for Pino using createRequire (works in ESM)
60+
const pino = require('pino');
6061

6162
// Build Pino options
6263
const pinoOptions: any = {
@@ -77,15 +78,34 @@ export class ObjectLogger implements Logger {
7778

7879
// Console transport
7980
if (this.config.format === 'pretty') {
80-
targets.push({
81-
target: 'pino-pretty',
82-
options: {
83-
colorize: true,
84-
translateTime: 'SYS:standard',
85-
ignore: 'pid,hostname'
86-
},
87-
level: this.config.level
88-
});
81+
// Check if pino-pretty is available
82+
let hasPretty = false;
83+
try {
84+
require.resolve('pino-pretty');
85+
hasPretty = true;
86+
} catch (e) {
87+
// ignore
88+
}
89+
90+
if (hasPretty) {
91+
targets.push({
92+
target: 'pino-pretty',
93+
options: {
94+
colorize: true,
95+
translateTime: 'SYS:standard',
96+
ignore: 'pid,hostname'
97+
},
98+
level: this.config.level
99+
});
100+
} else {
101+
console.warn('[Logger] pino-pretty not found. Install it for pretty logging: pnpm add -D pino-pretty');
102+
// Fallback to text/simple
103+
targets.push({
104+
target: 'pino/file',
105+
options: { destination: 1 },
106+
level: this.config.level
107+
});
108+
}
89109
} else if (this.config.format === 'json') {
90110
// JSON to stdout
91111
targets.push({

0 commit comments

Comments
 (0)