summaryrefslogtreecommitdiff
path: root/src/Loggr.php
blob: 6727bb1af57e2224430627555cab804e1ca9631d (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
<?php

namespace Asko\Loggr;

/**
 * Loggr is an extendable logging utility class brought to you by the frustration of
 * every logging class always having its own unique format, making debugging difficult.
 *
 * Instead of having its own yet-another-format that no tool supports, Loggr attempts to match
 * many already existing formats, allowing you to use whichever you prefer most.
 *
 * @author Asko Nõmm <asko@faultd.com>
 */
class Loggr
{
    private array $trace;

    /**
     * Since it would suck if a logging utility throws an exception,
     * Loggr instead of throwing will simply set the error here, in case
     * logging doesn't work, and you might want to find out why.
     *
     * @var string|null $error
     */
    public ?string $error = null;

    public function __construct(
        public ?Driver $driver = null,
        public ?Format $format = Format::Laravel,
    ){}

    /**
     * Logs a message at a specified level with optional context.
     *
     * @param Level $level The severity level of the log message. Defaults to Level::Info.
     * @param mixed $context Additional data or context to include with the log message. Optional.
     * @return void
     */
    private function log(Level $level = Level::Info, mixed $context = null): void
    {
        if (!$this->driver || !$this->format) {
            $this->error = "Driver or format not set.";
            return;
        }

        $this->driver->log($this->format->serialize(new Message(
            level: $level,
            trace: $this->trace,
            context: $context,
        )));
    }

    /**
     * Logs an emergency level message with optional context.
     *
     * @param mixed $context Additional data or context to include with the emergency message. Optional.
     * @return void
     */
    public function emergency(mixed $context = null): void
    {
        $this->trace = debug_backtrace()[0];
        $this->log(Level::Emergency, $context);
    }

    /**
     * Sends an alert-level log message with optional context.
     *
     * @param mixed $context Additional data or context to include with the log message. Optional.
     * @return void
     */
    public function alert(mixed $context = null): void
    {
        $this->trace = debug_backtrace()[0];
        $this->log(Level::Alert, $context);
    }

    /**
     * Logs a critical level message with optional context.
     *
     * @param mixed $context Additional data or context to include with the log message. Optional.
     * @return void
     */
    public function critical( mixed $context = null): void
    {
        $this->trace = debug_backtrace()[0];
        $this->log(Level::Critical, $context);
    }

    /**
     * Logs an error message with optional context.
     *
     * @param mixed $context Additional data or context to include with the error message. Optional.
     * @return void
     */
    public function error(mixed $context = null): void
    {
        $this->trace = debug_backtrace()[0];
        $this->log(Level::Error, $context);
    }

    /**
     * Logs a warning message with the specified context.
     *
     * @param mixed $context Optional context information to include in the log.
     * @return void
     */
    public function warning(mixed $context = null): void
    {
        $this->trace = debug_backtrace()[0];
        $this->log(Level::Warning, $context);
    }

    /**
     * Logs a notice message with the specified context.
     *
     * @param mixed $context Optional context information to include in the log.
     * @return void
     */
    public function notice(mixed $context = null): void
    {
        $this->trace = debug_backtrace()[0];
        $this->log(Level::Notice, $context);
    }

    /**
     * Logs an informational message with the specified context.
     *
     * @param mixed $context Optional context information to include in the log.
     * @return void
     */
    public function info(mixed $context = null): void
    {
        $this->trace = debug_backtrace()[0];
        $this->log(Level::Info, $context);
    }

    /**
     * Logs a debug message with the specified context.
     *
     * @param mixed $context Optional context information to include in the log.
     * @return void
     */
    public function debug(mixed $context = null): void
    {
        $this->trace = debug_backtrace()[0];
        $this->log(Level::Debug, $context);
    }
}