Hamsterlog works with Node.js, perfect for web servers, APIs and CLI tools.
Download the Hamsterlog file for Node.js (yes, it's just one tiny file!):
Javascript version: Download hamster-node.jsPlace the file in your project directory and import it:
import { Hamster } from "./hamster-node.js";
import { Hamster } from "./hamster-node.js";
// Initialize Hamsterlog
const hamster = new Hamster({
apiKey: "YOUR_API_KEY",
defaultTags: ["nodejs", "my-node-app"],
});
// Start logging
console.log("This log shows up in Hamsterlog!");
console.warn("This warning too.");
console.error("You guessed it.");
hamster.log("Add some tags like this.", ["demo", "important"]);
hamster.warn("Automatically receives a 'warning' tag.");
hamster.error("Automatically receives an 'error' tag.", ["critical"]);
// All these logs also show up in the local console.
Keep using console.log/warn/error/info/debug like you always have.
Hamsterlog just quietly captures everything in the background.
Unless you want to add tags, there's no need to change your code or
replace console.log()
with hamster.log()
.
The Hamster constructor accepts the following configuration options:
const hamster = new Hamster({
// Required: The API key of the 'source' you want to link the logs to.
// Get it in your Dashboard > Settings > Sources
apiKey: "your-api-key-here",
// Optional: Tags added to every log (default: [])
defaultTags: ["nodejs", "production", "api"],
// Optional: Replaces the native console.log/warn/error/info/debug methods
// to automatically send logs to Hamsterlog (default: true)
captureConsole: true,
// Optional: Also show logs in local console (default: true)
copyToLocalConsole: true,
});
Send a general log message.
hamster.log("User logged in successfully", ["auth", "success"]);
hamster.log("Processing payment for order #1234");
hamster.log("Cache miss for key: user_preferences_123", ["cache", "debug"]);
Send a warning message. Automatically includes the "warning" tag.
hamster.warn("API rate limit approaching", ["api", "rate-limit"]);
hamster.warn("Deprecated function called", ["deprecated"]);
hamster.warn("Memory usage above 80%", ["performance", "memory"]);
Send an error message. Automatically includes the "error" tag.
hamster.error("Database connection failed", ["database", "critical"]);
hamster.error("Invalid user input: " + userInput, ["validation"]);
hamster.error("Payment processing failed for order #1234", ["payment", "failure"]);
Here's how to integrate Hamsterlog with an Express.js application:
import express from "express";
import { Hamster } from "./hamster-node.js";
// Initialize Hamsterlog
const hamster = new Hamster({
apiKey: process.env.HAMSTER_API_KEY,
defaultTags: ["express", "api"],
});
const app = express();
// Middleware to log all requests
app.use((req, res, next) => {
hamster.log(`${req.method} ${req.path}`, ["request", req.method.toLowerCase()]);
next();
});
// Error handling middleware
app.use((err, req, res, next) => {
hamster.error(`Error in ${req.method} ${req.path}: ${err.message}`, ["middleware"]);
res.status(500).json({ error: "Internal Server Error" });
});
app.get("/api/users", (req, res) => {
try {
// Your API logic here
hamster.log("Users API called", ["api", "users"]);
res.json({ users: [] });
} catch (error) {
hamster.error(`Users API error: ${error.message}`, ["api", "users"]);
res.status(500).json({ error: "Failed to fetch users" });
}
});
app.listen(3000, () => {
hamster.log("Server started on port 3000", ["startup"]);
});
Check our Troubleshooting guide or the FAQ. Or just email us. We're friendly hamsters who actually respond!