Traillog is a simple, colorful logging utility for Node.js applications. It provides an easy way to add formatted, color-coded logs with timestamps and caller information for better debugging and log readability.
- Color-coded log messages using chalk
- Timestamp for each log entry
- Caller's file and line number for easy tracing
- Support for different log levels: SUCCESS, ERROR, WARN, and INFO
- Optional metadata logging in JSON format
To install Traillog, run the following command in your project directory:
npm install traillog
First, import the logger from traillog in your JavaScript or TypeScript file:
import logger from "traillog";
Then, you can use the different logging methods:
// Log a success message
logger.success("Operation completed successfully");
// Log an error message
logger.error("An error occurred", { errorCode: 500 });
// Log a warning message
logger.warn("This is a warning");
// Log an informational message
logger.info("This is an informational message", { user: "John Doe" });
Each logging method accepts two parameters:
-
message
(required): The main log message as a string. -
meta
(optional): An object containing additional metadata to be logged.
The log output will be formatted as follows:
[TIMESTAMP] [LOG_LEVEL] (FILE:LINE): MESSAGE
METADATA (if provided)
For example:
[2023-05-20T12:34:56.789Z] [SUCCESS] (src/app.ts:42): Operation completed successfully
[2023-05-20T12:34:57.123Z] [ERROR] (src/app.ts:45): An error occurred
{
"errorCode": 500
}
The logger
object exposes four main methods for logging:
Logs a success message in green.
-
message
(string): The success message to log. -
meta
(object, optional): Additional metadata to log.
Example:
logger.success("Data saved successfully", { id: 123 });
Logs an error message in red.
-
message
(string): The error message to log. -
meta
(object, optional): Additional metadata to log, such as error details.
Example:
logger.error("Failed to connect to database", { errorCode: "DB_001" });
Logs a warning message in yellow.
-
message
(string): The warning message to log. -
meta
(object, optional): Additional metadata to log.
Example:
logger.warn("Deprecated function called", { function: "oldMethod" });
Logs an informational message in blue.
-
message
(string): The informational message to log. -
meta
(object, optional): Additional metadata to log.
Example:
logger.info("User logged in", { userId: "user123", timestamp: Date.now() });
Contributions are welcome! Please feel free to submit a Pull Request.