Skip to content

Commit 30f7bc2

Browse files
fix(workers-tagged-logger): fix multi-arg message stringification and colo tag nesting
stringifyMessages() was called without spreading the msgs array, causing multi-argument log calls to produce a JSON-stringified array instead of space-separated individually-stringified messages. getCfFromRequest() returns a CfProps object but the result was set via shorthand { colo } creating a nested { colo: { colo: 'DFW' } } object instead of the intended { colo: 'DFW' }. Co-authored-by: Codesmith <codesmith-bot@users.noreply.github.com>
1 parent 8811ddb commit 30f7bc2

4 files changed

Lines changed: 32 additions & 6 deletions

File tree

npm-pkgs/workers-tagged-logger/src/hono.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,32 @@ describe('useWorkersLogger()', () => {
5454
`)
5555
})
5656

57+
it('sets colo tag as a flat string from request cf properties', async () => {
58+
const h = setupTest()
59+
const app = new Hono<App>()
60+
.use(async (c, next) => {
61+
c.env = { ENVIRONMENT: 'production' }
62+
await next()
63+
})
64+
.use(useWorkersLogger('worker-a'))
65+
.get(async (c) => {
66+
h.log.info('hi')
67+
return c.text('hello')
68+
})
69+
70+
const req = new Request('https://example.com')
71+
Object.defineProperty(req, 'cf', { value: { colo: 'DFW' }, writable: false })
72+
73+
const res = await app.fetch(req)
74+
expect(await res.text()).toBe('hello')
75+
expect(res.status).toBe(200)
76+
77+
const tags = h.oneLog().tags
78+
expect(tags).toBeDefined()
79+
expect(tags!.colo).toBe('DFW')
80+
expect(tags!.environment).toBe('production')
81+
})
82+
5783
it('does not add environment when not present in bindings', async () => {
5884
const h = setupTest()
5985
const app = new Hono<App>().use(useWorkersLogger('worker-a')).get(async (c) => {

npm-pkgs/workers-tagged-logger/src/hono.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ export function useWorkersLogger<T extends LogTags>(
3434
}
3535
}
3636

37-
const colo = getCfFromRequest(c.req.raw)
38-
if (colo !== null) {
39-
log.setTags({ colo })
37+
const cfProps = getCfFromRequest(c.req.raw)
38+
if (cfProps !== null) {
39+
log.setTags({ colo: cfProps.colo })
4040
}
4141

4242
if (tags !== undefined) {

npm-pkgs/workers-tagged-logger/src/logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ export class WorkersLogger<T extends LogTags> implements LogLevelFns {
322322
} else if (msgs.length === 1) {
323323
message = stringifyMessage(msgs[0])
324324
} else {
325-
message = stringifyMessages(msgs)
325+
message = stringifyMessages(...msgs)
326326
}
327327
}
328328

npm-pkgs/workers-tagged-logger/src/test/logger/logger.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ describe('WorkersLogger', () => {
136136
test('multiple values are logged to the same log', async () => {
137137
const h = setupTest()
138138
await withLogTags({ source: 'worker-a' }, async () => {
139-
h.log.info('hello', 123, new Error('boom!'), { banda: 'rocks' }, ['a', 'b'], {
139+
h.log.info('hello', 123, 'error occurred', { banda: 'rocks' }, ['a', 'b'], {
140140
foo: { bar: { baz: 'abc' } },
141141
})
142142
expect(h.oneLog().message).toMatchInlineSnapshot(
143-
`"["hello",123,{},{"banda":"rocks"},["a","b"],{"foo":{"bar":{"baz":"abc"}}}]"`
143+
`"hello 123 error occurred {"banda":"rocks"} ["a","b"] {"foo":{"bar":{"baz":"abc"}}}"`
144144
)
145145
})
146146
})

0 commit comments

Comments
 (0)