Skip to content

Instantly share code, notes, and snippets.

@vosiander
Last active December 13, 2015 17:08
Show Gist options
  • Save vosiander/4945262 to your computer and use it in GitHub Desktop.
Save vosiander/4945262 to your computer and use it in GitHub Desktop.
Small logger class for proof-of-concept
<?php
/**
* Basic Logger Class which handles the setting and retrieving of captured arguments
*/
class Log
{
/**
* @var array
*/
private static $environment = array();
/**
* @param $msg
*/
public static function it($msg)
{
$backtrace = debug_backtrace();
$calledFunctionName = !empty($backtrace[4]) ? $backtrace[4]['function'] : 'unnamed';
echo call_user_func_array('sprintf', array_merge((array)$msg, static::$environment[$calledFunctionName]));
}
/**
* Sets the environmental variables
*/
public static function set()
{
$backtrace = debug_backtrace();
$calledFunctionName = !empty($backtrace[2]) ? $backtrace[2]['function'] : 'unnamed';
static::$environment[$calledFunctionName] = $backtrace[2]['args'];
}
}
/**
* Used for capturing and storing of arguments
*/
trait Logger
{
/**
* Hook into every Class call to capture all arguments
* @param $name
* @param $args
* @return mixed
*/
public function __call($name, $args)
{
Log::set();
$result = call_user_func_array(array($this, $name), $args);
return $result;
}
}
class Test
{
/** important! */
use Logger;
protected function doit($in, $out)
{
Log::it("Input from doit: in (%d) and out (%d)\n");
}
}
$t = new Test();
$t->doit(1, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment