Home Reference Source

Function

Static Public Summary
public

logger(name: string | function, config: object): bool

A class decorator that adds a logger object to class instances.

Static Public

public logger(name: string | function, config: object): bool source

import logger from '@xailabs/logger/src/logger.js'

A class decorator that adds a logger object to class instances. The logger object prepends a specified name to all log output.

Unless you specify a custom accessor, the logger object is available via this.logger and you can use it as in this.logger.warn('oh noes!'). A special case is accessor: 'this'- it leads to the log methods being available onthe decorated instance directly and you can use it as in this.warn('oh noes!').

Params:

NameTypeAttributeDescription
name string | function
  • optional
  • default: 'logger'

The name prefix. Prepended to all output of the created logger. If it is a function, the function should return a string.

config object
  • optional

An optional configuration object.

config.backend object | array
  • optional
  • default: window.console

The logger object that will be used. Can also be an array of logger objects.

config.functions array
  • optional
  • default: ['log', 'info', 'warn', 'error', 'trace', 'table', 'debug']

An array of function names supported by the logger object..

config.accessor string
  • optional
  • default: 'logger'

The name by which the logger object can be accessed on the decorated instance.

config.prefixer function
  • optional

A function that creates the prefix. Receives the name string and should return a string.

config.level string | number
  • optional
  • default: debug

A log level name. Should be either a number or one of the available functions (the array index is the numeric level), decides whether the message will be printed. E.g. the lowest level is log - only logger.log() will be printed, while the highest level is debug (last array element)

Return:

bool

Always returns true. (Allows usage as in (args) => this.logger.log(args) && doSomething(args))

Example:

import React from 'react';
import logger from '@xailabs/logger';
@logger('App')
class App extends React.Component {
     componentDidMount() {
         this.logger.warn('watch out!'); // logs '[App] watch out!'
     }
}