Skip to content

Instantly share code, notes, and snippets.

@Zenger
Created August 8, 2013 10:56
Show Gist options
  • Save Zenger/6183689 to your computer and use it in GitHub Desktop.
Save Zenger/6183689 to your computer and use it in GitHub Desktop.
Slugger.php
<?php
require_once('Slugger.php');
try {
if (!@mysql_connect())
{
// Let's fake a small eror
throw new Slug("Couldn't connect to database!");
}
}
catch(Slug $e) // we catch a slug
{
// instead of outputing the error, log it
$e->log();
// You can still work with the erorr like $e->getMessage();
}
?>
<?php
/**
* Slugger, simple logging class
* Writes logs into a file and sends them by e-mail once in a while
* Written for pure example
* @require PHP >= 5.3
* @todo : Fix bug when the class is firt time instantiated it send an empty message
*/
/* Don't forget to setup a date default timezone */
date_default_timezone_set("Europe/Chisinau"); // because
class Slugger
{
static $email = "zenger@inbox.ru"; // the e-mail to send logs to
static $from = "log@slugger.dom"; // simulate logger
static $mailer = "mail"; // send mail through
static $path = "logs"; // folder to keep logs in
static $report = 1; // how often to send logs in minutes
static $echo = true; // echo the exception?
static $lastLogName = "log.last_report.html"; // last log filename
public static function prerequisites()
{
if (!file_exists( self::$path ))
{
try{
@mkdir(self::$path);
}
catch(Exception $e)
{
die($e->getMessage()); // we failed to create a folder to keep logs in
}
}
if (!file_exists(self::$path . '/' . self::$lastLogName ))
{
try{
@touch(self::$path . '/' . self::$lastLogName );
}
catch(Exception $e)
{
die($e->getMessage());
}
}
if (!file_exists(self::$path . '/.htaccess' ))
{
try{
$str = "order allow,deny\ndeny from all";
file_put_contents(self::$path . '/.htaccess' , $str );
}
catch(Exception $e)
{
die($e->getMessage());
}
}
if (version_compare( PHP_VERSION, '5.3.0') >= 0)
{
// pass
}
else
{
die("This class works with php 5.3.0 and up. Your PHP version is ". PHP_VERSION);
}
}
public static function getLogFileName()
{
self::prerequisites(); // just in case, because we're paranoic
$last_log = filemtime( self::$path . '/' . self::$lastLogName );
$log = new DateTime('@'. $last_log );
$log = $log->add(date_interval_create_from_date_string(self::$report . 'minutes'));
return "log." . $log->getTimestamp() . ".html";
}
public static function log(Slug $exception )
{
self::prerequisites();
$currentLog = self::getLogFileName();
$str = "[%s]: %s\nLine: %s\nFile: %s\nCode: %s\n";
$str .= "\n" . str_repeat('-', 80) . "\n";
$message = sprintf( $str, date('d-m-Y H:i:s'), $exception->getMessage(), $exception->getLine(), $exception->getFile(), $exception->getCode());
try {
$fh = fopen( self::$path . "/" . $currentLog , 'a');
fwrite($fh , "\n" . $message);
fclose($fh);
} catch(Exception $e)
{
die("Couldn't write to log file, check file permissions");
}
// shall we send the log ?
$log = new DateTime('@'. filemtime( self::$path . '/' . self::$lastLogName ) );
$now = new DateTime( 'now' );
// check time passed since the last update
$passed = $log->diff( $now );
if (intval( $passed->format('%i') ) > self::$report )
{
self::sendLog($currentLog);
}
}
public static function sendLog( $logName )
{
// update the last log time
@touch(self::$path . '/' .self::$lastLogName );
// mail the log file name
self::mail( $logName );
}
public static function mail($logName)
{
// sendmail because we're lazy
// Mail headers
$headers = "From: ".self::$from."\r\nReply-To: ". self::$from;
// 26214400 = 25mb
if (filesize(self::$path . "/" . $logName) !== false && filesize(self::$path . "/". $logName) < 26214400)
{
@mail(self::$email, "Slugger Log as of ". date('d-m-Y H:i:s'), file_get_contents(self::$path . "/" . $logName) , $headers);
}
else
{
return false;
}
}
}
class Slug extends Exception
{
public function log()
{
Slugger::log( $this );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment