1818
1919use iTXTech \SimpleFramework \Util \Util ;
2020
21- class Logger{
22- const EMERGENCY = "emergency " ;
23- const ALERT = "alert " ;
24- const CRITICAL = "critical " ;
25- const ERROR = "error " ;
26- const WARNING = "warning " ;
27- const NOTICE = "notice " ;
28- const INFO = "info " ;
29- const DEBUG = "debug " ;
30-
21+ abstract class Logger extends LoggerHandler{
22+ public const ERROR = 3 ;
23+ public const WARNING = 2 ;
24+ public const INFO = 1 ;
25+ public const DEBUG = 0 ;
26+
27+ private const PREFIX = [
28+ self ::DEBUG => "DEBUG " ,
29+ self ::INFO => "INFO " ,
30+ self ::WARNING => "WARNING " ,
31+ self ::ERROR => "ERROR "
32+ ];
33+
34+ public static $ logLevel = self ::INFO ;
3135 public static $ hasPrefix = true ;
3236 public static $ disableOutput = false ;
3337 public static $ disableClass = false ;
3438
3539 private static $ logfile = "" ;
3640 /** @var LoggerHandler */
37- private static $ loggerHandler = "" ;
41+ private static $ loggerHandler = Logger::class ;
3842
3943 public static function setLoggerHandler (string $ class ) : bool {
4044 if (is_a ($ class , LoggerHandler::class, true )){
@@ -51,36 +55,20 @@ public static function setLogFile(string $logfile){
5155 self ::$ logfile = $ logfile ;
5256 }
5357
54- public static function emergency ($ message , $ name = "EMERGENCY " ){
55- self ::send ($ message , $ name , TextFormat::RED );
56- }
57-
58- public static function alert ($ message , $ name = "ALERT " ){
59- self ::send ($ message , $ name , TextFormat::RED );
60- }
61-
62- public static function critical ($ message , $ name = "CRITICAL " ){
63- self ::send ($ message , $ name , TextFormat::RED );
64- }
65-
66- public static function error ($ message , $ name = "ERROR " ){
67- self ::send ($ message , $ name , TextFormat::DARK_RED );
68- }
69-
70- public static function warning ($ message , $ name = "WARNING " ){
71- self ::send ($ message , $ name , TextFormat::YELLOW );
58+ public static function error (string $ message ){
59+ self ::send ($ message , self ::ERROR , TextFormat::DARK_RED );
7260 }
7361
74- public static function notice ( $ message , $ name = " NOTICE " ){
75- self ::send ($ message , $ name , TextFormat::AQUA );
62+ public static function warning ( string $ message ){
63+ self ::send ($ message , self :: WARNING , TextFormat::YELLOW );
7664 }
7765
78- public static function info ($ message , $ name = " INFO " ){
79- self ::send ($ message , $ name , TextFormat::WHITE );
66+ public static function info (string $ message ){
67+ self ::send ($ message , self :: INFO , TextFormat::WHITE );
8068 }
8169
82- public static function debug ($ message , $ name = " DEBUG " ){
83- self ::send ($ message , $ name , TextFormat::GRAY );
70+ public static function debug (string $ message ){
71+ self ::send ($ message , self :: DEBUG , TextFormat::GRAY );
8472 }
8573
8674 public static function logException (\Throwable $ e ){
@@ -109,9 +97,9 @@ public static function logException(\Throwable $e){
10997 E_USER_DEPRECATED => "E_USER_DEPRECATED " ,
11098 ];
11199 if ($ errno === 0 ){
112- $ type = self ::CRITICAL ;
100+ $ type = self ::ERROR ;
113101 }else {
114- $ type = ($ errno === E_ERROR or $ errno === E_USER_ERROR ) ? self ::ERROR : (( $ errno === E_USER_WARNING or $ errno === E_WARNING ) ? self ::WARNING : self :: NOTICE ) ;
102+ $ type = ($ errno === E_ERROR or $ errno === E_USER_ERROR ) ? self ::ERROR : self ::WARNING ;
115103 }
116104 $ errno = isset ($ errorConversion [$ errno ]) ? $ errorConversion [$ errno ] : $ errno ;
117105 if (($ pos = strpos ($ errstr , "\n" )) !== false ){
@@ -125,24 +113,12 @@ public static function logException(\Throwable $e){
125113
126114 public static function log ($ level , $ message ){
127115 switch ($ level ){
128- case self ::EMERGENCY :
129- self ::emergency ($ message );
130- break ;
131- case self ::ALERT :
132- self ::alert ($ message );
133- break ;
134- case self ::CRITICAL :
135- self ::critical ($ message );
136- break ;
137116 case self ::ERROR :
138117 self ::error ($ message );
139118 break ;
140119 case self ::WARNING :
141120 self ::warning ($ message );
142121 break ;
143- case self ::NOTICE :
144- self ::notice ($ message );
145- break ;
146122 case self ::INFO :
147123 self ::info ($ message );
148124 break ;
@@ -152,12 +128,11 @@ public static function log($level, $message){
152128 }
153129 }
154130
155- public static function send (string $ message , string $ prefix , string $ color ){
131+ public static function send (string $ message , int $ level , string $ color ){
156132 if (self ::$ disableOutput ){
157133 return ;
158134 }
159- if (self ::$ loggerHandler !== "" ){
160- self ::$ loggerHandler ::send ($ message , $ prefix , $ color );
135+ if ($ level < self ::$ logLevel ){
161136 return ;
162137 }
163138 if (self ::$ hasPrefix ){
@@ -171,10 +146,16 @@ public static function send(string $message, string $prefix, string $color){
171146 $ class = $ class == "" ? "Console " : $ class ;
172147 }
173148 $ message = TextFormat::toANSI (TextFormat::AQUA . "[ " . date ("G:i:s " , $ now ) . "] " .
174- TextFormat::RESET . $ color . "< " . $ class . "/ " . $ prefix . "> " . " " . $ message . TextFormat::RESET );
149+ TextFormat::RESET . $ color . "< " . $ class . "/ " . self ::PREFIX [$ level ] . "> " . " " .
150+ $ message . TextFormat::RESET );
175151 }else {
176152 $ message = TextFormat::toANSI ($ message . TextFormat::RESET );
177153 }
154+
155+ self ::$ loggerHandler ::println ($ message );
156+ }
157+
158+ public static function println (string $ message ){
178159 $ cleanMessage = TextFormat::clean ($ message );
179160
180161 if (!Terminal::hasFormattingCodes ()){
0 commit comments