summaryrefslogtreecommitdiffstats
path: root/vendor/gipfl/log/src/Logger.php
blob: 1cbeb78146e18e1d472e8f58e1370529f7c84f61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php

namespace gipfl\Log;

use Psr\Log\LoggerInterface;
use function array_values;
use function spl_object_hash;

class Logger implements LoggerInterface
{
    /** @deprecated please use LogLevel::LEVEL_EMERGENCY */
    const LEVEL_EMERGENCY = LogLevel::LEVEL_EMERGENCY;
    /** @deprecated please use LogLevel::LEVEL_ALERT */
    const LEVEL_ALERT = LogLevel::LEVEL_ALERT;
    /** @deprecated please use LogLevel::LEVEL_CRITICAL */
    const LEVEL_CRITICAL = LogLevel::LEVEL_CRITICAL;
    /** @deprecated please use LogLevel::LEVEL_ERROR */
    const LEVEL_ERROR = LogLevel::LEVEL_ERROR;
    /** @deprecated please use LogLevel::LEVEL_WARNING */
    const LEVEL_WARNING = LogLevel::LEVEL_WARNING;
    /** @deprecated please use LogLevel::LEVEL_NOTICE */
    const LEVEL_NOTICE = LogLevel::LEVEL_NOTICE;
    /** @deprecated please use LogLevel::LEVEL_INFO */
    const LEVEL_INFO = LogLevel::LEVEL_INFO;
    /** @deprecated please use LogLevel::LEVEL_DEBUG */
    const LEVEL_DEBUG = LogLevel::LEVEL_DEBUG;
    /** @deprecated Please use LogLevel::MAP_NAME_TO_LEVEL */
    const MAP_NAME_TO_LEVEL = LogLevel::MAP_NAME_TO_LEVEL;

    /** @var LogWriter[] */
    protected $writers = [];

    /** @var LogFilter[] */
    protected $filters = [];

    /**
     * @param LogWriter $writer
     */
    public function addWriter(LogWriter $writer)
    {
        $this->writers[spl_object_hash($writer)] = $writer;
    }

    /**
     * @param LogFilter $filter
     */
    public function addFilter(LogFilter $filter)
    {
        $this->filters[spl_object_hash($filter)] = $filter;
    }

    /**
     * @return LogWriter[]
     */
    public function getWriters()
    {
        return array_values($this->writers);
    }

    /**
     * @return LogFilter[]
     */
    public function getFilters()
    {
        return array_values($this->filters);
    }

    /**
     * @param LogWriter $writer
     */
    public function removeWriter(LogWriter $writer)
    {
        unset($this->filters[spl_object_hash($writer)]);
    }

    /**
     * @param LogFilter $filter
     */
    public function removeFilter(LogFilter $filter)
    {
        unset($this->filters[spl_object_hash($filter)]);
    }

    /**
     * @deprecated Please use LogLevel::mapNameToNumeric()
     */
    public static function mapLogLevel($name)
    {
        return LogLevel::mapNameToNumeric($name);
    }

    public function emergency($message, array $context = [])
    {
        $this->log(__FUNCTION__, $message, $context);
    }

    public function alert($message, array $context = [])
    {
        $this->log(__FUNCTION__, $message, $context);
    }

    public function critical($message, array $context = [])
    {
        $this->log(__FUNCTION__, $message, $context);
    }

    public function error($message, array $context = [])
    {
        $this->log(__FUNCTION__, $message, $context);
    }

    public function warning($message, array $context = [])
    {
        $this->log(__FUNCTION__, $message, $context);
    }

    public function notice($message, array $context = [])
    {
        $this->log(__FUNCTION__, $message, $context);
    }

    public function info($message, array $context = [])
    {
        $this->log(__FUNCTION__, $message, $context);
    }

    public function debug($message, array $context = [])
    {
        $this->log(__FUNCTION__, $message, $context);
    }

    public function wants($level, $message, array $context = [])
    {
        foreach ($this->filters as $filter) {
            if (! $filter->wants($level, $message, $context)) {
                return false;
            }
        }

        return true;
    }

    public function log($level, $message, array $context = [])
    {
        if (! $this->wants($level, $message, $context)) {
            return;
        }

        foreach ($this->writers as $writer) {
            if ($writer instanceof LogWriterWithContext) {
                $writer->write($level, $message, $context);
            } else {
                $writer->write($level, $this->formatMessage(
                    $message,
                    $context
                ));
            }
        }
    }

    protected function formatMessage($message, $context = [])
    {
        if (empty($context)) {
            return $message;
        } else {
            return \sprintf($message, $context);
        }
    }
}