Small and super simple console logger with log level, formatter and custom reporter support. Runs in node and the browser, written in TypeScript.
npm i tiny-console-logger
import createLogger, { LogLevel } from 'tiny-console-logger';
const logger = createLogger();
logger.setLevel(LogLevel.INFO);
logger.log('Hello');
LogLevel.ERROR
: allows.error
callsLogLevel.WARN
: allows.warn
calls and calls aboveLogLevel.INFO
: allows.log
and.info
calls and calls aboveLogLevel.DEBUG
: allows.debug
calls and calls aboveLogLevel.TRACE
: allows.trace
calls and calls above
A reporter does the actual logging. The default reporter uses the global console
, but it's also possible to use a custom reporter:
const arrayLog = [];
logger.setReporter((args, methodName, logLevel) => {
arrayLog.push(`[${logLevel}] ${args.map(String).join(' ')}`);
});
Because reporters are functions, they are easy to combine:
import createLogger, { defaultReporter } from 'tiny-console-logger';
const arrayLog = [];
const logger = createLogger();
logger.setReporter((args, methodName, logLevel) => {
arrayLog.push(`[${logLevel}] ${args.map(String).join(' ')}`);
defaultReporter(args, methodName, logLevel);
});
A formatter modifies arguments passed to the reporter. The default formatter just returns arguments as-is, but it's possible to use a custom formatter:
logger.setFormatter(args => ['[prefix]', ...args]);
Or:
logger.setFormatter((args, methodName, logLevel) => [
`[${logLevel}:prefix]`,
...args,
`(console.${methodName})`
]);
trace(...args: any[])
debug(...args: any[])
info(...args: any[])
log(...args: any[])
warn(...args: any[])
error(...args: any[])
Sets the log level.
logger.setLevel(LogLevel.ERROR)
Returns the current log level.
Sets custom formatter.
Sets custom reporter.