1111 * Class OneLog
1212 *
1313 * @author Denis-Florin Rendler <connect@rendler.me>
14- *
15- * @method emergency()
16- * @method static emergency()
17- * @method alert()
18- * @method static alert()
19- * @method critical()
20- * @method static critical()
21- * @method error()
22- * @method static error()
23- * @method warning()
24- * @method static warning()
25- * @method notice()
26- * @method static notice()
27- * @method info()
28- * @method static info()
29- * @method debug()
30- * @method static debug()
31- * @method log()
32- * @method static log()
33- *
34- * @property LoggerInterface $default
3514 */
3615class OneLog
3716{
38- public const DEFAULT_LOGGER = ' default ' ;
17+ use PSRLoggerTrait ;
3918
40- /**
41- * @var OneLog|null
42- */
43- private static $ resolved ;
19+ public const DEFAULT_LOGGER = 'default ' ;
4420
4521 /**
4622 * @var LoggerInterface|NullLogger
@@ -67,65 +43,6 @@ public function __construct(LoggerInterface $default = null, LoggerInterface ...
6743 foreach ($ logger as $ loggerInstance ) {
6844 $ this ->registerLogger ($ loggerInstance );
6945 }
70-
71- if (self ::$ resolved !== $ this ) {
72- self ::$ resolved = null ;
73- self ::$ resolved = $ this ;
74- }
75- }
76-
77- /**
78- * @example OneLog::debug(<string>'message', <array>context)
79- *
80- * @param string $level
81- * @param mixed ...$params
82- *
83- * @return mixed
84- */
85- public static function __callStatic (string $ level , $ params )
86- {
87- if (!static ::$ resolved instanceof self) {
88- throw new \RuntimeException ('Logger is not properly instantiated! ' );
89- }
90-
91- return self ::$ resolved ->__call ($ level , $ params );
92- }
93-
94- /**
95- * Returns the OneLog instance
96- *
97- * @return OneLog
98- */
99- public static function instance (): OneLog
100- {
101- if (self ::$ resolved ) {
102- return self ::$ resolved ;
103- }
104-
105- throw new \RuntimeException ('OneLog is not properly instantiated! ' );
106- }
107-
108- /**
109- * Make sure we clear the static instance as well
110- */
111- public function __destruct ()
112- {
113- self ::$ resolved = null ;
114- }
115-
116- /**
117- * Proxy for logger methods on default logger instance
118- *
119- * @example $instance->debug(<string>'message', <array>context)
120- *
121- * @param string $level
122- * @param array $params
123- *
124- * @return bool
125- */
126- public function __call (string $ level , array $ params ): bool
127- {
128- return $ this ->defaultLogger ->{$ level }(...$ params );
12946 }
13047
13148 /**
@@ -164,11 +81,11 @@ public function loggers(): array
16481 public function registerLogger (LoggerInterface $ logger , $ name = null ): void
16582 {
16683 $ loggerName = $ name ?? spl_object_hash ($ logger );
167-
84+
16885 if (null === $ name && ($ logger instanceof Logger || $ logger instanceof NamedLoggerInterface)) {
16986 $ loggerName = $ logger ->getName ();
17087 }
171-
88+
17289 $ this ->loggers [$ loggerName ] = $ logger ;
17390 }
17491}
0 commit comments