Hamsterlog works with Deno. Perfect for web servers, APIs, CLI tools, and edge functions.
Download the Hamsterlog file for Deno (yes, it's just one tiny file!):
Download hamster-deno.tsPlace the file in your project directory and import it:
import { Hamster } from "./hamster-deno.ts";
import { Hamster } from "./hamster-deno.ts";
// Initialize Hamsterlog
const hamster = new Hamster({
apiKey: "YOUR_API_KEY",
defaultTags: ["deno", "my-deno-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 a Deno server:
import { Hamster } from "./hamster-deno.ts";
import { serve } from "https://deno.land/[email protected]/http/server.ts";
// Initialize Hamsterlog
const hamster = new Hamster({
apiKey: Deno.env.get("HAMSTER_API_KEY")!,
defaultTags: ["deno", "server"],
});
hamster.log("Starting Deno server", ["startup"]);
async function handler(req: Request): Promise<Response> {
const url = new URL(req.url);
// Log each request
hamster.log(`${req.method} ${url.pathname}`, ["request", req.method.toLowerCase()]);
try {
// Handle routes
if (url.pathname === "/api/users") {
hamster.log("Users API called", ["api", "users"]);
return new Response(JSON.stringify({ users: [] }), {
headers: { "Content-Type": "application/json" },
});
}
if (url.pathname === "/health") {
hamster.log("Health check", ["health"]);
return new Response("OK");
}
// This log will now have the tags: deno, server, error, 404
hamster.error("Not Found", ["404"]);
return new Response("Not Found", { status: 404 });
} catch (error) {
hamster.error(`Server error on ${url.pathname}: ${error.message}`, ["server"]);
return new Response("Internal Server Error", { status: 500 });
}
}
await serve(handler, { port: 8000 });
hamster.log("Server running on port 8000", ["startup", "success"]);
Deno's permission system affects how Hamsterlog works:
--allow-net
: Required for sending logs to the
Hamsterlog API
--allow-env
: Required to read environment variables
(optional)
# Basic usage
deno run --allow-net --allow-env app.ts
# Development with all permissions
deno run --allow-all app.ts
For production, use specific permissions:
# Only allow network access to your Hamsterlog API
deno run --allow-net=hamsterlog.dev --allow-env=HAMSTER_API_KEY app.ts
Check our Troubleshooting guide or the FAQ. Or just email us. We're friendly hamsters who actually respond!