Hamsterlog works with Bun, perfect for web servers, APIs and CLI tools.
Download the Hamsterlog file for Bun (yes, it's just one tiny file!):
Download hamster-bun.tsPlace the file in your project directory and import it:
import { Hamster } from "./hamster-bun.ts";
import { Hamster } from "./hamster-bun.ts";
// Initialize Hamsterlog
const hamster = new Hamster({
apiKey: "YOUR_API_KEY",
defaultTags: ["bun", "my-bun-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"]);
Want to add Hamsterlog to your Bun server? Here's how simple it is:
import { Hamster } from "./hamster-bun.ts";
// Initialize Hamsterlog
const hamster = new Hamster({
apiKey: "YOUR_API_KEY",
defaultTags: ["bun", "server"],
});
const server = Bun.serve({
port: 3000,
async fetch(req) {
const url = new URL(req.url);
// Your logs automatically go to Hamsterlog
console.log(`Someone visited: ${url.pathname}`);
if (url.pathname === "/") {
return new Response("Hello World!");
}
if (url.pathname === "/api/health") {
console.log("Health check - all good!");
return new Response("OK");
}
// This log will now have the tags: bun, server, error, 404
hamster.error("Not Found", ["404"]);
return new Response("Not Found", { status: 404 });
},
});
console.log("Server running on port 3000");
Check our Troubleshooting guide or the FAQ. Or just email us. We're friendly hamsters who actually respond!