Skip to content

Commit

Permalink
feat: add logRequest option (#24)
Browse files Browse the repository at this point in the history
* feat: add logRequest option

* chore: graphql peer dep
  • Loading branch information
Eomm authored Aug 15, 2023
1 parent e132bc9 commit b550029
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 2 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ app.register(mercuriusLogging, {
prependAlias: true, // default: false
logBody: true, // default: false
logVariables: true, // default: false
logRequest: true // default: false
})
```

Expand All @@ -146,6 +147,32 @@ app.register(mercuriusLogging, {
The log level of the plugin. Note that the `request` logger is used, so you will get the additional
[request data](https://www.fastify.io/docs/latest/Reference/Logging/#usage) such as the `reqId`.

### logRequest

Add to the log line the `req: request` object. This is useful if you want to log the request's headers or other.
You can customize what to log by using the `logSerializers` option of Fastify.

```js
const app = Fastify({
logger: {
level: 'debug',
serializers: {
req: function reqSerializer (req) {
// look at the standard serializer for the req object:
// https://github.com/pinojs/pino-std-serializers/
return {
headers: req.headers
}
}
}
}
})

app.register(mercuriusLogging, {
logRequest: true
})
```

### prependAlias

Queries and mutations may have an alias. If you want to append the alias to the log, set this option to `true`.
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ function mercuriusLogging (app, opts, next) {
logLevel: 'info',
prependAlias: false,
logBody: false,
logVariables: false
logVariables: false,
logRequest: false
}, opts)

options.buildBody = opts.logBody === true
Expand Down Expand Up @@ -57,6 +58,7 @@ function logGraphQLDetails (opts, schema, document, context) {
: context.reply.request.body

context.reply.request.log[opts.logLevel]({
req: opts.logRequest === true ? context.reply.request : undefined,
graphql: {
queries: queryOps.length > 0 ? queryOps : undefined,
mutations: mutationOps.length > 0 ? mutationOps : undefined,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"ava": "^5.3.1",
"c8": "^8.0.0",
"fastify": "^4.4.0",
"graphql": "^16.8.0",
"mercurius": "^13.0.0",
"split2": "^4.1.0",
"standard": "^17.0.0",
Expand Down
95 changes: 94 additions & 1 deletion test/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ function buildApp (t, logger, opts) {
}

test('should log every query', async (t) => {
t.plan(3)
t.plan(4)

const stream = split(JSON.parse)
stream.on('data', line => {
t.is(line.req, undefined)
t.is(line.reqId, 'req-1')
t.deepEqual(line.graphql, {
queries: ['add', 'add', 'echo', 'counter']
Expand Down Expand Up @@ -624,3 +625,95 @@ test('should log the whole request when operationName is set', async (t) => {

t.deepEqual(response.json(), { data: { c: 5, d: 5 } })
})

test('should log the request object when logRequest is true', async (t) => {
t.plan(2)

const query = `
query boom($num: Int!) {
a: add(x: $num, y: $num)
b: add(x: $num, y: $num)
}
query baam($num: Int!, $bin: Int!) {
c: add(x: $num, y: $bin)
d: add(x: $num, y: $bin)
}
`

const stream = split(JSON.parse)
stream.on('data', line => {
t.is(line.reqId, 'req-1')
t.deepEqual(line.req, {
method: 'POST',
url: '/graphql',
hostname: 'localhost:80',
remoteAddress: '127.0.0.1'
})
})

const app = buildApp(t, { stream }, {
logRequest: true,
logVariables: true
})

await app.inject({
method: 'POST',
headers: { 'content-type': 'application/json' },
url: '/graphql',
body: JSON.stringify({
query,
operationName: 'baam',
variables: { num: 2, bin: 3 }
})
})
})

test('user can customize the log the request object when logRequest is true', async (t) => {
t.plan(3)

const query = `
query boom($num: Int!) {
a: add(x: $num, y: $num)
b: add(x: $num, y: $num)
}
`

const stream = split(JSON.parse)
stream.on('data', line => {
t.is(line.reqId, 'req-1')
t.deepEqual(line.req, {
headers: {
'content-length': '131',
'content-type': 'application/json',
foo: 'bar',
host: 'localhost:80',
'user-agent': 'lightMyRequest'
}
})
})

const app = buildApp(t, {
stream,
serializers: {
req: function reqSerializer (req) {
t.pass('reqSerializer called')
return {
headers: req.headers
}
}
}
}, {
logRequest: true,
logVariables: true
})

await app.inject({
method: 'POST',
headers: { 'content-type': 'application/json', foo: 'bar' },
url: '/graphql',
body: JSON.stringify({
query,
variables: { num: 2 }
})
})
})
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ declare namespace mercuriusLogging {
prependAlias?: boolean
logBody?: boolean | ((context: MercuriusContext) => boolean)
logVariables?: boolean
logRequest?: boolean
}

export const mercuriusLogging: MercuriusLogging
Expand Down
6 changes: 6 additions & 0 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ app.register(plugin, {
logBody: (context: MercuriusContext) => true,
logVariables: false
})

// Using options with different types
app.register(plugin, {
logLevel: 'warn',
logRequest: true
})

0 comments on commit b550029

Please sign in to comment.