Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
LoggerAwareTrait
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
4 / 4
6
100.00% covered (success)
100.00%
1 / 1
 logError
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 logWarning
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 debug
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 log
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * Extension of \Laminas\Log\LoggerAwareTrait with some convenience methods.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Error_Logging
25 * @author   Chris Hallberg <challber@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org Main Site
28 */
29
30namespace VuFind\Log;
31
32use function get_class;
33
34/**
35 * Extension of \Laminas\Log\LoggerAwareTrait with some convenience methods.
36 *
37 * @category VuFind
38 * @package  Error_Logging
39 * @author   Chris Hallberg <challber@villanova.edu>
40 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
41 * @link     https://vufind.org Main Site
42 */
43trait LoggerAwareTrait
44{
45    use \Laminas\Log\LoggerAwareTrait;
46    use VarDumperTrait;
47
48    /**
49     * Log an error message.
50     *
51     * @param string $msg          Log message
52     * @param array  $context      Log context
53     * @param bool   $prependClass Prepend class name to message?
54     *
55     * @return void
56     */
57    protected function logError($msg, array $context = [], $prependClass = true)
58    {
59        $this->log('err', $msg, $context, $prependClass);
60    }
61
62    /**
63     * Log a warning message.
64     *
65     * @param string $msg          Log message
66     * @param array  $context      Log context
67     * @param bool   $prependClass Prepend class name to message?
68     *
69     * @return void
70     */
71    protected function logWarning($msg, array $context = [], $prependClass = true)
72    {
73        $this->log('warn', $msg, $context, $prependClass);
74    }
75
76    /**
77     * Log a debug message.
78     *
79     * @param string $msg          Log message
80     * @param array  $context      Log context
81     * @param bool   $prependClass Prepend class name to message?
82     *
83     * @return void
84     */
85    protected function debug($msg, array $context = [], $prependClass = true)
86    {
87        $this->log('debug', $msg, $context, $prependClass);
88    }
89
90    /**
91     * Send a message to the logger.
92     *
93     * @param string $level        Log level
94     * @param string $message      Log message
95     * @param array  $context      Log context
96     * @param bool   $prependClass Prepend class name to message?
97     *
98     * @return void
99     */
100    protected function log(
101        $level,
102        $message,
103        array $context = [],
104        $prependClass = false
105    ) {
106        if ($this->logger) {
107            if ($prependClass) {
108                $message = get_class($this) . ': ' . $message;
109            }
110            $this->logger->$level($message, $context);
111        }
112    }
113}