Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
52.00% covered (warning)
52.00%
13 / 25
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
LoggerService
52.00% covered (warning)
52.00%
13 / 25
40.00% covered (danger)
40.00%
2 / 5
44.31
0.00% covered (danger)
0.00%
0 / 1
 addLogger
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 getLogger
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 log
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 loadLogger
56.25% covered (warning)
56.25%
9 / 16
0.00% covered (danger)
0.00%
0 / 1
15.78
 isLoggerLoaded
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
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) 2017 (original work) Open Assessment Technologies SA
19 *
20 */
21
22namespace oat\oatbox\log;
23
24use oat\oatbox\service\ConfigurableService;
25use Psr\Log\LoggerInterface;
26use Psr\Log\LoggerTrait;
27use Psr\Log\NullLogger;
28
29class LoggerService extends ConfigurableService implements LoggerInterface
30{
31    use LoggerTrait;
32
33    public const SERVICE_ID = 'generis/log';
34
35    public const LOGGER_OPTION = 'logger';
36
37    /** @var LoggerInterface */
38    protected $logger;
39
40    /**
41     * Add a Psr3 logger to LoggerService instance
42     * Previous and new logger are encapsulated into a LoggerAggregator
43     *
44     * @param LoggerInterface $logger
45     * @return LoggerAggregator|LoggerInterface
46     */
47    public function addLogger(LoggerInterface $logger)
48    {
49        if ($this->isLoggerLoaded() && !($this->logger instanceof NullLogger)) {
50            $logger = new LoggerAggregator([$logger, $this->logger]);
51        }
52        $this->logger = $logger;
53        return $logger;
54    }
55
56    /**
57     * Get the logger
58     *
59     * @return LoggerInterface
60     */
61    public function getLogger()
62    {
63        if (!$this->isLoggerLoaded()) {
64            $this->loadLogger();
65        }
66        return $this->logger;
67    }
68
69    /**
70     * Wrap a log to built logger
71     *
72     * @param mixed $level
73     * @param string $message
74     * @param array $context
75     */
76    public function log($level, $message, array $context = [])
77    {
78        $this->getLogger()->log($level, $message, $context);
79    }
80
81    /**
82     * Load the logger from configuration
83     *
84     * If options does not contain any Psr3 Logger, NullLogger is set by default
85     *
86     * @return LoggerInterface
87     */
88    protected function loadLogger()
89    {
90        $logger = null;
91        if ($this->hasOption(self::LOGGER_OPTION)) {
92            $loggerOptions = $this->getOption(self::LOGGER_OPTION);
93            if (is_object($loggerOptions)) {
94                if (is_a($loggerOptions, LoggerInterface::class)) {
95                    $logger = $loggerOptions;
96                }
97            } elseif (is_array($loggerOptions) && isset($loggerOptions['class'])) {
98                $classname = $loggerOptions['class'];
99                if (is_a($classname, LoggerInterface::class, true)) {
100                    if (isset($loggerOptions['options'])) {
101                        $logger = new $classname($loggerOptions['options']);
102                    } else {
103                        $logger = new $classname();
104                    }
105                }
106            }
107        }
108
109        if (!is_null($logger)) {
110            $this->logger = $logger;
111        } else {
112            $this->logger = new NullLogger();
113        }
114
115        return $this->logger;
116    }
117
118    /**
119     * Check if the logger is loaded from configuration
120     *
121     * @return bool
122     */
123    protected function isLoggerLoaded()
124    {
125        return $this->logger instanceof LoggerInterface;
126    }
127}