Skip to content

Latest commit

 

History

History
229 lines (152 loc) · 4.84 KB

README.md

File metadata and controls

229 lines (152 loc) · 4.84 KB

@est-normalis/simple-apollo-logger

npm version npm GitHub Workflow Status License: MIT

A very simple logger for Apollo Server

Warning simple-apollo-logger is in alpha version.

Installation

yarn

yarn add @est-normalis/simple-apollo-logger

npm

npm i @est-normalis/simple-apollo-logger

Usage

To use this package you need to add plugin or extension (not recommended) to your ApolloServer

Plugin

[...]
import { apolloLogPlugin } from '@est-normalis/simple-apollo-logger'

const server = ApolloServer({
    plugins: [apolloLogPlugin()],
})

Extension (deprecated)

[...]
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.

Configuration

Simple-apollo-logger is highly customizable. You can pass options to it when creating it's object.

Plugin configuration

[...]

const opts = {
    logger = customLogger
    logRequests = false
}

[...]
    plugins: [apolloLogPlugin(opts)],
[...]

Extension configuration

[...]

const opts = {
    logger = customLogger
    logRequests = false
}

[...]
    extensions: [() => new logger(opts)],
[...]

The options object will be merged with default settings and used by logger.

Available options

Types of all options are available for typescript users via UserOptions interface.

logger

logger is a function which is called with a parameter containing prepared strings with data.

logRequests

logRequests enables request logging.

logResponses

logResponses enables respons logging.

prefix

prefix is a method executed before every log, which by default returns timestamp.

variableFilter

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.

Filter usage

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

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.

Updating

0.4.x to 0.5.x

Version 0.5 introduces usage of new plugin API. Using plugin instead of extension is highly recommended, but not obligatory.

To use new plugin API change delete your logging extension from server initialization:

const server = ApolloServer({
    extensions: [() => new logger()], // remove this line
    [...]
})

Change default import to apolloLogPlugin named import:

before:

import logger from '@est-normalis/simple-apollo-logger'

after

import { apolloLogPlugin } from '@est-normalis/simple-apollo-logger'

and use it in server initialization

const server = ApolloServer({
    plugins: [apolloLogPlugin()],
    [...]
})

Possible configuration options are not changed from version 0.4.

0.3.x to 0.4.x

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.

Prefix

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.

0.2.x to 0.3.x

Logger

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.