README
Logger
A tiny TypeScript-powered logging solution with support for color (TTY) and pretty object printing.
Installation
This is a Node.js module available through the npm registry.
Before installing, download and install Node.js. Node.js 6.0 or higher is required.
Installation is done using the
npm install
command:
$ npm install @robinlemon/logger
Usage
Quickstart
import Logger, { Levels } from '@robinlemon/logger';
const logger = new Logger({
Name: 'Main',
/* Unnecessary */
Suppressed: false,
/* Unnecessary */
DefaultLevel: LogLevel.INFO,
/* Unnecessary */
InspectOptions: {
showHidden: false,
showProxy: false,
depth: 2,
colors: true,
compact: false,
},
});
/**
* Development Logging
*/
logger.Log((process.env.NODE_ENV = 'development')) // -> INFO: development
logger.Log('foo'); // -> INFO: foo
logger.Log(LogLevel.SILLY, 'foo'); // -> SILLY: foo
/**
* Production Logging
*/
logger.Log((process.env.NODE_ENV = 'production')) // -> INFO: production
logger.Log('foo'); // -> INFO: foo
logger.Log(LogLevel.SILLY, 'foo'); // -> Not printed
/**
* Suppressing
*/
logger.Log('foo') // -> INFO: foo
logger.Hide();
logger.Log('foo'); // -> Not printed
logger.Show();
logger.Log('foo'); // -> INFO: foo
/**
* Colours
*/
logger.Log('%red%foo%'); // -> INFO: foo (in red)
Levels
Available Logging Levels:
export const LogLevel = {
'ERROR': Symbol('error'),
'WARN': Symbol('warn'),
'INFO': Symbol('info'),
'VERBOSE': Symbol('verbose'),
'DEBUG': Symbol('debug'),
'SILLY': Symbol('silly'),
};
Constructor Options
All constructor options are optional.
Option | Type | Default | Description |
---|---|---|---|
Name |
string or undefined |
undefined |
A label that is printed when Log is called. If set to undefined no label will be printed. |
MaxLevel |
LogLevel |
'INFO' or 'SILLY' |
The maximum level that should be printed, setting this to INFO and logging under SILLY will not print the silly leveled message.If NODE_ENV is not set to production this will automatically be set to SILLY , otherwise it will be INFO - unless explicitly overriden by constructor option. |
DefaultLevel |
LogLevel |
'INFO' |
The LogLevel to use when one is not provided to Log . |
Suppressed |
boolean |
false |
Whether the logger should respond to Log calls. |
InspectOptions |
Identical to util.inspect | See below. | The options to pass to NodeJS's util.inspect for objects . |
export const DefaultOptions: Required<ILoggerOptions> = {
Name: '',
DefaultLevel: LogLevel.INFO,
MaxLevel: LogLevel.INFO,
Suppressed: false,
InspectOptions: {
showHidden: false,
showProxy: false,
depth: 2,
colors: true,
compact: false,
},
};
Colouring
Any string of the following format will be interpolated to remove the percentage symbols.
This module supports all colour namespaces from Chalk.
`%${ColorName}%${Message}%`
Instance Properties
Note that all of the properties on this class are readonly
except Name
.
Name
Type: string
Description: The Logger instance's name.
new Logger({ Name: 'foo' }).Name // -> foo
DefaultLevel
Type: string
Description: The default level that is used to log when one is not provided as a lowercase string.
new Logger({ DefaultLevel: LogLevel.SILLY }).DefaultLevel // -> silly
MaxLevel
Type: string
Description: The maximum level the logger will print as a lowercase string.
new Logger({ MaxLevel: LogLevel.WARN }).MaxLevel // -> warn
isSuppressed
Type: boolean
Description: A boolean determining if the Logger instance is suppressed.
new Logger({ Suppressed: true }).isSuppressed // -> true
Instance Methods
All instance methods return the class reference (this
).
Hide
Suppresses log output.
new Logger().Hide();
Show
Removes the log suppression.
new Logger().Show();
Space
Prints an empty line.
new Logger().Space();
Log
Writes to std::out
given a logging level
and list of items to print.
/**
* @param Level The logging level to use.
* @param Items Infinite arguments here of things to log.
* @returns The current Logger instance.
*/
Log(Level?: LogLevel, ...Items: unknown[]): this;
new Logger().Log('foo');
new Logger().Log(LogLevel.SILLY, 'foo');
new Logger().Log('foo', 'bar', 'baz');
new Logger().Log(LogLevel.SILLY, 'foo', 'bar', 'baz');
Tests
To run the test suite, first install the dependencies, then run npm test
:
$ npm install
$ npm test