Function
Static Public Summary | ||
public |
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:
Name | Type | Attribute | Description |
name | string | function |
|
The name prefix. Prepended to all output of the created logger. If it is a function, the function should return a string. |
config | object |
|
An optional configuration object. |
config.backend | object | array |
|
The logger object that will be used. Can also be an array of logger objects. |
config.functions | array |
|
An array of function names supported by the |
config.accessor | string |
|
The name by which the logger object can be accessed on the decorated instance. |
config.prefixer | function |
|
A function that creates the prefix. Receives the |
config.level | string | number |
|
A log level name. Should be either a number or one of the available |
Return:
bool | Always returns |
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!'
}
}