Minimal namespaced stdout / stderr logger with a timestamp for the browser and Node.js.
The logger satisfies three requirements below and attempts to do nothing else:
- Is small: has no dependencies & <50 loc (for the browser)
- Has a timestamp
- Has a namespace
Example output:
2015-08-10T20:09:20.526Z (namespace) INFO: Hello world!
import createLoggers from 'namespaced-console-logger';
const loggers = createLoggers();
const logger = loggers.get('namespace');
logger.info('Hello %s!', 'world');
createLoggers()
takes a minimum logginglevel
, which is one of:['info', 'warn', 'error']
and defaults toinfo
. For example if you selectwarn
, allinfo
logs will be ignored.warn
anderror
logs will be displayed.
Support for formatting and arbitrary metadata
import createLoggers from 'namespaced-console-logger';
const loggers = createLoggers('info', '{{timestamp}} {{hostname}} {{calculatedValue}} ({{namespace}}) {{level}}:', { hostname: 'machinename', calculatedValue: () => { return 1 + 2; } });
const logger = loggers.get('namespace');
//2015-08-10T20:09:20.526Z machinename 3 (namespace) INFO: Hello world!
logger.info('Hello %s!', 'world');