import { log } from 'nexus'

Guideissues (features | bugs)

The Logger component is designed for outputting structured logs from your application. If you are new to this component then you may be interested in our logging guide.

fatal

A log that outputs at fatal (6) level.

error

A log that outputs at error (5) level.

warn

A log that outputs at warn (4) level.

info

A log that outputs at info (3) level.

debug

A log that outputs at debug (2) level.

trace

A log that outputs at trace (1) level.

addToContext

Add context to the logger. All subsequent logs will have this information included under their context property. Data is merged deeply using lodash merge.

Use this if you have some information that you wish all logs to include in their context.

Signature

1(context: Record<string, unknown>) => Logger

Example

1log.addToContext({ user: 'Toto' })
2log.info('hello')
3log.warn('bye')
4// { "context": { "user": "Toto" }, "event": "hello", ... }
5// { "context": { "user": "Toto" }, "event": "bye", ... }

Example of local scalar precedence

1log.addToContext({ user: { name: 'Toto', age: 10 })
2log.info("hi", { user: { name: 'Titi', heightCM: 155 })
3// { "context": { "user": { "name": "Titi", age: 10, heightCM: 155 }}, ... }

child

Create a new logger that inherits its parents' context and path.

Context added by children is never visible to parents.

Context added by children is deeply merged using lodash merge with the context inherited from parents.

Context added to parents is immediately visible to all existing children.

Signature

1(name: string) => Logger

Example

1log.addToContext({ user: 'Toto' })
2
3const bar = log.child('bar').addToContext({ bar: 'bar' })
4const foo = log.child('foo').addToContext({ foo: 'foo' })
5
6log.info('hello')
7bar.info('bar')
8foo.info('foo')
9
10// { "context": { "user": "Toto" }, path: ["app"], "event": "hello", ... }
11// { "context": { "user": "Toto", "bar": "bar" }, path: ["app", "bar"], "event": "bar", ... }
12// { "context": { "user": "Toto", "foo": "foo" }, path: ["app", "foo"], "event": "foo", ... }

Type Glossary

F Log

Signature
1(event: string, context?: Record<string, unknown>) => void

Parameters

event

The event name of this log. Maps to the event property in the output JSON.

Remarks

The log api is designed to get you thinking about logs in terms of events with structured data instead of strings of interpolated data. This approach gets you significantly more leverage out of your logs once they hit your logging platform, e.g. ELK

context

Contextual information about this log. The object passed here is deeply merged under the log's context property.

Example

1log.info('hello', { user: 'Toto' })
2// { "context": { "user": "Toto" }, ... }

Example

1import { log } from 'nexus'
2
3log.info('hello')

I JSONLog

Type
1{
2 level: 10 | 20 | 30 | 40 | 50
3 time: number
4 pid: number
5 hostname: string
6 path: string[]
7 context: JSON
8 event: string
9}
  • level
    Numeric representation of log levels. Numeric because it easy later to filter e.g. level 30 and up. 10 is trace while on the other end of the scale 50 is fatal.

  • time
    Unix timestamp in milliseconds.

  • pid
    The process ID given by the host kernel.

  • hostname
    The machine host (require('os').hostname())

  • path
    The fully qualified name of the logger.

    Example
    1import { log } from 'nexus'
    2
    3log.info('hi') // { path: ['nexus'], ... }
    4log.child('b').info('hallo') // { path: ['nexus', 'b'], ... }
  • context
    Custom contextual data added by you. Any data added by the log call, previous addToContext calls, or inheritance from parent context.

  • event
    The name of this log event.

Example
1{
2 "path": ["nexus", "dev", "watcher"],
3 "event": "restarting",
4 "level": 30,
5 "time": 1582856917643,
6 "pid": 49426,
7 "hostname": "Jasons-Prisma-Machine.local",
8 "context": {
9 "changed": "api/graphql/user.ts"
10 }
11}
Edit this page on Github