forked from michnhokn/kirby-logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.php
105 lines (83 loc) · 2.65 KB
/
Logger.php
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
<?php
namespace Michnhokn;
use Kirby\Data\Json;
use Kirby\Database\Database;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Filesystem\F;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Date;
use Kirby\Toolkit\Str;
class Logger
{
public const LEVEL_DEBUG = 'DEBUG';
public const LEVEL_INFO = 'INFO';
public const LEVEL_WARNING = 'WARNING';
public const LEVEL_ERROR = 'ERROR';
public const LEVEL_CRITICAL = 'CRITICAL';
private const LEVELS = [
self::LEVEL_DEBUG,
self::LEVEL_INFO,
self::LEVEL_WARNING,
self::LEVEL_ERROR,
self::LEVEL_CRITICAL
];
public const CHANNEL_DEFAULT = 'default';
public const CHANNEL_AUDIT = 'audit';
private const CHANNELS = [
self::CHANNEL_DEFAULT,
self::CHANNEL_AUDIT,
];
private static ?Database $connection = null;
public static function connect(): Database
{
if (static::$connection !== null) {
return static::$connection;
}
static::$connection = new Database([
'type' => 'sqlite',
'database' => kirby()->root('logs') . "/logger.sqlite"
]);
if (!self::$connection->validateTable('logs')) {
self::$connection->execute(F::read(__DIR__ . '/logs-schema.sql'));
}
return self::$connection;
}
public static function write(
string $message,
string $level = self::LEVEL_INFO,
string $channel = self::CHANNEL_DEFAULT,
array $context = [],
array $extra = [],
): void {
if (!in_array($channel, self::getChannels())) {
throw new \Exception("Logger channel $channel is not configured!");
}
(self::connect())->table('monolog')->insert([
'channel' => $channel,
'level' => $level,
'message' => $message,
'context' => Json::encode($context),
'extra' => Json::encode($extra),
'created_at' => Date::now()->format("Y-m-d\TH:i:s.uO"),
]);
}
public static function __callStatic(string $method, array $arguments)
{
$level = Str::upper($method);
if (in_array($level, static::LEVELS)) {
$arguments['level'] = $level;
self::write(...$arguments);
return;
}
throw new InvalidArgumentException('Invalid static Db method: ' . $method);
}
public static function getLevels(): array
{
return self::LEVELS;
}
public static function getChannels(): array
{
$channelsFromConfig = option('michnhokn.logger.channels', []);
return A::merge(self::CHANNELS, $channelsFromConfig);
}
}