Skip to content

Commit 0f8e49f

Browse files
committed
legacylogger tests and fixes
1 parent f9eb035 commit 0f8e49f

File tree

2 files changed

+100
-4
lines changed

2 files changed

+100
-4
lines changed

packages/logger/src/LegacyLogger.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ export class LegacyLogger {
1818

1919
#log(level: LogLevel, ...[maybeMsgOrArg, ...restArgs]: any[]) {
2020
if (typeof maybeMsgOrArg === 'string') {
21-
this.#logger.log(level, restArgs, maybeMsgOrArg);
21+
if (restArgs.length) {
22+
this.#logger.log(level, restArgs, maybeMsgOrArg);
23+
} else {
24+
this.#logger.log(level, maybeMsgOrArg);
25+
}
2226
} else {
2327
if (restArgs.length) {
2428
this.#logger.log(level, [maybeMsgOrArg, ...restArgs]);
@@ -53,9 +57,11 @@ export class LegacyLogger {
5357
}
5458

5559
child(name: string | Record<string, string | number>): LegacyLogger {
56-
name = stringifyName(name);
57-
if (this.#logger.prefix?.includes(name)) {
58-
// TODO: why do we do this?
60+
name =
61+
stringifyName(name) +
62+
// append space if object is strigified to space out the prefix
63+
(typeof name === 'object' ? ' ' : '');
64+
if (this.#logger.prefix === name) {
5965
return this;
6066
}
6167
return LegacyLogger.from(this.#logger.child(name));
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { LegacyLogger } from '@graphql-hive/logger';
2+
import { expect, it } from 'vitest';
3+
import { Logger, LoggerOptions } from '../src/Logger';
4+
import { MemoryLogWriter } from '../src/writers';
5+
6+
function createTLogger(opts?: Partial<LoggerOptions>) {
7+
const writer = new MemoryLogWriter();
8+
return [
9+
LegacyLogger.from(
10+
new Logger({ ...opts, writers: opts?.writers ? opts.writers : [writer] }),
11+
),
12+
writer,
13+
] as const;
14+
}
15+
16+
it('should correctly write legacy logger logs', () => {
17+
const [log, writer] = createTLogger();
18+
19+
log.info('hello world');
20+
log.info({ hello: 'world' });
21+
log.info('hello', { wor: 'ld' });
22+
log.info('hello', [{ wor: 'ld' }]);
23+
log.info('hello', { w: 'o' }, { rl: 'd' });
24+
log.info('hello', 'world');
25+
26+
log.child('child ').info('hello child');
27+
log.child({ chi: 'ld' }).info('hello chi ld');
28+
29+
expect(writer.logs).toMatchInlineSnapshot(`
30+
[
31+
{
32+
"level": "info",
33+
"msg": "hello world",
34+
},
35+
{
36+
"attrs": {
37+
"hello": "world",
38+
},
39+
"level": "info",
40+
},
41+
{
42+
"attrs": [
43+
{
44+
"wor": "ld",
45+
},
46+
],
47+
"level": "info",
48+
"msg": "hello",
49+
},
50+
{
51+
"attrs": [
52+
[
53+
{
54+
"wor": "ld",
55+
},
56+
],
57+
],
58+
"level": "info",
59+
"msg": "hello",
60+
},
61+
{
62+
"attrs": [
63+
{
64+
"w": "o",
65+
},
66+
{
67+
"rl": "d",
68+
},
69+
],
70+
"level": "info",
71+
"msg": "hello",
72+
},
73+
{
74+
"attrs": [
75+
"world",
76+
],
77+
"level": "info",
78+
"msg": "hello",
79+
},
80+
{
81+
"level": "info",
82+
"msg": "child hello child",
83+
},
84+
{
85+
"level": "info",
86+
"msg": "chi=ld hello chi ld",
87+
},
88+
]
89+
`);
90+
});

0 commit comments

Comments
 (0)