ms-iso-logger v0.1.3
ms-iso-logger
Isomorphic logger that works in client and server scope.
Install
yarn add ms-iso-logger
How it works
This package utilizes a webpack feature which looks at the browser
field in package.json
and uses that when bundling. When running on server side, node.js require
looks at the main
field, which points to another file. That way the interface looks the same but uses two different pieces of code behind the scenes. Check index-server.js
and index-client.js
for details.
Usage
To access the Logger
, simply import it from ms-iso-logger
and use it.
import { createLogger } from 'ms-iso-logger';
// create Logger and set context of logging instance (defaults to empty string)
const logger = createLogger('MyContext');
// log at different levels
logger.log('This is a message with level=INFO');
logger.debug('This is a message with level=DEBUG');
// change context of logging instance
logger.setContext('MyOtherComponent');
// log message with additional fields
logger.log({ message: 'This is a message', moreData: 'additional info' });
API
The module returns a single factory method that can be called to create a new Logger
instance for the current environment. The Logger offers a set of public methods. NOTE: Due to the isomorphic nature the code behind the scenes still uses CJS instead of ES6 modules, so it is not possible to use named imports to e.g. import the Logger directly.
createLogger(contextId:string):Logger
Factory method that creates a new Logger
instance.
Logger.setContext(contextId:string)
Set logging context for this single Logger instance using the contextId
parameter. This is added to the log output and should be used to identify the log origin. The reporter
argument my be used to set a different reporter. This defaults to
Logger.info(message:any)
Log a message with level INFO
. Message can be any primitive value or an object with additional fields which get passed as payload.
Logger.log(message:any)
Alias for Logger.info
to comply with window.console
naming.
Logger.debug(message:any)
Log a message with level DEBUG
.
Logger.warn(message:any)
Log a message with level WARN
.
Logger.error(message:any)
Log a message with level ERROR
.