dtducas-smart-log
v1.0.0
Published
A smart logging package with environment-aware log levels for Node.js applications
Maintainers
Readme
dtducas-smart-log
A smart logging package with environment-aware log levels for Node.js applications.
Features
- Environment-aware logging (Development vs Production)
- Multiple log levels:
info,warn,error,debug - Automatic timestamp formatting
- TypeScript support with full type definitions
- Zero dependencies
- Easy configuration
- Singleton pattern with factory function support
Installation
npm install dtducas-smart-logQuick Start
import smartLog from "dtducas-smart-log";
// Basic usage - replaces console.log
smartLog.info("Application started");
smartLog.warn("This is a warning");
smartLog.error("An error occurred");
smartLog.debug("Debug information");Environment Behavior
Development Environment (NODE_ENV=development)
All log levels are enabled:
smartLog.info()- LoggedsmartLog.warn()- LoggedsmartLog.error()- LoggedsmartLog.debug()- Logged
Production Environment (NODE_ENV=production)
Only error logs are enabled:
smartLog.info()- Not loggedsmartLog.warn()- Not loggedsmartLog.error()- LoggedsmartLog.debug()- Not logged
Log Format
All logs are formatted with timestamp and log level:
[2025-11-02T10:30:45.123Z] [INFO] Your log message here
[2025-11-02T10:30:45.456Z] [ERROR] Error message hereAPI Documentation
Default Import (Singleton)
import smartLog from "dtducas-smart-log";
smartLog.info("message", optionalArgs);
smartLog.warn("message", optionalArgs);
smartLog.error("message", optionalArgs);
smartLog.debug("message", optionalArgs);Methods
info(message: string, ...args: any[]): void
Log informational messages. Only works in development environment.
smartLog.info("User logged in", { userId: 123 });warn(message: string, ...args: any[]): void
Log warning messages. Only works in development environment.
smartLog.warn("API rate limit approaching", { remaining: 10 });error(message: string, ...args: any[]): void
Log error messages. Works in both development and production environments.
smartLog.error("Database connection failed", error);debug(message: string, ...args: any[]): void
Log debug messages. Only works in development environment.
smartLog.debug("Processing request", { data: payload });Configuration
configure(config: SmartLogConfig): void
Update logger configuration at runtime.
smartLog.configure({
environment: "production",
disableAll: false,
});Configuration Options
interface SmartLogConfig {
environment?: "development" | "production" | string;
disableAll?: boolean;
timestampFormat?: "iso" | "locale" | "custom";
customTimestampFormatter?: () => string;
}Advanced Usage
Creating Custom Logger Instances
import { createLogger } from "dtducas-smart-log";
const customLogger = createLogger({
environment: "production",
disableAll: false,
});
customLogger.error("This will be logged");
customLogger.info("This will NOT be logged in production");Disabling All Logs
import smartLog from "dtducas-smart-log";
// Disable all logs including errors
smartLog.configure({ disableAll: true });
smartLog.error("This will not be logged");
smartLog.info("This will not be logged");Custom Timestamp Format
import smartLog from "dtducas-smart-log";
// Use locale format
smartLog.configure({ timestampFormat: "locale" });
// Use custom formatter
smartLog.configure({
timestampFormat: "custom",
customTimestampFormatter: () => {
return new Date().toISOString().split("T")[0]; // YYYY-MM-DD
},
});TypeScript Support
Full TypeScript support with exported types:
import smartLog, {
LogLevel,
Environment,
SmartLogConfig,
SmartLogger,
} from "dtducas-smart-log";
const config: SmartLogConfig = {
environment: Environment.PRODUCTION,
disableAll: false,
};
smartLog.configure(config);Examples
Express.js Application
import express from "express";
import smartLog from "dtducas-smart-log";
const app = express();
app.get("/", (req, res) => {
smartLog.info("Homepage accessed");
res.send("Hello World");
});
app.use((err, req, res, next) => {
smartLog.error("Unhandled error", err);
res.status(500).send("Internal Server Error");
});
app.listen(3000, () => {
smartLog.info("Server started on port 3000");
});Migration from console.log
Before:
console.log("User logged in:", userId);
console.error("Error:", error);
console.warn("Warning:", message);After:
import smartLog from "dtducas-smart-log";
smartLog.info("User logged in:", userId);
smartLog.error("Error:", error);
smartLog.warn("Warning:", message);Environment Variables
The package automatically detects the environment using process.env.NODE_ENV:
# Development (all logs enabled)
NODE_ENV=development node app.js
# Production (only errors enabled)
NODE_ENV=production node app.jsLicense
MIT
Author
Duong Tran Quang (DTDucas) Email: [email protected]
Repository
https://github.com/dtducas/dtducas-smart-log
Issues
Report issues at: https://github.com/dtducas/dtducas-smart-log/issues
