A very simple logger for Apollo Server
Warning simple-apollo-logger is in alpha version.
yarn add @est-normalis/simple-apollo-logger
npm i @est-normalis/simple-apollo-logger
To use this package you need to add extension to your ApolloServer
[...]
import logger from '@est-normalis/simple-apollo-logger'
const server = ApolloServer({
extensions: [() => new logger()],
[...]
})
Now you will be able to see logs in your console.
Simple-apollo-logger is highly customizable. You can pass options to it when creating it's object.
[...]
const opts = {
logger = customLogger
logRequests = false
}
[...]
extensions: [() => new logger(opts)],
[...]
The options object will be merged with default settings and used by logger.
Types of all options are aviable for typescript users via UserOptions interface.
logger is a function which is called with a parameter containing prepared strings with data.
logRequests enables request logging.
logResponses enables respons logging.
prefix is a method executed before every log, which by default returns timestamp.
variableFilter in a settings object for filtering GraphQL variables content before logging it. It is using recursive search inside object to find even nested variables with matching name.
It is the default filter included in this extension:
variableFilter: {
keywords: ["password"],
replacementText: "[FILTERED]"
}
You can replace it with false
value if you want to disable it.
ignoreSchemaRequest allows you to ignore requests with name "IntrospectionQuery" which are usually requests fetching schema file.
Warning: you should not use this option in production since you can't be sure if query with this name is really fetching the schema.
logger is no longer an object responding to .log method, so if you are using custom logger object you need to replace it with custom logger method.
Example:
0.2.x:
const opts = {
logger: customLogger // customLogger has .log() method
}
0.3.x:
const opts = {
logger: msg => customLogger.log(msg)
}
If you were not using custom logger this update should not make any major changes.
This update should not result in major changes except for not logging headers anymore reson.
In this update TypeScript type definitions were also added (they replaced any
type in requestDidStart
function), but it should not
change way of how is the logger working.
Default prefix was changed from:
`[${Date.now()}]`
to:
`[${Date.now()}] `
Output from logger with default options should remain the same, however space between prefix and message was moved from concatenation of these strings to prefix itself.