Skip to content

Commit 4752be2

Browse files
committed
log migration to log4php
1 parent cf18ff7 commit 4752be2

File tree

2 files changed

+89
-168
lines changed

2 files changed

+89
-168
lines changed

src/Log/FileLogger.php

Lines changed: 54 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,15 @@
2525

2626
namespace doganoo\PHPUtil\Log;
2727

28-
use doganoo\PHPUtil\FileSystem\FileHandler;
29-
3028
/**
3129
* Class FileLogger
3230
*
3331
* TODO add other log levels
3432
*
3533
* @package doganoo\PHPUtil\Log
3634
*/
37-
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;
48-
/** @var int SIMPLE */
49-
public const SIMPLE = 0;
50-
/** @var int NORMAL */
51-
public const NORMAL = 1;
52-
/** @var int DESCRIPTIVE */
53-
public const DESCRIPTIVE = 2;
54-
/** @var int $level */
55-
private static $level = 0;
56-
/** @var string $path */
57-
private static $path = __DIR__ . "/logfile.log";
58-
/** @var string $EOL */
59-
private static $EOL = "\n";
60-
/** @var bool $logEnabled */
61-
private static $logEnabled = true;
62-
/** @var int $mode */
63-
private static $mode = FileLogger::SIMPLE;
35+
class FileLogger extends Logger {
36+
private static $path = null;
6437

6538
/**
6639
* Logger constructor prevents class instantiation
@@ -69,122 +42,36 @@ private function __construct() {
6942
}
7043

7144
/**
72-
* sets the end of line after a message is logged
73-
*
74-
* @param string $eol
75-
*/
76-
public static function setEOL(string $eol) {
77-
self::$EOL = $eol;
78-
}
79-
80-
/**
81-
* defines whether logging is enabled or not.
82-
*
83-
* @param bool $logEnabled
84-
*/
85-
public static function setLogEnabled(bool $logEnabled): void {
86-
self::$logEnabled = $logEnabled;
87-
}
88-
89-
/**
90-
* @param int $mode
91-
* @return bool
92-
*/
93-
public static function setMode(int $mode): bool {
94-
if ($mode === FileLogger::SIMPLE
95-
|| $mode === FileLogger::NORMAL
96-
|| $mode === FileLogger::DESCRIPTIVE
97-
) {
98-
FileLogger::$mode = $mode;
99-
return true;
100-
}
101-
return false;
102-
}
103-
104-
/**
105-
* setting the log level. The level can be one of:
106-
*
107-
* <ul>0 = DEBUG</ul>
108-
* <ul>1 = INFO</ul>
109-
* <ul>2 = WARN</ul>
110-
* <ul>3 = ERROR</ul>
111-
* <ul>4 = FATAL</ul>
112-
*
113-
* if $level does not match to any of these levels, the
114-
* log level will be initialized to ERROR (3).
115-
*
116-
* @param int $level
117-
*/
118-
public static function setLogLevel(int $level): void {
119-
if ($level >= FileLogger::DEBUG && $level <= FileLogger::FATAL) {
120-
self::$level = $level;
121-
} else {
122-
self::$level = FileLogger::ERROR;
123-
}
124-
125-
}
126-
127-
/**
128-
* logs a message with log level DEBUG
45+
* logs a message with log level INFO
12946
*
13047
* @param $message
13148
*/
132-
public static function debug($message) {
133-
self::log($message, FileLogger::DEBUG);
134-
}
135-
136-
/**
137-
* logs a message to the console
138-
*
139-
* @param string $message
140-
* @param int $level
141-
*/
142-
private static function log(string $message, int $level) {
143-
$description = "";
144-
$backTrace = \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
145-
if (FileLogger::$mode === FileLogger::NORMAL) $description = $backTrace[0]["file"] . " : ";
146-
if (FileLogger::$mode === FileLogger::DESCRIPTIVE) $description = \json_encode($backTrace) . " : ";;
147-
148-
$logLevelDescription = FileLogger::getLogLevelDescription($level);
149-
$fileHandler = new FileHandler(FileLogger::getPath());
150-
$fileHandler->forceCreate();
151-
$output = "";
152-
if ($level >= self::$level
153-
&& self::$logEnabled
154-
&& $fileHandler->isFile()) {
155-
$output .= (new \DateTime())->format("Y-m-d H:i:s");
156-
$output .= " : ";
157-
$output .= $logLevelDescription;
158-
$output .= " : ";
159-
$output .= $description;
160-
$output .= $message;
161-
$output .= self::$EOL;
162-
\file_put_contents(self::$path, $output, \FILE_APPEND);
163-
}
49+
public static function info($message) {
50+
parent::setConfig(FileLogger::getConfiguration());
51+
parent::info($message);
16452
}
16553

166-
/**
167-
* returns a string that describes the current log level
168-
*
169-
* @param int $level
170-
* @return string
171-
*/
172-
private static function getLogLevelDescription(int $level): string {
173-
if ($level === Logger::DEBUG) {
174-
return "debug";
175-
}
176-
if ($level === Logger::INFO) {
177-
return "info";
178-
}
179-
if ($level === Logger::WARN) {
180-
return "warn";
181-
}
182-
if ($level === Logger::ERROR) {
183-
return "error";
184-
}
185-
if ($level === Logger::FATAL) {
186-
return "fatal";
187-
}
54+
private static function getConfiguration(): array {
55+
return array(
56+
'appenders' => array(
57+
'default' => array(
58+
'class' => 'LoggerAppenderFile',
59+
'layout' => array(
60+
'class' => 'LoggerLayoutPattern',
61+
'params' => array(
62+
'conversionPattern' => '%date{Y-m-d H:i:s} %logger %-5level %msg%n'
63+
)
64+
),
65+
'params' => array(
66+
'file' => FileLogger::getPath(),
67+
'append' => true,
68+
),
69+
),
70+
),
71+
'rootLogger' => array(
72+
'appenders' => array('default'),
73+
),
74+
);
18875
}
18976

19077
/**
@@ -205,22 +92,14 @@ public static function setPath(string $path) {
20592
self::$path = $path;
20693
}
20794

208-
/**
209-
* logs a message with log level INFO
210-
*
211-
* @param $message
212-
*/
213-
public static function info($message) {
214-
self::log($message, FileLogger::INFO);
215-
}
216-
21795
/**
21896
* logs a message with log level WARN
21997
*
22098
* @param $message
22199
*/
222100
public static function warn($message) {
223-
self::log($message, FileLogger::WARN);
101+
parent::setConfig(FileLogger::getConfiguration());
102+
parent::warn($message);
224103
}
225104

226105
/**
@@ -229,7 +108,8 @@ public static function warn($message) {
229108
* @param $message
230109
*/
231110
public static function error($message) {
232-
self::log($message, FileLogger::ERROR);
111+
parent::setConfig(FileLogger::getConfiguration());
112+
parent::error($message);
233113
}
234114

235115
/**
@@ -238,7 +118,29 @@ public static function error($message) {
238118
* @param $message
239119
*/
240120
public static function fatal($message) {
241-
self::log($message, FileLogger::FATAL);
121+
parent::setConfig(FileLogger::getConfiguration());
122+
parent::fatal($message);
242123
}
243124

125+
/**
126+
* logs a message with log level TRACE
127+
*
128+
* @param $message
129+
*/
130+
public static function trace($message) {
131+
parent::setConfig(FileLogger::getConfiguration());
132+
parent::trace($message);
133+
}
134+
135+
/**
136+
* logs a message with log level DEBUG
137+
*
138+
* @param $message
139+
*/
140+
public static function debug($message) {
141+
parent::setConfig(FileLogger::getConfiguration());
142+
parent::debug($message);
143+
}
144+
145+
244146
}

src/Log/Logger.php

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,15 @@ class Logger {
5252
public const NORMAL = 1;
5353
/** @var int DESCRIPTIVE */
5454
public const DESCRIPTIVE = 2;
55+
public const ECHO = 0;
5556
/** @var int $level */
5657
private static $level = self::ERROR;
5758
/** @var string $EOL */
5859
private static $EOL = "\n";
5960
/** @var int $mode */
6061
private static $mode = Logger::SIMPLE;
62+
private static $config = null;
63+
6164

6265
/**
6366
* Logger constructor prevents class instantiation
@@ -132,22 +135,7 @@ public static function debug($message) {
132135
*/
133136
private static function log(string $message, int $level) {
134137
$logger = \Logger::getRootLogger();
135-
\Logger::configure(array(
136-
'appenders' => array(
137-
'default' => array(
138-
'class' => 'LoggerAppenderEcho',
139-
'layout' => array(
140-
'class' => 'LoggerLayoutPattern',
141-
'params' => array(
142-
'conversionPattern' => '%date{Y-m-d H:i:s} %logger %-5level %msg%n'
143-
)
144-
)
145-
)
146-
),
147-
'rootLogger' => array(
148-
'appenders' => array('default')
149-
),
150-
));
138+
\Logger::configure(self::getConfiguration());
151139

152140
switch ($level) {
153141
case Logger::DEBUG:
@@ -172,6 +160,28 @@ private static function log(string $message, int $level) {
172160
}
173161
}
174162

163+
private static function getConfiguration(): array {
164+
if (null === Logger::$config) return Logger::getDefaultConfiguration();
165+
return Logger::$config;
166+
}
167+
168+
private static function getDefaultConfiguration() {
169+
return array(
170+
'appenders' => array(
171+
'default' => array(
172+
'class' => 'LoggerAppenderEcho',
173+
'layout' => array(
174+
'class' => 'LoggerLayoutPattern',
175+
'params' => array(
176+
'conversionPattern' => '%date{Y-m-d H:i:s} %logger %-5level %msg%n'
177+
)
178+
)
179+
)
180+
),
181+
'rootLogger' => array(
182+
'appenders' => array('default')
183+
));
184+
}
175185

176186
/**
177187
* logs a message with log level INFO
@@ -217,4 +227,13 @@ public static function fatal($message) {
217227
public static function trace($message) {
218228
self::log($message, Logger::TRACE);
219229
}
230+
231+
/**
232+
* @param array $config
233+
* @return array
234+
*/
235+
public static function setConfig(array $config): array {
236+
return Logger::$config = $config;
237+
}
238+
220239
}

0 commit comments

Comments
 (0)