Opinionated logger for production-scale applications.
yarn add @shopify/logger
A basic logger can be created simply by calling the Logger
constructor:
const logger = new Logger();
The Logger
constructor also takes an options
object. For example:
const logger = new Logger({
name: 'my-logger',
formatter: new ConsoleFormatter(),
});
The options
object adheres to the following interface:
interface LoggerOptions {
formatter?: Formatter;
name?: string;
}
The name
of a logger will be used as it's root scope. A formatter may use this to provide context when outputting a log entry. For example, the ConsoleFormatter
will preface each log with its scope, such as:
[my-logger] ℹ info - some log text
A Formatter
is simply an object that implements the Formatter
interface:
export interface Formatter {
format(entry: FormatEntry): any;
}
In addition to writing your own formatter, See Formatters below for a list of provided formatters.
Given the logger above, we can log information to the console using the built-in info
, warn
, and error
functions. For example:
logger.info('Hello, world!');
logger.warn('Something bad might happen');
logger.error(new Error('Operation not permitted.'));
This package provides the following formatters:
ConsoleFormatter
Formats and prints logs via console.log
, console.warn
, and console.error
.