1: <?php
2: /*
3: * SimpleID
4: *
5: * Copyright (C) Kelvin Mo 2014-2025
6: *
7: * This program is free software; you can redistribute it and/or
8: * modify it under the terms of the GNU General Public
9: * License as published by the Free Software Foundation; either
10: * version 2 of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program; if not, write to the Free
19: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20: */
21:
22: namespace SimpleID\Util;
23:
24: use \Log;
25: use \Psr\Log\LoggerInterface;
26: use \Psr\Log\LogLevel;
27: use \Psr\Log\LoggerTrait;
28:
29: /**
30: * The default SimpleID logger.
31: *
32: * This logger extends the logger included in the Fat-Free framework
33: * by implementing the PSR-3 specification. In addition, it retains the
34: * same log file format as per SimpleID 1.
35: *
36: * You can replace this logger with any PSR-3 compliant logger.
37: *
38: * @see https://fatfreeframework.com/3.8/log
39: *
40: */
41: class DefaultLogger extends Log implements LoggerInterface {
42:
43: use LoggerTrait;
44:
45: /** @var string */
46: protected $log_level;
47:
48: /** @var array<string, int> */
49: protected static $log_levels = [
50: LogLevel::EMERGENCY => 0,
51: LogLevel::ALERT => 1,
52: LogLevel::CRITICAL => 2,
53: LogLevel::ERROR => 3,
54: LogLevel::WARNING => 4,
55: LogLevel::NOTICE => 5,
56: LogLevel::INFO => 6,
57: LogLevel::DEBUG => 7,
58: ];
59:
60: /**
61: * Creates a logger based on the Fat-Framework. The log file
62: * will be placed under the directory specified by the Fat-Free
63: * LOGS variable
64: *
65: * @param array<string, mixed> $config the SimpleID configuration
66: */
67: function __construct($config) {
68: parent::__construct(basename($config['log_file']));
69:
70: $this->log_level = $config['log_level'];
71: }
72:
73: /**
74: * Logs a message with an INFO log level. Direct resplacement
75: * of the parent log function
76: *
77: * @param string $text the message to log
78: * @param string $format ignored.
79: */
80: function write($text, $format = 'r') {
81: $this->log(LogLevel::INFO, $text);
82: return '';
83: }
84:
85: /**
86: * Logs a message
87: *
88: * @param string $level the log level
89: * @param string $message the message to log
90: * @param array<string, mixed> $context the context
91: */
92: function log($level, $message, array $context = []) {
93: $fw = \Base::instance();
94: $config = $fw->get('config');
95: $time = new \DateTimeImmutable();
96:
97: if (self::$log_levels[$level] > self::$log_levels[$config['log_level']]) return;
98:
99: if (count($context) > 0) $message .= ' ' . json_encode($context);
100:
101: $line = sprintf('%1$s %2$s [%3$s] %4$s', $time->format($config['date_time_format']), session_id(), $level, $message) . "\n";
102:
103: $fw->write($this->file, $line, true);
104: }
105: }
106:
107: ?>
108: