Hamster
hamsterlog
PHP

🐘 Hamsterlog for PHP

The Hamsterlog PHP SDK is designed for web applications, WordPress sites, Laravel projects, and any PHP application.

Installation

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

Download hamster-php.php

Place the file in your project directory and include it where you want to use it:

<?php
require_once 'hamster-php.php';

Quick Start

<?php
require_once 'hamster-php.php';

// Initialize Hamsterlog
$hamster = new Hamster([
    'apiKey' => 'YOUR_API_KEY',
    'defaultTags' => ['php', 'my-php-site'],
    'copyToErrorLog' => true,
]);

// Start logging
$hasmter->log("This log shows up in Hamsterlog!");
$hasmter->warn("This warning too.");
$hasmter->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"]);

// Some examples of errors that will show up in Hamsterlog:

// Undefined variable (E_NOTICE)
echo $undefinedVariable;

// Division by zero (E_WARNING)
$x = 1 / 0;

// User-triggered error (E_USER_ERROR)
trigger_error('This is a custom error', E_USER_ERROR);

// Call to undefined function (E_ERROR)
nonexistent_function();

// Type error (E_WARNING)
$array = [];
echo $array['nonexistent_key'];

// Exception
throw new Exception('An error thrown in php');

Configuration Options

The Hamster constructor accepts the following configuration options:

<?php
$hamster = new Hamster([
    // Required: Your Hamsterlog API key
    'apiKey' => 'your-api-key-here',
    
    // Optional: Tags added to every log (default: [])
    'defaultTags' => ['php', 'production', 'webapp'],
    
    // Optional: Also write to PHP error log (default: true)
    'copyToErrorLog' => true,
    
    // Optional: Capture errors and send them to Hamsterlog (default: true)
    'captureErrors' => true,

    // Optional: Capture exceptions and send them to Hamsterlog (default: true)
    'captureExceptions' => true,
]);

Configuration Details

  • apiKey (required): Your unique API key from the Hamsterlog dashboard
  • defaultTags: Array of strings that will be added to every log
  • copyToErrorLog: When true, logs are also written to PHP's error log
  • captureErrors: Capture errors and send them to Hamsterlog
  • captureExceptions: Capture exceptions and send them to Hamsterlog

API Methods

$hamster->log($message, $tags)

Send a general log message.

<?php
$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.

<?php
$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.

<?php
$hamster->error('Database connection failed', ['database', 'critical']);
$hamster->error('Invalid user input: ' . $userInput, ['validation']);
$hamster->error('Payment processing failed for order #1234', ['payment', 'failure']);

Troubleshooting

Logs not appearing?

  • Check that your API key is correct
  • Verify PHP has network access (check allow_url_fopen or cURL)
  • Check PHP error logs for any HTTP errors
  • Test with copyToErrorLog enabled to see local logs

Common PHP Issues

  • Ensure allow_url_fopen is enabled or use cURL
  • Check that the server can make outbound HTTP requests
  • Verify JSON extension is installed and enabled

💡 Pro Tip

Use PHP's register_shutdown_function() to ensure logs are sent even if your script encounters a fatal error.

🔧 Need Help?

Check our Troubleshooting guide or the FAQ for more help with PHP-specific issues.