Skip to content

Commit d480e23

Browse files
committedFeb 1, 2025·
chore: several minor updates
1 parent bbf7df7 commit d480e23

File tree

8 files changed

+32
-25
lines changed

8 files changed

+32
-25
lines changed
 

‎src/formatters/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export * from './json'
66
export * from './text'
77
export * from './types'
88

9-
export function createFormatter(format: 'json' | 'text' = 'text', options: { colors?: boolean } = {}): LogFormatter {
9+
export async function createFormatter(format: 'json' | 'text' = 'text', options: { colors?: boolean } = {}): Promise<LogFormatter> {
1010
switch (format) {
1111
case 'json':
1212
return new JsonFormatter()

‎src/formatters/text.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { LogEntry } from '../types'
2-
// src/formatters/text.ts
32
import type { LogFormatter } from './types'
43
import * as colors from '../colors'
54

‎src/formatters/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { LogEntry } from '../types'
22

33
export interface LogFormatter {
4-
format: (entry: LogEntry) => string
4+
format: (entry: LogEntry) => Promise<string> | string
55
}

‎src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ export class Logger {
172172
timestamp: new Date(),
173173
level,
174174
message,
175+
name: this.name,
175176
}
176177
}
177178

‎src/logger.ts

+16-11
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,21 @@ export interface LoggerOptions {
1313
}
1414

1515
export class Logger {
16-
private formatter: LogFormatter
17-
private isServer: boolean
16+
private formatter!: LogFormatter
17+
private isServer!: boolean
1818

1919
constructor(
2020
private readonly name: string,
2121
private options: LoggerOptions = {},
2222
) {
23-
this.formatter = createFormatter(
24-
options.format || (process.env.NODE_ENV === 'production' ? 'json' : 'text'),
25-
{ colors: options.colors ?? true },
23+
// We need to initialize these asynchronously
24+
this.initialize()
25+
}
26+
27+
private async initialize() {
28+
this.formatter = await createFormatter(
29+
this.options.format || (process.env.NODE_ENV === 'production' ? 'json' : 'text'),
30+
{ colors: this.options.colors ?? true },
2631
)
2732
this.isServer = await isServerProcess()
2833
}
@@ -113,15 +118,15 @@ export class Logger {
113118
}
114119

115120
private formatMessage(message: any, args: any[] = []): string {
116-
if (typeof message === 'string' && args.length > 0) {
121+
if (typeof message === 'string' && args.length > 0)
117122
return format(message, ...args)
118-
}
119-
if (message instanceof Error) {
123+
124+
if (message instanceof Error)
120125
return message.stack || message.message
121-
}
122-
if (typeof message === 'object') {
126+
127+
if (typeof message === 'object')
123128
return JSON.stringify(message, null, 2)
124-
}
129+
125130
return String(message)
126131
}
127132
}

‎src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface LogEntry {
1414
timestamp: Date
1515
level: LogLevel
1616
message: any
17+
name: string
1718
}
1819

1920
export type ColorFunction = (text: string) => void

‎test/server.test.ts

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
2+
import { JsonFormatter, TextFormatter } from '../src/formatters'
23
import { Logger } from '../src/index'
34
import { configManager } from '../src/storage/config-manager'
45
import { logManager } from '../src/storage/log-manager'
@@ -203,7 +204,7 @@ describe('CLI', () => {
203204
describe('Formatters', () => {
204205
const sampleEntry = {
205206
timestamp: new Date('2024-02-01T12:34:56.789Z'),
206-
level: 'info',
207+
level: 'info' as const,
207208
name: 'test',
208209
message: 'Hello world',
209210
}
@@ -219,9 +220,9 @@ describe('Formatters', () => {
219220
expect(output).toContain('INFO')
220221
})
221222

222-
test('formats log entry without colors', () => {
223+
test('formats log entry without colors', async () => {
223224
const formatter = new TextFormatter(false)
224-
const output = formatter.format(sampleEntry)
225+
const output = await formatter.format(sampleEntry)
225226

226227
expect(output).toBe('12:34:56:789 [test] INFO: Hello world')
227228
})
@@ -239,9 +240,9 @@ describe('Formatters', () => {
239240
})
240241

241242
describe('JsonFormatter', () => {
242-
test('formats log entry as JSON', () => {
243+
test('formats log entry as JSON', async () => {
243244
const formatter = new JsonFormatter()
244-
const output = formatter.format(sampleEntry)
245+
const output = await formatter.format(sampleEntry)
245246
const parsed = JSON.parse(output)
246247

247248
expect(parsed).toEqual({
@@ -253,24 +254,24 @@ describe('Formatters', () => {
253254
})
254255
})
255256

256-
test('includes metadata', () => {
257+
test('includes metadata', async () => {
257258
const formatter = new JsonFormatter()
258-
const output = formatter.format(sampleEntry)
259+
const output = await formatter.format(sampleEntry)
259260
const parsed = JSON.parse(output)
260261

261262
expect(parsed.metadata).toHaveProperty('pid')
262263
expect(parsed.metadata).toHaveProperty('hostname')
263264
expect(parsed.metadata).toHaveProperty('environment')
264265
})
265266

266-
test('handles object messages', () => {
267+
test('handles object messages', async () => {
267268
const formatter = new JsonFormatter()
268269
const entry = {
269270
...sampleEntry,
270271
message: { key: 'value' },
271272
}
272-
const output = formatter.format(entry)
273-
const parsed = JSON.parse(output)
273+
const output = await formatter.format(entry)
274+
const parsed = await JSON.parse(output)
274275

275276
expect(parsed.message).toEqual({ key: 'value' })
276277
})

‎tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
"target": "esnext",
4-
"lib": ["esnext"],
4+
"lib": ["esnext", "DOM"],
55
"moduleDetection": "force",
66
"module": "esnext",
77
"moduleResolution": "bundler",

0 commit comments

Comments
 (0)
Please sign in to comment.