Hamster
hamsterlog
Node.js

🌐 Hamsterlog for Node.js

Hamsterlog works with Node.js, perfect for web servers, APIs and CLI tools.

Installation

Download the Hamsterlog file for Node.js (yes, it's just one tiny file!):

Javascript version: Download hamster-node.js
or
Typescript version: Download hamster-node.ts

Place the file in your project directory and import it:

import { Hamster } from "./hamster-node.js";

Quick Start

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.

🍀 No migration headaches

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().

Configuration Options

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,
});

Methods

hamster.log(message, tags)

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"]);

hamster.warn(message, tags)

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"]);

hamster.error(message, tags)

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"]);

Example: Express.js Server

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"]);
});

Performance Notes

  • Automatic batching: Logs are automatically batched and sent every second to minimize network overhead.
  • Rate limiting: Built-in rate limiting prevents you from exceeding API limits.
  • Non-blocking: All network requests are asynchronous and won't block your application.
  • Memory efficient: Failed logs are queued and retried, but with reasonable memory limits.

🔧 Need Help?

Check our Troubleshooting guide or the FAQ. Or just email us. We're friendly hamsters who actually respond!