Skip to content

Commit ea8f525

Browse files
committed
logger output customization, new util methods
1 parent 3a6e6bf commit ea8f525

File tree

4 files changed

+177
-69
lines changed

4 files changed

+177
-69
lines changed

src/Log/FileLogger.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ class FileLogger {
4545
public const ERROR = 3;
4646
/** @var int FATAL log level 4 */
4747
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;
4854
/** @var int $level */
4955
private static $level = 0;
5056
/** @var string $path */
@@ -53,6 +59,8 @@ class FileLogger {
5359
private static $EOL = "\n";
5460
/** @var bool $logEnabled */
5561
private static $logEnabled = true;
62+
/** @var int $mode */
63+
private static $mode = FileLogger::SIMPLE;
5664

5765
/**
5866
* Logger constructor prevents class instantiation
@@ -78,6 +86,21 @@ public static function setLogEnabled(bool $logEnabled): void {
7886
self::$logEnabled = $logEnabled;
7987
}
8088

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+
81104
/**
82105
* setting the log level. The level can be one of:
83106
*
@@ -117,7 +140,11 @@ public static function debug($message) {
117140
* @param int $level
118141
*/
119142
private static function log(string $message, int $level) {
143+
$description = "";
120144
$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+
121148
$logLevelDescription = FileLogger::getLogLevelDescription($level);
122149
$fileHandler = new FileHandler(FileLogger::getPath());
123150
$fileHandler->forceCreate();
@@ -129,8 +156,7 @@ private static function log(string $message, int $level) {
129156
$output .= " : ";
130157
$output .= $logLevelDescription;
131158
$output .= " : ";
132-
$output .= \json_encode($backTrace);
133-
$output .= " : ";
159+
$output .= $description;
134160
$output .= $message;
135161
$output .= self::$EOL;
136162
\file_put_contents(self::$path, $output, \FILE_APPEND);

src/Log/Logger.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,20 @@ class Logger {
4444
public const ERROR = 3;
4545
/** @var int FATAL log level 4 */
4646
public const FATAL = 4;
47+
/** @var int SIMPLE */
48+
public const SIMPLE = 0;
49+
/** @var int NORMAL */
50+
public const NORMAL = 1;
51+
/** @var int DESCRIPTIVE */
52+
public const DESCRIPTIVE = 2;
4753
/** @var int $level */
4854
private static $level = 0;
4955
/** @var string $EOL */
5056
private static $EOL = "\n";
5157
/** @var bool $logEnabled */
5258
private static $logEnabled = true;
59+
/** @var int $mode */
60+
private static $mode = Logger::SIMPLE;
5361

5462
/**
5563
* Logger constructor prevents class instantiation
@@ -99,6 +107,21 @@ public static function setLogLevel(int $level): void {
99107
}
100108
}
101109

110+
/**
111+
* @param int $mode
112+
* @return bool
113+
*/
114+
public static function setMode(int $mode): bool {
115+
if ($mode === Logger::SIMPLE
116+
|| $mode === Logger::NORMAL
117+
|| $mode === Logger::DESCRIPTIVE
118+
) {
119+
Logger::$mode = $mode;
120+
return true;
121+
}
122+
return false;
123+
}
124+
102125
/**
103126
* logs a message with log level DEBUG
104127
*
@@ -118,13 +141,17 @@ private static function log(string $message, int $level) {
118141
$backTrace = \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
119142
$logLevelDescription = Logger::getLogLevelDescription($level);
120143

144+
$description = "";
145+
$backTrace = \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
146+
if (Logger::$mode === Logger::NORMAL) $description = $backTrace[0]["file"] . " : ";
147+
if (Logger::$mode === Logger::DESCRIPTIVE) $description = \json_encode($backTrace) . " : ";;
148+
121149
if ($level >= self::$level && self::$logEnabled) {
122150
echo (new \DateTime())->format("Y-m-d H:i:s");
123151
echo " : ";
124152
echo $logLevelDescription;
125153
echo " : ";
126-
echo \json_encode($backTrace);
127-
echo " : ";
154+
echo $description;
128155
echo $message;
129156
echo self::$EOL;
130157
}

src/Util/NumberUtil.php

Lines changed: 73 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
<?php
22
/**
33
* MIT License
4-
*
54
* Copyright (c) 2018 Dogan Ucar, <dogan@dogan-ucar.de>
6-
*
75
* Permission is hereby granted, free of charge, to any person obtaining a copy
86
* of this software and associated documentation files (the "Software"), to deal
97
* in the Software without restriction, including without limitation the rights
108
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
119
* copies of the Software, and to permit persons to whom the Software is
1210
* furnished to do so, subject to the following conditions:
13-
*
1411
* The above copyright notice and this permission notice shall be included in all
1512
* copies or substantial portions of the Software.
16-
*
1713
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1814
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1915
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -24,42 +20,85 @@
2420
*/
2521

2622
namespace doganoo\PHPUtil\Util;
27-
2823
/**
2924
* Class NumberUtil
3025
*
3126
* @package doganoo\PHPUtil\Util
3227
*/
33-
final class NumberUtil {
34-
/**
35-
* prevent from instantiation
36-
* StringUtil constructor.
37-
*/
38-
private function __construct() {
39-
}
28+
final class NumberUtil{
29+
/**
30+
* prevent from instantiation
31+
* StringUtil constructor.
32+
*/
33+
private function __construct(){
34+
}
35+
36+
/**
37+
* returns the formatted number with grouped thousands
38+
*
39+
* @param $value
40+
* @param int $decimals
41+
*
42+
* @return null|string
43+
*/
44+
public static function format($value, int $decimals = 2): ?string{
45+
if(!\is_numeric($value)){
46+
return null;
47+
}
48+
return \number_format($value, $decimals);
49+
}
4050

41-
/**
42-
* returns the formatted number with grouped thousands
43-
*
44-
* @param $value
45-
* @param int $decimals
46-
* @return null|string
47-
*/
48-
public static function format($value, int $decimals = 2): ?string {
49-
if (!\is_numeric($value)) {
50-
return null;
51-
}
52-
return \number_format($value, $decimals);
53-
}
51+
/**
52+
* returns an array with the elements
53+
*
54+
* @param int $number
55+
*
56+
* @return array
57+
*/
58+
public static function intToArray(int $number): array{
59+
return StringUtil::stringToArray($number);
60+
}
5461

55-
/**
56-
* returns an array with the elements
57-
*
58-
* @param int $number
59-
* @return array
60-
*/
61-
public static function intToArray(int $number): array {
62-
return StringUtil::stringToArray($number);
63-
}
62+
/**
63+
* This method checks if $value is greater than $value1. If $gte is set to
64+
* true, the method checks if $value is greater than or equal to $value1.
65+
* From http://php.net/manual/de/language.types.float.php:
66+
* So never trust floating number results to the last digit, and do not
67+
* compare floating point numbers directly for equality.
68+
* Contributed notes in http://php.net/manual/de/language.types.float.php
69+
* suggests rounding the values before comparing (see 115 catalin dot luntraru at gmail dot com).
70+
*
71+
* @param float $value
72+
* @param float $value1
73+
* @param bool $gte
74+
*
75+
* @return bool
76+
* @since 1.0.0
77+
*/
78+
public static function floatGreaterThan(float $value, float $value1, bool $gte = false){
79+
$value = round($value, 10, PHP_ROUND_HALF_EVEN);
80+
$value1 = round($value1, 10, PHP_ROUND_HALF_EVEN);
81+
if($gte){
82+
return $value >= $value1;
83+
} else{
84+
return $value > $value1;
85+
}
86+
}
6487

88+
/**
89+
* This method compares two float numbers for equality. PHP float values
90+
* should never be compared directly, according to: http://php.net/manual/de/language.types.float.php
91+
*
92+
* @param float $value
93+
* @param float $value1
94+
*
95+
* @return bool
96+
*/
97+
public static function compareFloat(float $value, float $value1){
98+
$epsilon = 0.00001;
99+
if(abs($value - $value1) < $epsilon){
100+
return true;
101+
}
102+
return false;
103+
}
65104
}

src/Util/StringUtil.php

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
<?php
22
/**
33
* MIT License
4-
*
54
* Copyright (c) 2018 Dogan Ucar, <dogan@dogan-ucar.de>
6-
*
75
* Permission is hereby granted, free of charge, to any person obtaining a copy
86
* of this software and associated documentation files (the "Software"), to deal
97
* in the Software without restriction, including without limitation the rights
108
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
119
* copies of the Software, and to permit persons to whom the Software is
1210
* furnished to do so, subject to the following conditions:
13-
*
1411
* The above copyright notice and this permission notice shall be included in all
1512
* copies or substantial portions of the Software.
16-
*
1713
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1814
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1915
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -24,37 +20,57 @@
2420
*/
2521

2622
namespace doganoo\PHPUtil\Util;
27-
2823
/**
2924
* Class StringUtil
3025
*
3126
* @package doganoo\PHPUtil\Util
3227
*/
33-
final class StringUtil {
34-
/**
35-
* prevent from instantiation
36-
* StringUtil constructor.
37-
*/
38-
private function __construct() {
39-
}
28+
final class StringUtil{
29+
/**
30+
* prevent from instantiation
31+
* StringUtil constructor.
32+
*/
33+
private function __construct(){
34+
}
35+
36+
/**
37+
* returns an array of elements of the string
38+
*
39+
* @param null|string $string
40+
*
41+
* @return array
42+
*/
43+
public static function stringToArray(?string $string): array{
44+
$result = [];
45+
$strLen = \strlen($string);
46+
if(null === $string) return $result;
47+
if(1 === $strLen){
48+
$result[] = $string;
49+
return $result;
50+
}
51+
for($i = 0; $i < $strLen; $i ++){
52+
$result[] = $string[$i];
53+
}
54+
return $result;
55+
}
56+
57+
/**
58+
* @param string $string
59+
*
60+
* @return string
61+
*/
62+
public function toUTF8(string $string): string{
63+
$string = iconv('ASCII', 'UTF-8//IGNORE', $string);
64+
return $string;
65+
// return \mb_convert_encoding($string, "UTF-8");
66+
}
4067

41-
/**
42-
* returns an array of elements of the string
43-
*
44-
* @param null|string $string
45-
* @return array
46-
*/
47-
public static function stringToArray(?string $string): array {
48-
$result = [];
49-
$strLen = \strlen($string);
50-
if (null === $string) return $result;
51-
if (1 === $strLen) {
52-
$result[] = $string;
53-
return $result;
54-
}
55-
for ($i = 0; $i < $strLen; $i++) {
56-
$result[] = $string[$i];
57-
}
58-
return $result;
59-
}
68+
/**
69+
* @param string $string
70+
*
71+
* @return string
72+
*/
73+
public function getEncoding(string $string): string{
74+
return \mb_detect_encoding($string, "auto", true);
75+
}
6076
}

0 commit comments

Comments
 (0)