Plop
A simple logging library for PHP
Psr3Logger.php
1 <?php
2 /*
3  This file is part of Plop, a simple logging library for PHP.
4 
5  Copyright © 2014 François Poirotte
6 
7  Plop is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Plop is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Plop. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 namespace Plop;
22 
28 class Psr3Logger extends \Psr\Log\AbstractLogger
29 {
30  protected static $factory = null;
31  protected static $instance = null;
32 
41  public function __construct()
42  {
43  if (static::$factory === null) {
44  static::$factory = new \Plop\RecordFactory(
45  new \Plop\Interpolator\Psr3()
46  );
47  }
48  }
49 
58  public static function getInstance()
59  {
60  if (static::$instance === null) {
61  static::$instance = new static();
62  }
63  return static::$instance;
64  }
65 
82  public function log($level, $message, array $context = array())
83  {
84  /***
85  * \note
86  * There are several contexts in which the follow code
87  * may not work as expected.
88  *
89  * Using threads (https://github.com/krakjoe/pthreads)
90  * with this method will probably mess things up.
91  *
92  * The following functions may also cause trouble
93  * depending on how they're used:
94  * - register_tick_function()
95  * - debug_backtrace()
96  */
97  $logging = \Plop\Plop::getInstance();
98  $factory = $logging->getRecordFactory();
99  try {
100  // Switch to a factory that uses PSR3-interpolation.
101  $logging->setRecordFactory(static::$factory);
102  $logging->$level($message, $context);
103  } catch (Exception $e) {
104  }
105 
106  // Restore the original factory.
107  $logging->setRecordFactory($factory);
108  }
109 }
static & getInstance()
Definition: Plop.php:195
log($level, $message, array $context=array())
Definition: Psr3Logger.php:82
A special logger which acts as a bridge between PSR-3 loggers and Plop.
Definition: Psr3Logger.php:28
This exception is thrown by Plop whenever a problem is detected.
Definition: Exception.php:30
static getInstance()
Definition: Psr3Logger.php:58