@shiraya-ma/mai-logger
v3.0.2
Published
- [MaiLogger](#mailogger) - [TOC](#toc) - [Features](#features) - [Installation](#installation) - [Usage](#usage) - [API](#api) - [Constructor](#constructor) - [ConstructorOption](#constructoroption)
Downloads
25
Readme
MaiLogger
TOC
MaiLogger is a flexible and easy-to-use logging module for JavaScript applications. It provides output in the format [LOG_LEVEL] yyyy/mm/dd HH:MM:SS messages
by default.
Features
- Customizable log levels (TRACE, DEBUG, INFO, WARN, ERROR)
- Locale support for date and time formatting
- Color-coded log output for both browser and Node.js environments
Installation
Install the module via npm:
npm i @shiraya-ma/mai-logger
Usage
Import and create an instance of MaiLogger:
import { MaiLogger } from '@shiraya-ma/mai-logger';
const log = new MaiLogger({ level: 0, locale: 'ja-JP' });
// or const log = new MaiLogger({ level: 'TRACE', locale: 'ja-JP' });
log.trace('hello world!', 'this is MaiLogger');
This will output:
[TRACE] 2024/05/23 17:00:00 hello world! this is MaiLogger
API
Constructor
constructor(option?: MaiLogger.ConstructorOption)
ConstructorOption
| Option | Type | Default | Description |
| :-: | :-: | :-: | :- |
| level | number | LogLevel | "INFO"
| The most detailed log level to output. |
| locale | Intl.LocalesArgument | "ja-JP"
| Locale to which the date and time displayed in the log conform. |
Log Levels
| Log Level | Description |
| :-: | :- |
| TRACE
| Most detailed information, typically of interest only when diagnosing problems. |
| DEBUG
| Detailed information on the flow through the system. |
| INFO
| Interesting runtime events (startup/shutdown). |
| WARN
| Use of deprecated APIs, poor use of API, 'almost' errors, other runtime |situations that are undesirable or unexpected, but not necessarily wrong.
| ERROR
| Other runtime errors or unexpected conditions. |
Methods
trace(...message: any)
Outputs trace level logs.
debug(...message: any)
Outputs debug level logs.
info(...message: any)
Outputs info level logs.
warn(...message: any)
Outputs warning level logs.
error(...message: any)
Outputs error level logs.
Examples
Basic Usage
import { MaiLogger } from '@shiraya-ma/mai-logger';
const log = new MaiLogger({ level: 'DEBUG' });
// or const log = new MaiLogger({ level: 1 });
log.debug('This is a debug message');
log.info('This is an info message');
log.warn('This is a warning message');
log.error('This is an error message');
Advanced Usage
import { MaiLogger } from '@shiraya-ma/mai-logger';
const log = new MaiLogger({ level: 'DEBUG' });
// or const log = new MaiLogger({ level: 1 });
log.trace('Trace message will not be shown');
log.debug('This is a debug message');
log.info('This is an info message');
log.warn('This is a warning message');
log.error('This is an error message');
License
This project is licensed under the MIT License - see the LICENSE file for details.