@nithin-sivakumar/logger
v1.0.11
Published
A simple logger for Express.js that logs to console and file.
Readme
SimpleLogger
SimpleLogger is a logging utility that integrates morgan for HTTP request logging and winston for logging to files and the console. It allows for easy customization of log formats and levels, making it a flexible solution for Express applications.
Features
- HTTP Request Logging: Logs HTTP requests using
morganwith customizable formats. - File Logging: Logs server information in a JSON format to a file, ensuring persistence.
- Console Logging: Logs information to the console with colors for better readability.
- Customizable: You can change the log format, log level, and decide if you want customized colors for HTTP status codes.
- Easy Integration: Easily integrates into Express applications as middleware.
Installation
npm install @nithin-sivakumar/loggerUsage
Basic Usage
import express from "express";
import createLogger from "@nithin-sivakumar/logger";
const app = express();
// Initialize logger with default options
app.use(createLogger());
// Your routes here
app.get("/", (req, res) => {
res.send("Hello, world!");
});
app.listen(3000, () => {
console.log("Server is running on port 3000");
});Customizing the Logger
You can customize the logger's format, log level, and whether it should use custom colors for HTTP status codes.
import express from "express";
import createLogger from "@nithin-sivakumar/logger";
const app = express();
// Customize logger
const logger = createLogger({
format: "combined", // morgan log format
level: "debug", // winston log level
customize: true, // Use custom colors for status codes
});
app.use(logger);
// Your routes here
app.get("/", (req, res) => {
res.send("Hello, world!");
});
app.listen(3000, () => {
console.log("Server is running on port 3000");
});Logger Configuration Options
format: The log format formorgan. Available values are:"dev": Concise colored output (for development)."combined": Standard Apache combined log format."tiny": Shorter, minimal log format.- Custom format string (for
morgan).
level: The log level forwinston. Available values are:"info": Logs general information."warn": Logs warnings."error": Logs errors."debug": Logs debug information."verbose": Logs more detailed information.
customize: Whether to customize the log output with colors based on HTTP status. Defaults totrue.
API
createLogger(options)
Creates a logger middleware for use in Express applications.
Parameters
options(optional): Configuration options for the logger.format(optional): The log format formorgan. Defaults to"dev".level(optional): The log level forwinston. Defaults to"info".customize(optional): Whether to customize the log output with colors. Defaults totrue.
