44
55namespace Tempest \Log ;
66
7- use Monolog \Level ;
7+ use Monolog \Level as MonologLogLevel ;
88use Monolog \Logger as Monolog ;
9+ use Psr \Log \LogLevel as PsrLogLevel ;
910use Stringable ;
11+ use Tempest \EventBus \EventBus ;
1012
1113final class GenericLogger implements Logger
1214{
@@ -15,64 +17,80 @@ final class GenericLogger implements Logger
1517
1618 public function __construct (
1719 private readonly LogConfig $ logConfig ,
20+ private readonly EventBus $ eventBus ,
1821 ) {
1922 }
2023
2124 public function emergency (Stringable |string $ message , array $ context = []): void
2225 {
23- $ this ->writeLog (Level::Emergency , $ message , $ context );
26+ $ this ->log (LogLevel:: EMERGENCY , $ message , $ context );
2427 }
2528
2629 public function alert (Stringable |string $ message , array $ context = []): void
2730 {
28- $ this ->writeLog (Level::Alert , $ message , $ context );
31+ $ this ->log (LogLevel:: ALERT , $ message , $ context );
2932 }
3033
3134 public function critical (Stringable |string $ message , array $ context = []): void
3235 {
33- $ this ->writeLog (Level::Critical , $ message , $ context );
36+ $ this ->log (LogLevel:: CRITICAL , $ message , $ context );
3437 }
3538
3639 public function error (Stringable |string $ message , array $ context = []): void
3740 {
38- $ this ->writeLog (Level::Error , $ message , $ context );
41+ $ this ->log (LogLevel:: ERROR , $ message , $ context );
3942 }
4043
4144 public function warning (Stringable |string $ message , array $ context = []): void
4245 {
43- $ this ->writeLog (Level::Warning , $ message , $ context );
46+ $ this ->log (LogLevel:: WARNING , $ message , $ context );
4447 }
4548
4649 public function notice (Stringable |string $ message , array $ context = []): void
4750 {
48- $ this ->writeLog (Level::Notice , $ message , $ context );
51+ $ this ->log (LogLevel:: NOTICE , $ message , $ context );
4952 }
5053
5154 public function info (Stringable |string $ message , array $ context = []): void
5255 {
53- $ this ->writeLog (Level::Info , $ message , $ context );
56+ $ this ->log (LogLevel:: INFO , $ message , $ context );
5457 }
5558
5659 public function debug (Stringable |string $ message , array $ context = []): void
5760 {
58- $ this ->writeLog (Level::Debug , $ message , $ context );
61+ $ this ->log (LogLevel:: DEBUG , $ message , $ context );
5962 }
6063
64+ /** @param MonologLogLevel|LogLevel|string $level */
6165 public function log ($ level , Stringable |string $ message , array $ context = []): void
6266 {
63- $ level = Level::tryFrom ($ level ) ?? Level::Info;
67+ if (! $ level instanceof MonologLogLevel) {
68+ $ level = match ($ level ) {
69+ LogLevel::EMERGENCY , PsrLogLevel::EMERGENCY => MonologLogLevel::Emergency,
70+ LogLevel::ALERT , PsrLogLevel::ALERT => MonologLogLevel::Alert,
71+ LogLevel::CRITICAL , PsrLogLevel::CRITICAL => MonologLogLevel::Critical,
72+ LogLevel::ERROR , PsrLogLevel::ERROR => MonologLogLevel::Error,
73+ LogLevel::WARNING , PsrLogLevel::WARNING => MonologLogLevel::Warning,
74+ LogLevel::NOTICE , PsrLogLevel::NOTICE => MonologLogLevel::Notice,
75+ LogLevel::INFO , PsrLogLevel::INFO => MonologLogLevel::Info,
76+ LogLevel::DEBUG , PsrLogLevel::DEBUG => MonologLogLevel::Debug,
77+ default => MonologLogLevel::Info
78+ };
79+ }
6480
6581 $ this ->writeLog ($ level , $ message , $ context );
82+
83+ $ this ->eventBus ->dispatch (new MessageLogged (LogLevel::fromMonolog ($ level ), $ message , $ context ));
6684 }
6785
68- private function writeLog (Level $ level , string $ message , array $ context ): void
86+ private function writeLog (MonologLogLevel $ level , string $ message , array $ context ): void
6987 {
7088 foreach ($ this ->logConfig ->channels as $ channel ) {
7189 $ this ->resolveDriver ($ channel , $ level )->log ($ level , $ message , $ context );
7290 }
7391 }
7492
75- private function resolveDriver (LogChannel $ channel , Level $ level ): Monolog
93+ private function resolveDriver (LogChannel $ channel , MonologLogLevel $ level ): Monolog
7694 {
7795 if (! isset ($ this ->drivers [spl_object_id ($ channel )])) {
7896 $ this ->drivers [spl_object_id ($ channel )] = new Monolog (
0 commit comments