biz-log-file
TypeScript icon, indicating that this package has built-in type declarations

1.0.7 • Public • Published

Biz Log File

biz-log-file is a lightweight, flexible logging utility for Node.js projects, built with TypeScript. It allows you to log messages in JSON format to a specified file, automatically creating directories if they don't exist.

Installation

You can install this package via npm:

npm install biz-log-file

Features

  • Log messages of different levels (info, error, warn) in JSON format.
  • Automatically creates log files and directories if they do not exist.
  • Customizable log file names and locations.
  • Logs can handle any type of message (string, object, array, etc.).

Usage

Here is a quick guide on how to use biz-log-file in your project.

1. Import the Logger

First, import the logger into your Node.js project:

import Logger from 'biz-log-file';

2. Initialize the Logger

Create a new instance of the logger by specifying a file path for the log:

const logger = new Logger('logs/app-log.json');

If the directory (logs/) doesn't exist, it will be created automatically.

3. Log Messages

You can log different types of messages using the info(), warn(), and error() methods.

// Log a simple info message
logger.info('This is an informational message');

// Log an error message
logger.error('This is an error message');

// Log an object
logger.info({ event: 'user_signup', user: 'johndoe' });

// Log an array
logger.warn(['Warning!', 'Low disk space', 'Take action']);

4. Customize Log Levels

You can pass messages of any type (strings, numbers, objects, arrays) to the logger with different log levels:

logger.log('This is a custom log message', 'info'); // Info level
logger.log({ status: 'critical', message: 'Database error' }, 'error'); // Error level
logger.log(['Warning message', 'Potential issue detected'], 'warn'); // Warning level

Log Output Format

The logs will be saved in JSON format, each entry containing the following:

  • timestamp: The date and time when the log was created.
  • level: The log level (info, error, or warn).
  • message: The content of the log message, which can be of any type.

Example log output (app-log.json):

{
  "timestamp": "2024-09-19T10:00:00.000Z",
  "level": "info",
  "message": "This is an informational message"
}
{
  "timestamp": "2024-09-19T10:00:01.000Z",
  "level": "error",
  "message": "This is an error message"
}
{
  "timestamp": "2024-09-19T10:00:02.000Z",
  "level": "warn",
  "message": ["Warning!", "Low disk space", "Take action"]
}

API Reference

new Logger(logFileName: string)

  • logFileName: (optional) The name and location of the log file. If not provided, it defaults to log.json in the current working directory.

log(message: any, level: 'info' | 'error' | 'warn')

Logs a message with the specified log level.

  • message: Can be any type (string, object, array, number, etc.).
  • level: (optional) The log level, defaults to 'info'.

info(message: any)

Logs a message at the info level.

error(message: any)

Logs a message at the error level.

warn(message: any)

Logs a message at the warn level.

License

This project is licensed under the ISC License.

Readme

Keywords

Package Sidebar

Install

npm i biz-log-file

Weekly Downloads

2

Version

1.0.7

License

ISC

Unpacked Size

8.22 kB

Total Files

6

Last publish

Collaborators

  • tangie