|
3 | 3 | import type { LoggerConfig, LogLevel } from '@objectstack/spec/system'; |
4 | 4 | import type { Logger } from '@objectstack/spec/contracts'; |
5 | 5 |
|
| 6 | +// Re-export the contract type so consumers can do |
| 7 | +// `import type { Logger } from '@objectstack/core/logger'` without also |
| 8 | +// pulling `@objectstack/spec` into their bundle graph manually. |
| 9 | +export type { Logger }; |
| 10 | + |
6 | 11 | const LEVEL_ORDER: Record<LogLevel, number> = { |
7 | 12 | debug: 0, |
8 | 13 | info: 1, |
@@ -115,10 +120,23 @@ export class ObjectLogger implements Logger { |
115 | 120 |
|
116 | 121 | const out = line + '\n'; |
117 | 122 |
|
118 | | - if (level === 'error' || level === 'fatal') { |
119 | | - process.stderr?.write(out); |
120 | | - } else { |
121 | | - process.stdout?.write(out); |
| 123 | + // Browser-safe output: prefer process streams when available, otherwise |
| 124 | + // fall back to console. The previous unguarded `process.stderr?.write` |
| 125 | + // throws `ReferenceError: process is not defined` in browsers because |
| 126 | + // `process` itself is the missing global, not just its `stderr` field. |
| 127 | + if (typeof process !== 'undefined' && (process as any).stderr) { |
| 128 | + if (level === 'error' || level === 'fatal') { |
| 129 | + (process as any).stderr.write(out); |
| 130 | + } else { |
| 131 | + (process as any).stdout?.write(out); |
| 132 | + } |
| 133 | + } else if (typeof console !== 'undefined') { |
| 134 | + const fn = |
| 135 | + level === 'error' || level === 'fatal' ? console.error |
| 136 | + : level === 'warn' ? console.warn |
| 137 | + : level === 'debug' ? console.debug |
| 138 | + : console.log; |
| 139 | + fn(line); |
122 | 140 | } |
123 | 141 |
|
124 | 142 | if (this.fileStream) { |
|
0 commit comments