Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
80.00% |
12 / 15 |
|
70.00% |
7 / 10 |
CRAP | |
0.00% |
0 / 1 |
| LoggerAwareTrait | |
80.00% |
12 / 15 |
|
70.00% |
7 / 10 |
14.35 | |
0.00% |
0 / 1 |
| setLogger | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getLogger | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| logEmergency | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| logAlert | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| logCritical | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| logError | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| logWarning | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| logNotice | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| logInfo | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| logDebug | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2016 (original work) Open Assessment Technologies SA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\oatbox\log; |
| 23 | |
| 24 | use Psr\Log\LoggerInterface; |
| 25 | use Psr\Log\NullLogger; |
| 26 | use oat\oatbox\service\ServiceManager; |
| 27 | use Zend\ServiceManager\ServiceLocatorAwareInterface; |
| 28 | |
| 29 | /** |
| 30 | * Trait for classes that want to use the Logger |
| 31 | * |
| 32 | * @author Joel Bout <joel@taotesting.com> |
| 33 | */ |
| 34 | trait LoggerAwareTrait |
| 35 | { |
| 36 | private $logger; |
| 37 | |
| 38 | /** |
| 39 | * Set a new logger |
| 40 | * |
| 41 | * @param LoggerInterface $logger |
| 42 | */ |
| 43 | public function setLogger(LoggerInterface $logger) |
| 44 | { |
| 45 | $this->logger = $logger; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Get the logger based on service manager |
| 50 | * |
| 51 | * @return \Psr\Log\LoggerInterface |
| 52 | */ |
| 53 | public function getLogger() |
| 54 | { |
| 55 | if ($this->logger instanceof LoggerInterface) { |
| 56 | return $this->logger; |
| 57 | } |
| 58 | if ($this instanceof ServiceLocatorAwareInterface) { |
| 59 | $logger = $this->getServiceLocator()->get(LoggerService::SERVICE_ID); |
| 60 | } else { |
| 61 | $logger = ServiceManager::getServiceManager()->get(LoggerService::SERVICE_ID); |
| 62 | } |
| 63 | return ($logger instanceof LoggerInterface) ? $logger : new NullLogger(); |
| 64 | } |
| 65 | |
| 66 | // Helpers |
| 67 | |
| 68 | /** |
| 69 | * Logs an emergency |
| 70 | * |
| 71 | * @param string $message |
| 72 | * @param array $context |
| 73 | */ |
| 74 | public function logEmergency($message, $context = []) |
| 75 | { |
| 76 | $this->getLogger()->emergency($message, $context); |
| 77 | } |
| 78 | |
| 79 | public function logAlert($message, $context = []) |
| 80 | { |
| 81 | $this->getLogger()->alert($message, $context); |
| 82 | } |
| 83 | |
| 84 | public function logCritical($message, $context = []) |
| 85 | { |
| 86 | $this->getLogger()->critical($message, $context); |
| 87 | } |
| 88 | |
| 89 | public function logError($message, $context = []) |
| 90 | { |
| 91 | $this->getLogger()->error($message, $context); |
| 92 | } |
| 93 | |
| 94 | public function logWarning($message, $context = []) |
| 95 | { |
| 96 | $this->getLogger()->warning($message, $context); |
| 97 | } |
| 98 | |
| 99 | public function logNotice($message, $context = []) |
| 100 | { |
| 101 | $this->getLogger()->notice($message, $context); |
| 102 | } |
| 103 | |
| 104 | public function logInfo($message, $context = []) |
| 105 | { |
| 106 | $this->getLogger()->info($message, $context); |
| 107 | } |
| 108 | |
| 109 | public function logDebug($message, $context = []) |
| 110 | { |
| 111 | $this->getLogger()->debug($message, $context); |
| 112 | } |
| 113 | } |