The Hamsterlog PHP SDK is designed for web applications, WordPress sites, Laravel projects, and any PHP application.
Download the Hamsterlog file for PHP (yes, it's just one tiny file!):
Download hamster-php.phpPlace the file in your project directory and include it where you want to use it:
<?php
require_once 'hamster-php.php';
<?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');
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,
]);
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']);
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']);
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']);
allow_url_fopen
or
cURL)
copyToErrorLog
enabled to see local logsallow_url_fopen
is enabled or use cURL
Use PHP's register_shutdown_function()
to ensure logs are
sent even if your script encounters a fatal error.
Check our Troubleshooting guide or the FAQ for more help with PHP-specific issues.