A robust, event-driven, stream-based logging library for Node.js.
Streamlogs.js manages application logs by utilizing native Node.js Readable streams and events. It offers clean, customizable output, metadata support, native error handling, and simple markdown formatting.
- Stream-Based: Logs are Node.js readable streams, allowing you to easily
pipe()them anywhere (stdout, files, HTTP responses, etc.). - Event-Driven: Listen to specific log events (e.g.,
logError,logFatal,log) for external processing or notifications. - Multiple Output Formats: Supports beautifully colorized terminal logs and structured JSON logs.
- Markdown Support: Use simple markdown (
**bold**,*dim*,__underscore__) to format your CLI logs. - Global Error Handling: The default logger automatically captures unhandled exceptions and promise rejections.
- Environment Aware: Automatically sets the log level using the
LOG_LEVELenvironment variable.
npm install streamlogsThe easiest way to use Streamlogs is via the pre-configured default logger, defLog, which pipes directly to process.stdout and supports colors.
import { defLog } from "streamlogs";
// Simple logging
defLog.info("Application started successfully!", "System");
// Logging with variables (bold formatting)
defLog.warn("Connection to **redis** timed out.", "Database");
// Logging errors
try {
throw new Error("Cannot read property 'id' of null");
} catch (error) {
defLog.error(error, "AuthService");
}If you need more control, you can create your own logger instances and pipe them to a file or stream.
import { basicLogger, logLevel } from "streamlogs";
import * as fs from "fs";
// Create a logger (level, defaultSender, colors)
const log = new basicLogger(logLevel.debug, "My App", false);
// Pipe to a file
const logFile = fs.createWriteStream("app.log", { flags: "a" });
log.pipe(logFile);
log.info("This will be saved to app.log");
log.debug("A debug message", "Network", { port: 8080 }); // Includes metadata!Structured logging is ideal for log aggregators like ElasticSearch or Datadog.
import { JSONLogger, logLevel } from "streamlogs";
import * as fs from "fs";
const jsonLog = new JSONLogger(logLevel.info, "Microservice");
// Pipe to a file or standard output
jsonLog.pipe(fs.createWriteStream("structured.log", { flags: "a" }));
jsonLog.info("Service healthy", "HealthCheck", { cpu: "12%", ram: "450MB" });Streamlogs provides the following levels (in increasing severity):
tracedebuginfowarnerrorfatal
You can control the global log level by setting the LOG_LEVEL environment variable before running your app:
LOG_LEVEL=debug node app.jsYou can attach listeners directly to the loggers to perform side effects (like sending a Discord webhook on fatal errors).
import { defLog } from "streamlogs";
defLog.on("logError", (message, sender) => {
// Alerting logic here
console.log(`Error caught from ${sender}: ${message}`);
});
defLog.on("logFatal", (message, sender) => {
// Send email to admin
});(For JSONLogger, the event listener will receive a JSONMessage object instead of a string).
Constructor: new basicLogger(level?: logLevel, defaultSender?: string, colors?: boolean)
- Emits:
logTrace,logDebug,logInfo,logWarn,logError,logFatal - Includes markdown parsing:
**bright**,*dim*,__underscore__(whencolorsis true).
Constructor: new JSONLogger(level?: logLevel, defaultSender?: string)
- Emits:
logTrace,logDebug,logInfo,logWarn,logError,logFatal, andlog
MIT