-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add logMessage option for custom log message * feat: update documentation with logMessage option * chore: add test for uncovered usecases * feat: fix docs, refactor dynamic log message function call and update test * Apply suggestions from code review --------- Co-authored-by: Davide Arena <[email protected]> Co-authored-by: Manuel Spigolon <[email protected]>
- Loading branch information
1 parent
2f686fa
commit c169637
Showing
3 changed files
with
330 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,246 @@ | ||
'use strict' | ||
|
||
const test = require('ava') | ||
const { buildApp, jsonLogger } = require('./_helper') | ||
|
||
test('should log without msg when logMessage is undefined', async (t) => { | ||
t.plan(5) | ||
|
||
const stream = jsonLogger( | ||
line => { | ||
t.is(line.req, undefined) | ||
t.is(line.reqId, 'req-1') | ||
t.deepEqual(line.msg, undefined) | ||
t.deepEqual(line.graphql, { | ||
operationName: 'logMe', | ||
queries: ['add', 'add', 'echo', 'counter'] | ||
}) | ||
}) | ||
|
||
const app = buildApp(t, { stream }) | ||
|
||
const query = `query logMe{ | ||
four: add(x: 2, y: 2) | ||
six: add(x: 3, y: 3) | ||
echo(msg: "hello") | ||
counter | ||
}` | ||
|
||
const response = await app.inject({ | ||
method: 'POST', | ||
headers: { 'content-type': 'application/json' }, | ||
url: '/graphql', | ||
body: JSON.stringify({ query }) | ||
}) | ||
t.deepEqual(response.json(), { | ||
data: { | ||
four: 4, | ||
six: 6, | ||
echo: 'hellohello', | ||
counter: 0 | ||
} | ||
}) | ||
}) | ||
|
||
test('should log without msg when logMessage is\'nt a valid function', async (t) => { | ||
t.plan(5) | ||
|
||
const stream = jsonLogger( | ||
line => { | ||
t.is(line.req, undefined) | ||
t.is(line.reqId, 'req-1') | ||
t.deepEqual(line.msg, undefined) | ||
t.deepEqual(line.graphql, { | ||
operationName: 'logMe', | ||
queries: ['add', 'add', 'echo', 'counter'] | ||
}) | ||
}) | ||
|
||
const app = buildApp(t, { stream }, { logMessage: 1234 }) | ||
|
||
const query = `query logMe{ | ||
four: add(x: 2, y: 2) | ||
six: add(x: 3, y: 3) | ||
echo(msg: "hello") | ||
counter | ||
}` | ||
|
||
const response = await app.inject({ | ||
method: 'POST', | ||
headers: { 'content-type': 'application/json' }, | ||
url: '/graphql', | ||
body: JSON.stringify({ query }) | ||
}) | ||
t.deepEqual(response.json(), { | ||
data: { | ||
four: 4, | ||
six: 6, | ||
echo: 'hellohello', | ||
counter: 0 | ||
} | ||
}) | ||
}) | ||
|
||
test('should log without msg using a logMessage function returning an undefined value', async (t) => { | ||
t.plan(5) | ||
|
||
const customLogMessage = (context) => undefined | ||
|
||
const stream = jsonLogger( | ||
line => { | ||
t.is(line.req, undefined) | ||
t.is(line.reqId, 'req-1') | ||
t.deepEqual(line.msg, undefined) | ||
t.deepEqual(line.graphql, { | ||
operationName: 'logMe', | ||
queries: ['add', 'add', 'echo', 'counter'] | ||
}) | ||
}) | ||
|
||
const app = buildApp(t, { stream }, { logMessage: customLogMessage }) | ||
|
||
const query = `query logMe{ | ||
four: add(x: 2, y: 2) | ||
six: add(x: 3, y: 3) | ||
echo(msg: "hello") | ||
counter | ||
}` | ||
|
||
const response = await app.inject({ | ||
method: 'POST', | ||
headers: { 'content-type': 'application/json' }, | ||
url: '/graphql', | ||
body: JSON.stringify({ query }) | ||
}) | ||
t.deepEqual(response.json(), { | ||
data: { | ||
four: 4, | ||
six: 6, | ||
echo: 'hellohello', | ||
counter: 0 | ||
} | ||
}) | ||
}) | ||
|
||
test('should log without msg using a logMessage function throwing an error', async (t) => { | ||
t.plan(5) | ||
|
||
const customLogMessage = (context) => { throw new Error() } | ||
|
||
const stream = jsonLogger( | ||
line => { | ||
t.is(line.req, undefined) | ||
t.is(line.reqId, 'req-1') | ||
t.deepEqual(line.msg, undefined) | ||
t.deepEqual(line.graphql, { | ||
operationName: 'logMe', | ||
queries: ['add', 'add', 'echo', 'counter'] | ||
}) | ||
}) | ||
|
||
const app = buildApp(t, { stream }, { logMessage: customLogMessage }) | ||
|
||
const query = `query logMe{ | ||
four: add(x: 2, y: 2) | ||
six: add(x: 3, y: 3) | ||
echo(msg: "hello") | ||
counter | ||
}` | ||
|
||
const response = await app.inject({ | ||
method: 'POST', | ||
headers: { 'content-type': 'application/json' }, | ||
url: '/graphql', | ||
body: JSON.stringify({ query }) | ||
}) | ||
t.deepEqual(response.json(), { | ||
data: { | ||
four: 4, | ||
six: 6, | ||
echo: 'hellohello', | ||
counter: 0 | ||
} | ||
}) | ||
}) | ||
|
||
test('should log with msg using a logMessage function returning a string', async (t) => { | ||
t.plan(5) | ||
|
||
const customLogMessage = (context) => `This is a request made with method ${context.reply.request.method}` | ||
|
||
const stream = jsonLogger( | ||
line => { | ||
t.is(line.req, undefined) | ||
t.is(line.reqId, 'req-1') | ||
t.deepEqual(line.msg, 'This is a request made with method POST') | ||
t.deepEqual(line.graphql, { | ||
operationName: 'logMe', | ||
queries: ['add', 'add', 'echo', 'counter'] | ||
}) | ||
}) | ||
|
||
const app = buildApp(t, { stream }, { logMessage: customLogMessage }) | ||
|
||
const query = `query logMe{ | ||
four: add(x: 2, y: 2) | ||
six: add(x: 3, y: 3) | ||
echo(msg: "hello") | ||
counter | ||
}` | ||
|
||
const response = await app.inject({ | ||
method: 'POST', | ||
headers: { 'content-type': 'application/json' }, | ||
url: '/graphql', | ||
body: JSON.stringify({ query }) | ||
}) | ||
t.deepEqual(response.json(), { | ||
data: { | ||
four: 4, | ||
six: 6, | ||
echo: 'hellohello', | ||
counter: 0 | ||
} | ||
}) | ||
}) | ||
|
||
test('should log with msg using a logMessage function returning an array', async (t) => { | ||
t.plan(5) | ||
|
||
const customLogMessage = (context) => [`This is a request made with method ${context.reply.request.method} by foo%s`, 'bar'] | ||
|
||
const stream = jsonLogger( | ||
line => { | ||
t.is(line.req, undefined) | ||
t.is(line.reqId, 'req-1') | ||
t.deepEqual(line.msg, 'This is a request made with method POST by foobar') | ||
t.deepEqual(line.graphql, { | ||
operationName: 'logMe', | ||
queries: ['add', 'add', 'echo', 'counter'] | ||
}) | ||
}) | ||
|
||
const app = buildApp(t, { stream }, { logMessage: customLogMessage }) | ||
|
||
const query = `query logMe{ | ||
four: add(x: 2, y: 2) | ||
six: add(x: 3, y: 3) | ||
echo(msg: "hello") | ||
counter | ||
}` | ||
|
||
const response = await app.inject({ | ||
method: 'POST', | ||
headers: { 'content-type': 'application/json' }, | ||
url: '/graphql', | ||
body: JSON.stringify({ query }) | ||
}) | ||
t.deepEqual(response.json(), { | ||
data: { | ||
four: 4, | ||
six: 6, | ||
echo: 'hellohello', | ||
counter: 0 | ||
} | ||
}) | ||
}) |