Tlog implement Psr\Log\LoggerInterface

This commit is contained in:
Manuel Raynaud
2013-01-20 23:55:18 +01:00
parent 88a82c69d1
commit ad17c5d99e
5 changed files with 175 additions and 126 deletions

View File

@@ -10,7 +10,8 @@
"symfony/console" : "2.1.*", "symfony/console" : "2.1.*",
"symfony/event-dispatcher" : "2.1.*", "symfony/event-dispatcher" : "2.1.*",
"symfony-cmf/routing" : "1.0.*@dev", "symfony-cmf/routing" : "1.0.*@dev",
"propel/propel1" : "1.6.*" "propel/propel1" : "1.6.*",
"psr/log" : "dev-master"
}, },
"minimum-stability": "stable" "minimum-stability": "stable"
} }

52
core/composer.lock generated
View File

@@ -1,5 +1,5 @@
{ {
"hash": "2be3cc68d014718aa32d78effb38b917", "hash": "8a668b67ef3c3dd2fe8f426c37b2db5d",
"packages": [ "packages": [
{ {
"name": "phing/phing", "name": "phing/phing",
@@ -113,6 +113,44 @@
"mapping" "mapping"
] ]
}, },
{
"name": "psr/log",
"version": "dev-master",
"source": {
"type": "git",
"url": "https://github.com/php-fig/log",
"reference": "1.0.0"
},
"dist": {
"type": "zip",
"url": "https://github.com/php-fig/log/archive/1.0.0.zip",
"reference": "1.0.0",
"shasum": ""
},
"time": "2012-12-21 11:40:51",
"type": "library",
"autoload": {
"psr-0": {
"Psr\\Log\\": ""
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for logging libraries",
"keywords": [
"log",
"psr",
"psr-3"
]
},
{ {
"name": "symfony-cmf/routing", "name": "symfony-cmf/routing",
"version": "dev-master", "version": "dev-master",
@@ -120,12 +158,12 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony-cmf/Routing", "url": "https://github.com/symfony-cmf/Routing",
"reference": "1.0.0-alpha3" "reference": "817b65edad2a4318cffd5017ff7eaabb9f2b65f8"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://github.com/symfony-cmf/Routing/archive/1.0.0-alpha3.zip", "url": "https://github.com/symfony-cmf/Routing/archive/817b65edad2a4318cffd5017ff7eaabb9f2b65f8.zip",
"reference": "1.0.0-alpha3", "reference": "817b65edad2a4318cffd5017ff7eaabb9f2b65f8",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@@ -133,7 +171,7 @@
"symfony/routing": ">=2.1,<2.3-dev", "symfony/routing": ">=2.1,<2.3-dev",
"symfony/http-kernel": ">=2.1,<2.3-dev" "symfony/http-kernel": ">=2.1,<2.3-dev"
}, },
"time": "2012-12-16 17:52:57", "time": "2013-01-20 20:39:41",
"type": "library", "type": "library",
"extra": { "extra": {
"branch-alias": { "branch-alias": {
@@ -145,6 +183,7 @@
"Symfony\\Cmf\\Component\\Routing": "" "Symfony\\Cmf\\Component\\Routing": ""
} }
}, },
"notification-url": "https://packagist.org/downloads/",
"license": [ "license": [
"MIT" "MIT"
], ],
@@ -554,6 +593,7 @@
], ],
"minimum-stability": "stable", "minimum-stability": "stable",
"stability-flags": { "stability-flags": {
"symfony-cmf/routing": 20 "symfony-cmf/routing": 20,
"psr/log": 20
} }
} }

View File

@@ -1,44 +0,0 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Core\Bundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use \Propel;
class PropelBundle extends Bundle
{
/**
*
* Construct the depency injection builder
*
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
*/
public function build(ContainerBuilder $container)
{
Propel::init(THELIA_CONF_DIR . "/config_thelia.php");
}
}

View File

@@ -34,10 +34,25 @@ namespace Thelia\Core;
use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Thelia\Core\Bundle; use Thelia\Core\Bundle;
use Propel;
class Thelia extends Kernel class Thelia extends Kernel
{ {
public function init()
{
parent::init();
$this->initPropel();
}
protected function initPropel()
{
Propel::init(THELIA_CONF_DIR . "/config_thelia.php");
}
/** /**
* Initializes the service container. * Initializes the service container.
* *
@@ -46,12 +61,27 @@ class Thelia extends Kernel
* The cached version of the service container is used when fresh, otherwise the * The cached version of the service container is used when fresh, otherwise the
* container is built. * container is built.
*/ */
protected function initializeContainer() // protected function initializeContainer()
{ // {
$this->container = $this->buildContainer(); // $class = $this->getContainerClass();
//$this->container->set('kernel', $this); // $cache = new ConfigCache($this->getCacheDir().'/'.$class.'.php', $this->debug);
// $fresh = true;
} // if (!$cache->isFresh()) {
// $container = $this->buildContainer();
// $this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass());
//
// $fresh = false;
// }
//
// require_once $cache;
//
// $this->container = new $class();
//
// if (!$fresh && $this->container->has('cache_warmer')) {
// $this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir'));
// }
//
// }
/** /**
* Gets the cache directory. * Gets the cache directory.
@@ -91,17 +121,17 @@ class Thelia extends Kernel
* *
* @return ContainerBuilder The compiled service container * @return ContainerBuilder The compiled service container
*/ */
protected function buildContainer() // protected function buildContainer()
{ // {
$container = $this->getContainerBuilder(); // $container = $this->getContainerBuilder();
$container->set('kernel', $this); // $container->set('kernel', $this);
//
foreach ($this->bundles as $bundle) { // foreach ($this->bundles as $bundle) {
$bundle->build($container); // $bundle->build($container);
} // }
//
return $container; // return $container;
} // }
/** /**
* return available bundle * return available bundle
@@ -115,8 +145,7 @@ class Thelia extends Kernel
{ {
$bundles = array( $bundles = array(
/* TheliaBundle contain all the dependency injection description */ /* TheliaBundle contain all the dependency injection description */
new Bundle\TheliaBundle(), new Bundle\TheliaBundle()
new Bundle\PropelBundle()
); );
/** /**

View File

@@ -23,6 +23,7 @@
namespace Thelia\Log; namespace Thelia\Log;
use Thelia\Model\ConfigQuery; use Thelia\Model\ConfigQuery;
use Psr\Log\LoggerInterface;
/** /**
* *
@@ -33,7 +34,7 @@ use Thelia\Model\ConfigQuery;
* @author Franck Allimant <franck@cqfdev.fr> * @author Franck Allimant <franck@cqfdev.fr>
* @author Manuel Raynaud <mraynaud@openstudio.fr> * @author Manuel Raynaud <mraynaud@openstudio.fr>
*/ */
class Tlog Implements TlogInterface class Tlog Implements LoggerInterface
{ {
// Nom des variables de configuration // Nom des variables de configuration
const VAR_LEVEL = "tlog_level"; const VAR_LEVEL = "tlog_level";
@@ -44,13 +45,26 @@ class Tlog Implements TlogInterface
const VAR_SHOW_REDIRECT = "tlog_show_redirect"; const VAR_SHOW_REDIRECT = "tlog_show_redirect";
// all level of trace // all level of trace
const TRACE = 100; const DEBUG = 100;
const DEBUG = 200; const INFO = 200;
const WARNING = 300; const NOTICE = 300;
const INFO = 400; const WARNING = 400;
const ERROR = 500; const ERROR = 500;
const FATAL = 600; const CRITICAL = 600;
const ALERT = 700;
const EMERGENCY = 800;
const MUET = PHP_INT_MAX; const MUET = PHP_INT_MAX;
protected $levels = array(
100 => "DEBUG",
200 => "INFO",
300 => "NOTICE",
400 => "WARNING",
500 => "ERROR",
600 => "CRITICAL",
700 => "ALERT",
800 => "EMERGENCY"
);
// default values // default values
const DEFAULT_LEVEL = self::DEBUG; const DEFAULT_LEVEL = self::DEBUG;
@@ -187,65 +201,54 @@ class Tlog Implements TlogInterface
// Methodes d'accès aux traces // Methodes d'accès aux traces
// --------------------------- // ---------------------------
public function trace() public function debug($message, array $context = array())
{ {
if ($this->level > self::TRACE) $this->log(self::DEBUG, $message, $context);
return;
$args = func_get_args();
$this->out("TRACE", $args);
} }
public function debug() public function info($message, array $context = array())
{ {
if ($this->level > self::DEBUG) $this->log(self::INFO, $message, $context);
return; }
$args = func_get_args(); public function notice($message, array $context = array())
{
$this->out("DEBUG", $args); $this->log(self::NOTICE, $message, $context);
} }
public function info() public function warning($message, array $context = array())
{ {
if ($this->level > self::INFO) $this->log(self::WARNING, $message, $context);
return;
$args = func_get_args();
$this->out("INFO", $args);
} }
public function warning() public function error($message, array $context = array())
{ {
if ($this->level > self::WARNING) $this->log(self::ERROR, $message, $context);
}
public function critical($message, array $context = array())
{
$this->log(self::CRITICAL, $message, $context);
}
public function alert($message, array $context = array())
{
$this->log(self::ALERT, $message, $context);
}
public function emergency($message, array $context = array())
{
$this->log(self::EMERGENCY, $message, $context);
}
public function log($level, $message, array $context = array()) {
if($this->level > $level && array_key_exists($level, $this->levels) === false)
return; return;
$args = func_get_args(); $this->out($this->levels[$level], $message, $context);
$this->out("WARNING", $args);
} }
public function error()
{
if ($this->level > self::ERROR)
return;
$args = func_get_args();
$this->out("ERREUR", $args);
}
public function fatal()
{
if ($this->level > self::FATAL)
return;
$args = func_get_args();
$this->out("FATAL", $args);
}
// Mode back office // Mode back office
public static function SetBackOfficeMode($booleen) public static function SetBackOfficeMode($booleen)
@@ -368,14 +371,34 @@ class Tlog Implements TlogInterface
return $origine; return $origine;
} }
protected function interpolate($message, array $context = array())
{
// build a replacement array with braces around the context keys
$replace = array();
foreach ($context as $key => $val) {
$replace['{' . $key . '}'] = $val;
}
private function out($level, $tabargs) // interpolate replacement values into the message and return
return strtr($message, $replace);
}
private function out($level, $message, array $context = array())
{ {
$text = ''; $text = '';
foreach ($tabargs as $arg) { /*if (is_array($message)) {
$text .= is_scalar($arg) ? $arg : print_r($arg, true); foreach ($message as $arg) {
$text .= is_scalar($arg) ? $arg : print_r($arg, true);
}
} else */ if (is_scalar($message) === false) {
$text = print_r($message, true);
} else {
$text = $message;
} }
$text = $this->interpolate($text, $context);
$origine = $this->findOrigin(); $origine = $this->findOrigin();