Skip to content

Commit 59354b8

Browse files
committed
new log format and little bug fixes
1 parent d068d67 commit 59354b8

File tree

4 files changed

+118
-20
lines changed

4 files changed

+118
-20
lines changed

src/Log/FileLogger.php

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,24 @@
3535
* @package doganoo\PHPUtil\Log
3636
*/
3737
class FileLogger {
38+
/** @var int DEBUG log level 0 */
39+
public const DEBUG = 0;
40+
/** @var int INFO log level 1 */
41+
public const INFO = 1;
42+
/** @var int WARN log level 2 */
43+
public const WARN = 2;
44+
/** @var int ERROR log level 3 */
45+
public const ERROR = 3;
46+
/** @var int FATAL log level 4 */
47+
public const FATAL = 4;
3848
/** @var int $level */
3949
private static $level = 0;
4050
/** @var string $path */
4151
private static $path = __DIR__ . "/logfile.log";
4252
/** @var string $EOL */
4353
private static $EOL = "\n";
54+
/** @var bool $logEnabled */
55+
private static $logEnabled = true;
4456

4557
/**
4658
* Logger constructor prevents class instantiation
@@ -57,6 +69,15 @@ public static function setEOL(string $eol) {
5769
self::$EOL = $eol;
5870
}
5971

72+
/**
73+
* defines whether logging is enabled or not.
74+
*
75+
* @param bool $logEnabled
76+
*/
77+
public static function setLogEnabled(bool $logEnabled): void {
78+
self::$logEnabled = $logEnabled;
79+
}
80+
6081
/**
6182
* setting the log level. The level can be one of:
6283
*
@@ -72,10 +93,10 @@ public static function setEOL(string $eol) {
7293
* @param int $level
7394
*/
7495
public static function setLogLevel(int $level): void {
75-
if ($level >= 0 && $level <= 4) {
96+
if ($level >= FileLogger::DEBUG && $level <= FileLogger::FATAL) {
7697
self::$level = $level;
7798
} else {
78-
self::$level = 3;
99+
self::$level = FileLogger::ERROR;
79100
}
80101

81102
}
@@ -86,7 +107,7 @@ public static function setLogLevel(int $level): void {
86107
* @param $message
87108
*/
88109
public static function debug($message) {
89-
self::log($message, 0);
110+
self::log($message, FileLogger::DEBUG);
90111
}
91112

92113
/**
@@ -96,18 +117,50 @@ public static function debug($message) {
96117
* @param int $level
97118
*/
98119
private static function log(string $message, int $level) {
99-
$fileHandler = new FileHandler(self::getPath());
120+
$backTrace = \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
121+
$logLevelDescription = FileLogger::getLogLevelDescription($level);
122+
$fileHandler = new FileHandler(FileLogger::getPath());
100123
$fileHandler->forceCreate();
101124
$output = "";
102-
if ($level >= self::$level) {
125+
if ($level >= self::$level
126+
&& self::$logEnabled
127+
&& $fileHandler->isFile()) {
103128
$output .= (new \DateTime())->format("Y-m-d H:i:s");
104129
$output .= " : ";
130+
$output .= $logLevelDescription;
131+
$output .= " : ";
132+
$output .= \json_encode($backTrace);
133+
$output .= " : ";
105134
$output .= $message;
106135
$output .= self::$EOL;
107136
\file_put_contents(self::$path, $output, \FILE_APPEND);
108137
}
109138
}
110139

140+
/**
141+
* returns a string that describes the current log level
142+
*
143+
* @param int $level
144+
* @return string
145+
*/
146+
private static function getLogLevelDescription(int $level): string {
147+
if ($level === Logger::DEBUG) {
148+
return "debug";
149+
}
150+
if ($level === Logger::INFO) {
151+
return "info";
152+
}
153+
if ($level === Logger::WARN) {
154+
return "warn";
155+
}
156+
if ($level === Logger::ERROR) {
157+
return "error";
158+
}
159+
if ($level === Logger::FATAL) {
160+
return "fatal";
161+
}
162+
}
163+
111164
/**
112165
* returns the log path
113166
*
@@ -132,7 +185,7 @@ public static function setPath(string $path) {
132185
* @param $message
133186
*/
134187
public static function info($message) {
135-
self::log($message, 1);
188+
self::log($message, FileLogger::INFO);
136189
}
137190

138191
/**
@@ -141,7 +194,7 @@ public static function info($message) {
141194
* @param $message
142195
*/
143196
public static function warn($message) {
144-
self::log($message, 2);
197+
self::log($message, FileLogger::WARN);
145198
}
146199

147200
/**
@@ -150,7 +203,7 @@ public static function warn($message) {
150203
* @param $message
151204
*/
152205
public static function error($message) {
153-
self::log($message, 3);
206+
self::log($message, FileLogger::ERROR);
154207
}
155208

156209
/**
@@ -159,7 +212,7 @@ public static function error($message) {
159212
* @param $message
160213
*/
161214
public static function fatal($message) {
162-
self::log($message, 4);
215+
self::log($message, FileLogger::FATAL);
163216
}
164217

165218
}

src/Log/Logger.php

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,21 @@
2929
* Class
3030
*
3131
* TODO enable custom log format
32+
* TODO elaborate for abstract static classes/abstract classes with static variables/methods
3233
*
3334
* @package doganoo\PHPUtil\Log
3435
*/
3536
class Logger {
37+
/** @var int DEBUG log level 0 */
38+
public const DEBUG = 0;
39+
/** @var int INFO log level 1 */
40+
public const INFO = 1;
41+
/** @var int WARN log level 2 */
42+
public const WARN = 2;
43+
/** @var int ERROR log level 3 */
44+
public const ERROR = 3;
45+
/** @var int FATAL log level 4 */
46+
public const FATAL = 4;
3647
/** @var int $level */
3748
private static $level = 0;
3849
/** @var string $EOL */
@@ -81,12 +92,11 @@ public static function setLogEnabled(bool $logEnabled): void {
8192
* @param int $level
8293
*/
8394
public static function setLogLevel(int $level): void {
84-
if ($level >= 0 && $level <= 4) {
95+
if ($level >= Logger::DEBUG && $level <= Logger::FATAL) {
8596
self::$level = $level;
8697
} else {
87-
self::$level = 3;
98+
self::$level = Logger::ERROR;
8899
}
89-
90100
}
91101

92102
/**
@@ -95,7 +105,7 @@ public static function setLogLevel(int $level): void {
95105
* @param $message
96106
*/
97107
public static function debug($message) {
98-
self::log($message, 0);
108+
self::log($message, Logger::DEBUG);
99109
}
100110

101111
/**
@@ -105,21 +115,52 @@ public static function debug($message) {
105115
* @param int $level
106116
*/
107117
private static function log(string $message, int $level) {
118+
$backTrace = \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
119+
$logLevelDescription = Logger::getLogLevelDescription($level);
120+
108121
if ($level >= self::$level && self::$logEnabled) {
109122
echo (new \DateTime())->format("Y-m-d H:i:s");
110123
echo " : ";
124+
echo $logLevelDescription;
125+
echo " : ";
126+
echo \json_encode($backTrace);
127+
echo " : ";
111128
echo $message;
112129
echo self::$EOL;
113130
}
114131
}
115132

133+
/**
134+
* returns a string that describes the current log level
135+
*
136+
* @param int $level
137+
* @return string
138+
*/
139+
private static function getLogLevelDescription(int $level): string {
140+
if ($level === Logger::DEBUG) {
141+
return "debug";
142+
}
143+
if ($level === Logger::INFO) {
144+
return "info";
145+
}
146+
if ($level === Logger::WARN) {
147+
return "warn";
148+
}
149+
if ($level === Logger::ERROR) {
150+
return "error";
151+
}
152+
if ($level === Logger::FATAL) {
153+
return "fatal";
154+
}
155+
}
156+
116157
/**
117158
* logs a message with log level INFO
118159
*
119160
* @param $message
120161
*/
121162
public static function info($message) {
122-
self::log($message, 1);
163+
self::log($message, Logger::INFO);
123164
}
124165

125166
/**
@@ -128,7 +169,7 @@ public static function info($message) {
128169
* @param $message
129170
*/
130171
public static function warn($message) {
131-
self::log($message, 2);
172+
self::log($message, Logger::WARN);
132173
}
133174

134175
/**
@@ -137,7 +178,7 @@ public static function warn($message) {
137178
* @param $message
138179
*/
139180
public static function error($message) {
140-
self::log($message, 3);
181+
self::log($message, Logger::ERROR);
141182
}
142183

143184
/**
@@ -146,7 +187,6 @@ public static function error($message) {
146187
* @param $message
147188
*/
148189
public static function fatal($message) {
149-
self::log($message, 4);
190+
self::log($message, Logger::FATAL);
150191
}
151-
152192
}

src/Util/AppContainer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
namespace doganoo\PHPUtil\Util;
2727

2828

29-
use doganoo\PHPAlgorithms\Datastructure\lists\Node;
29+
use doganoo\PHPAlgorithms\Datastructure\Lists\Node;
3030
use doganoo\PHPAlgorithms\Datastructure\Maps\HashMap;
3131

3232
/**
@@ -52,6 +52,7 @@ private function __construct() {
5252
* @param callable $callable
5353
* @throws \doganoo\PHPAlgorithms\common\Exception\InvalidKeyTypeException
5454
* @throws \doganoo\PHPAlgorithms\common\Exception\UnsupportedKeyTypeException
55+
* @throws \ReflectionException
5556
*/
5657
public static function add(string $name, callable $callable) {
5758
$map = self::getInstance();

src/Util/ArrayUtil.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ private function __construct() {
4747
public static function arrayToString(array $array, $delimiter = ""): string {
4848
$string = "";
4949
foreach ($array as $value) {
50-
$string .= $value . $delimiter;
50+
if (\is_array($value)) {
51+
$string .= ArrayUtil::arrayToString($value, $delimiter) . $delimiter;
52+
} else {
53+
$string .= $value . $delimiter;
54+
}
5155
}
5256
return $string;
5357
}

0 commit comments

Comments
 (0)