xref: /plugin/pureldap/vendor/freedsx/ldap/src/FreeDSx/Ldap/LoggerTrait.php (revision dad993c57a70866aa1db59c43f043769c2eb7ed0)
1*dad993c5SAndreas Gohr<?php
2*dad993c5SAndreas Gohr
3*dad993c5SAndreas Gohr/**
4*dad993c5SAndreas Gohr * This file is part of the FreeDSx LDAP package.
5*dad993c5SAndreas Gohr *
6*dad993c5SAndreas Gohr * (c) Chad Sikorra <Chad.Sikorra@gmail.com>
7*dad993c5SAndreas Gohr *
8*dad993c5SAndreas Gohr * For the full copyright and license information, please view the LICENSE
9*dad993c5SAndreas Gohr * file that was distributed with this source code.
10*dad993c5SAndreas Gohr */
11*dad993c5SAndreas Gohr
12*dad993c5SAndreas Gohrnamespace FreeDSx\Ldap;
13*dad993c5SAndreas Gohr
14*dad993c5SAndreas Gohruse FreeDSx\Ldap\Exception\RuntimeException;
15*dad993c5SAndreas Gohruse Psr\Log\LoggerInterface;
16*dad993c5SAndreas Gohruse Psr\Log\LogLevel;
17*dad993c5SAndreas Gohr
18*dad993c5SAndreas Gohr/**
19*dad993c5SAndreas Gohr * Some simple logging methods. Only logs if we have a logger in the options.
20*dad993c5SAndreas Gohr *
21*dad993c5SAndreas Gohr * @author Chad Sikorra <Chad.Sikorra@gmail.com>
22*dad993c5SAndreas Gohr */
23*dad993c5SAndreas Gohrtrait LoggerTrait
24*dad993c5SAndreas Gohr{
25*dad993c5SAndreas Gohr    /**
26*dad993c5SAndreas Gohr     * Logs a message and then throws a runtime exception.
27*dad993c5SAndreas Gohr     *
28*dad993c5SAndreas Gohr     * @throws RuntimeException
29*dad993c5SAndreas Gohr     */
30*dad993c5SAndreas Gohr    private function logAndThrow(
31*dad993c5SAndreas Gohr        string $message,
32*dad993c5SAndreas Gohr        array $context = []
33*dad993c5SAndreas Gohr    ): void {
34*dad993c5SAndreas Gohr        $this->log(
35*dad993c5SAndreas Gohr            LogLevel::ERROR,
36*dad993c5SAndreas Gohr            $message,
37*dad993c5SAndreas Gohr            $context
38*dad993c5SAndreas Gohr        );
39*dad993c5SAndreas Gohr
40*dad993c5SAndreas Gohr        throw new RuntimeException($message);
41*dad993c5SAndreas Gohr    }
42*dad993c5SAndreas Gohr
43*dad993c5SAndreas Gohr    protected function logError(
44*dad993c5SAndreas Gohr        string $message,
45*dad993c5SAndreas Gohr        array $context = []
46*dad993c5SAndreas Gohr    ): void {
47*dad993c5SAndreas Gohr        $this->log(
48*dad993c5SAndreas Gohr            LogLevel::ERROR,
49*dad993c5SAndreas Gohr            $message,
50*dad993c5SAndreas Gohr            $context
51*dad993c5SAndreas Gohr        );
52*dad993c5SAndreas Gohr    }
53*dad993c5SAndreas Gohr
54*dad993c5SAndreas Gohr    protected function logInfo(
55*dad993c5SAndreas Gohr        string $message,
56*dad993c5SAndreas Gohr        array $context = []
57*dad993c5SAndreas Gohr    ): void {
58*dad993c5SAndreas Gohr        $this->log(
59*dad993c5SAndreas Gohr            LogLevel::INFO,
60*dad993c5SAndreas Gohr            $message,
61*dad993c5SAndreas Gohr            $context
62*dad993c5SAndreas Gohr        );
63*dad993c5SAndreas Gohr    }
64*dad993c5SAndreas Gohr
65*dad993c5SAndreas Gohr    /**
66*dad993c5SAndreas Gohr     * Log a message with a level and context (if we have a logger).
67*dad993c5SAndreas Gohr     *
68*dad993c5SAndreas Gohr     * @param string $level
69*dad993c5SAndreas Gohr     * @param string $message
70*dad993c5SAndreas Gohr     * @param array $context
71*dad993c5SAndreas Gohr     */
72*dad993c5SAndreas Gohr    private function log(
73*dad993c5SAndreas Gohr        string $level,
74*dad993c5SAndreas Gohr        string $message,
75*dad993c5SAndreas Gohr        array $context = []
76*dad993c5SAndreas Gohr    ): void {
77*dad993c5SAndreas Gohr        if (isset($this->options['logger']) && $this->options['logger'] instanceof LoggerInterface) {
78*dad993c5SAndreas Gohr            $this->options['logger']->log(
79*dad993c5SAndreas Gohr                $level,
80*dad993c5SAndreas Gohr                $message,
81*dad993c5SAndreas Gohr                $context
82*dad993c5SAndreas Gohr            );
83*dad993c5SAndreas Gohr        }
84*dad993c5SAndreas Gohr    }
85*dad993c5SAndreas Gohr}
86