1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Log class (Static Wrapper for LogPsr3) |
||
5 | * |
||
6 | * Provides a static interface for the PSR-3 compliant LogPsr3 class. |
||
7 | * |
||
8 | * This class allows developers to use logging functionality via static |
||
9 | * methods while maintaining the flexibility of the LogPsr3 instance-based design. |
||
10 | * |
||
11 | * @method static void write(string $message) |
||
12 | * @category Utility |
||
13 | * @package jidaikobo/log |
||
14 | * @author jidaikobo-shibata <[email protected]> |
||
15 | * @license MIT License <https://opensource.org/licenses/MIT> |
||
16 | * @link https://github.com/jidaikobo-shibata/log |
||
17 | */ |
||
18 | |||
19 | namespace Jidaikobo; |
||
20 | |||
21 | use Jidaikobo\LogPsr3; |
||
22 | |||
23 | class Log |
||
24 | { |
||
25 | private static ?LogPsr3 $instance = null; |
||
26 | |||
27 | /** |
||
28 | * Initialize the Logger instance. |
||
29 | * |
||
30 | * @param string $logFile path of log file |
||
31 | * @param int $maxFileSize log rotation size |
||
32 | * |
||
33 | * @return void |
||
34 | */ |
||
35 | public static function init(string $logFile = null, int $maxFileSize = 10 * 1024 * 1024): void |
||
36 | { |
||
37 | if ($logFile === null) { |
||
38 | $logFile = dirname(__DIR__, 4) . '/logs/php.log'; // default project root |
||
39 | } |
||
40 | |||
41 | $logDir = dirname($logFile); |
||
42 | if (!is_dir($logDir)) { |
||
43 | mkdir($logDir, 0777, true); |
||
44 | } |
||
45 | |||
46 | self::$instance = new LogPsr3($logFile, $maxFileSize); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Get the Logger instance. |
||
51 | * |
||
52 | * @return LogPsr3 |
||
53 | * @throws \RuntimeException if the instance is not initialized. |
||
54 | */ |
||
55 | public static function getInstance(): LogPsr3 |
||
56 | { |
||
57 | if (!self::$instance) { |
||
58 | self::init(); |
||
59 | |||
60 | if (!self::$instance) { |
||
61 | throw new \RuntimeException('Logger instance is not initialized.'); |
||
62 | } |
||
63 | } |
||
64 | |||
65 | return self::$instance; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Forward static calls to the Logger instance. |
||
70 | * |
||
71 | * @param string $method called method name |
||
72 | * @param array<int, mixed> $args arguments |
||
73 | * |
||
74 | * @return mixed |
||
75 | */ |
||
76 | public static function __callStatic(string $method, array $args) |
||
77 | { |
||
78 | $instance = self::getInstance(); |
||
79 | |||
80 | if (!method_exists($instance, $method)) { |
||
81 | $availableMethods = implode(', ', get_class_methods($instance)); |
||
82 | $message = sprintf( |
||
83 | "Method '%s' does not exist on LogPsr3. Available methods: %s", |
||
84 | $method, |
||
85 | $availableMethods |
||
86 | ); |
||
87 | throw new \BadMethodCallException($message); |
||
88 | } |
||
89 | |||
90 | return $instance->$method(...$args); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Add static method for phpstan |
||
95 | * |
||
96 | * @param mixed $message The message to log. |
||
97 | * @param string $level The log level. |
||
98 | * |
||
99 | * @return void |
||
100 | */ |
||
101 | public static function write(mixed $message, string $level = 'INFO'): void |
||
102 | { |
||
103 | self::getInstance()->write($message, $level); |
||
104 | } |
||
105 | } |
||
106 |