@rmtc/logger
v1.0.0
Published
Consistent logging for the @rmtc/toolchain CLI
Downloads
116
Readme
@rmtc/logger
Consistent logging for the @rmtc/toolchain CLI.
[!WARNING] This package is intended for internal use by @rmtc/toolchain. It's probably not useful by itself.
[!WARNING] This project is intended for use in @rowanmanning's projects. It's free to use but I don't offer support for use-cases outside of what I need.
Table of Contents
Requirements
This library requires the following to run:
- Node.js 18+
Usage
Install the module with npm:
npm install @rmtc/logger
This module exports a Logger class which is used to provide consistent logging for the command line interfaces:
const {Logger} = require('@rmtc/logger');
const logger = new Logger();
logger.info('Hello World!');
Logging
The logger has different log methods depending on the level of log you want to output:
logger.debug('This is a debug-level message');
logger.info('This is an info-level message');
logger.warn('This is a warning message');
logger.error('This is an error message');
The error
method can also accept an Error
object, which it will render neatly:
logger.error(new Error('something went wrong));
Configuration
When you create a logger, you can configure it with the following options by passing them as part of an options object:
const logger = new Logger({
// options go here
});
The available options are:
logLevel
String
. The lowest log level to output logs for. Any less important log than this level will not be output. Defaults to process.env.LOG_LEVEL
, and if that's not set then info
. E.g.
const logger = new Logger({ logLevel: 'warn' });
logger.info('shh'); // Logs nothing
prefix
String
. A string to prepend to all log messages from the logger. A space will be added to separate the prefix from the message. Defaults to an empty string. E.g.
const logger = new Logger({ prefix: 'Hello' });
logger.info('World!'); // Logs "Hello World!"
Child loggers
If you want to chain prefixes together, you can create child loggers. These inherit the settings from their parent but you can add a further prefix. E.g.
const logger = new Logger({ prefix: 'one' });
logger.info('testing'); // Logs "one testing"
const childLogger = logger.child({ prefix: 'two' });
logger.info('testing'); // Logs "one two testing"
Contributing
See the central README for a contribution guide and code of conduct.
License
Licensed under the MIT license. Copyright © 2023, Rowan Manning