Hamster
hamsterlog
Best Practices

Best Practices

Environment Variables

Always use environment variables for your API key:

// .env file
HAMSTER_API_KEY="your-actual-api-key-here"

// In your code
const hamster = new Hamster({
    apiKey: process.env.HAMSTER_API_KEY,
    defaultTags: ["nodejs", process.env.NODE_ENV || "development"],
    captureConsole: true,
    copyToLocalConsole: process.env.NODE_ENV === "development",
});

Meaningful Tags

Use descriptive tags to make your logs easier to filter and search:

// Good: Specific and meaningful
hamster.log("User registration completed", ["auth", "registration", "success"]);
hamster.error("Payment failed: insufficient funds", ["payment", "stripe", "insufficient-funds"]);

// Not ideal: Too generic
hamster.log("Something happened", ["info"]);
hamster.error("Error occurred", ["error"]);

Error Handling

Log errors with context and useful information:

try {
    await processPayment(orderId, amount);
    hamster.log(`Payment processed successfully for order ${orderId}`, ["payment", "success"]);
} catch (error) {
    hamster.error(`Payment failed for order ${orderId}: ${error.message}`, [
        "payment", 
        "error", 
        error.code || "unknown-error"
    ]);
    throw error; // Re-throw if needed
}

Conditional Logging

You can conditionally enable logging based on environment:

const hamster = process.env.NODE_ENV === "production" 
    ? new Hamster({
        apiKey: process.env.HAMSTER_API_KEY,
        defaultTags: ["nodejs", "production"],
        captureConsole: true,
        copyToLocalConsole: false,
    })
    : new Hamster({
        apiKey: process.env.HAMSTER_API_KEY || "dev-key",
        defaultTags: ["nodejs", "development"],
        captureConsole: true,
        copyToLocalConsole: true,
    });