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.
You can install this package via npm:
npm install biz-log-file
- 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.).
Here is a quick guide on how to use biz-log-file
in your project.
First, import the logger into your Node.js project:
import Logger from 'biz-log-file';
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.
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']);
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
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
, orwarn
). - 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"]
}
-
logFileName
: (optional) The name and location of the log file. If not provided, it defaults tolog.json
in the current working directory.
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'
.
Logs a message at the info
level.
Logs a message at the error
level.
Logs a message at the warn
level.
This project is licensed under the ISC License.