Skip to content

Instantly share code, notes, and snippets.

@voidabhi
Last active February 16, 2016 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voidabhi/88b18ec3114d4b7844e4 to your computer and use it in GitHub Desktop.
Save voidabhi/88b18ec3114d4b7844e4 to your computer and use it in GitHub Desktop.
Php Logging
{
"name": "logging",
"type": "library",
"description": "PHP Logging",
"keywords": [
"Log",
"Logging"
],
"license": "MIT",
"authors": [
{
"name": "Abhijeet Mohan", "email": "void.aby@gmail.com", "role": "Developer"
}
],
"require": {
"php": ">=5.4",
"psr/log": "~1"
}
}
<?php
namespace Logging;
use Psr\Log\AbstractLogger;
/**
* Class ConsoleLogger
*
* @package Thruway
*/
class ConsoleLogger extends AbstractLogger
{
/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
* @return null
*/
public function log($level, $message, array $context = [])
{
$now = date("Y-m-d\TH:i:s") . substr((string)microtime(), 1, 8);
echo $now . " " . str_pad($level, 10, " ") . " " . $message . "\n";
}
}
<?php
namespace Logging;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
class Logger
{
/**
* @var LoggerInterface
*/
private static $logger;
/**
* @param LoggerInterface $logger
*/
public static function set(LoggerInterface $logger)
{
static::$logger = $logger;
}
/**
* @param null $object
* @param $level
* @param $message
* @param array $context
* @return null
*/
public static function log($object = null, $level, $message, $context = [])
{
if (is_object($object)) {
$className = get_class($object);
$pid = getmypid();
$message = "[{$className} {$pid}] {$message}";
}
if (static::$logger == null) {
static::$logger = new ConsoleLogger();
}
return static::$logger->log($level, $message, $context);
}
/**
* @param null $object
* @param null $object
* @param $message
* @param array $context
* @return null
*/
public static function alert($object = null, $message, $context = [])
{
static::log($object, LogLevel::ALERT, $message, $context);
}
/**
* @param null $object
* @param $message
* @param array $context
* @return null
*/
public static function critical($object = null, $message, $context = [])
{
static::log($object, LogLevel::CRITICAL, $message, $context);
}
/**
* @param null $object
* @param $message
* @param array $context
* @return null
*/
public static function debug($object = null, $message, $context = [])
{
static::log($object, LogLevel::DEBUG, $message, $context);
}
/**
* @param null $object
* @param $message
* @param array $context
* @return null
*/
public static function emergency($object = null, $message, $context = [])
{
static::log($object, LogLevel::EMERGENCY, $message, $context);
}
/**
* @param null $object
* @param $message
* @param array $context
* @return null
*/
public static function error($object = null, $message, $context = [])
{
static::log($object, LogLevel::ERROR, $message, $context);
}
/**
* @param null $object
* @param $message
* @param array $context
* @return null
*/
public static function info($object = null, $message, $context = [])
{
static::log($object, LogLevel::INFO, $message, $context);
}
/**
* @param null $object
* @param $message
* @param array $context
* @return null
*/
public static function notice($object = null, $message, $context = [])
{
static::log($object, LogLevel::NOTICE, $message, $context);
}
/**
* @param $message
* @param array $context
* @return null
*/
public static function warning($object = null, $message, $context = [])
{
static::log($object, LogLevel::WARNING, $message, $context);
}
/**
* Protected constructor to prevent creating a new instance of the
* *Singleton* via the `new` operator from outside of this class.
*/
protected function __construct()
{
}
/**
* Private clone method to prevent cloning of the instance of the
* *Singleton* instance.
*
* @return void
*/
private function __clone()
{
}
/**
* Private unserialize method to prevent unserializing of the *Singleton*
* instance.
*
* @return void
*/
private function __wakeup()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment