Skip to content

Commit 5e1ab79

Browse files
committed
- minimum PHP version (8.1)
1 parent deb180c commit 5e1ab79

File tree

8 files changed

+52
-31
lines changed

8 files changed

+52
-31
lines changed

Log.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@
1515
use Koded\Logging\Processors\{Cli, Processor};
1616
use DateTimeZone;
1717
use Psr\Log\LoggerTrait;
18+
use Stringable;
1819
use Throwable;
20+
use function constant;
21+
use function date_create_immutable;
22+
use function spl_object_id;
23+
use function strtoupper;
24+
use function strtr;
25+
use function timezone_open;
1926

2027
/**
2128
* Class Log for logging different types of application messages.
@@ -78,17 +85,17 @@ public function __construct(
7885
private string $dateformat = self::DATE_FORMAT,
7986
string $timezone = 'UTC')
8087
{
81-
$this->timezone = @\timezone_open($timezone) ?: \timezone_open('UTC');
88+
$this->timezone = @timezone_open($timezone) ?: timezone_open('UTC');
8289
foreach ($processors as $processor) {
8390
$this->attach(new $processor['class']($processor));
8491
}
8592
}
8693

87-
public function log($level, $message, array $context = []): void
94+
public function log($level, string|Stringable $message, array $context = []): void
8895
{
8996
try {
90-
$levelName = \strtoupper($level);
91-
$level = \constant('static::' . $levelName);
97+
$levelName = strtoupper($level);
98+
$level = constant('static::' . $levelName);
9299
} catch (Throwable) {
93100
$levelName = 'LOG';
94101
$level = -1;
@@ -106,21 +113,21 @@ public function log($level, $message, array $context = []): void
106113
public function attach(Processor $processor): Logger
107114
{
108115
if (0 !== $processor->levels()) {
109-
$this->processors[\spl_object_id($processor)] = $processor;
116+
$this->processors[spl_object_id($processor)] = $processor;
110117
}
111118
return $this;
112119
}
113120

114121
public function detach(Processor $processor): Logger
115122
{
116-
unset($this->processors[\spl_object_id($processor)]);
123+
unset($this->processors[spl_object_id($processor)]);
117124
return $this;
118125
}
119126

120127
public function exception(Throwable $e, Processor $processor = null): void
121128
{
122129
$this->attach($processor ??= new Cli([]));
123-
$this->critical($e->getMessage() . PHP_EOL . ' -- [Trace]: ' . $e->getTraceAsString());
130+
$this->critical($e->getMessage() . PHP_EOL . ' [Trace]: ' . $e->getTraceAsString());
124131
$this->detach($processor);
125132
}
126133

@@ -138,12 +145,12 @@ private function formatMessage(object|string $message, array $params = []): stri
138145
foreach ($params as $k => $v) {
139146
$replacements['{' . $k . '}'] = $v;
140147
}
141-
return \strtr((string)$message, $replacements);
148+
return strtr((string)$message, $replacements);
142149
}
143150

144151
private function now(): string
145152
{
146-
return \date_create_immutable('now', $this->timezone)
153+
return date_create_immutable('now', $this->timezone)
147154
->format($this->dateformat);
148155
}
149156
}

Processors/Cli.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212

1313
namespace Koded\Logging\Processors;
1414

15+
use function defined;
16+
use function fopen;
17+
use function fwrite;
18+
use function strtr;
19+
1520
/**
1621
* Log processor for CLI.
1722
*
@@ -26,11 +31,11 @@ class Cli extends Processor
2631
public function __construct(array $settings)
2732
{
2833
parent::__construct($settings);
29-
$this->handle = \defined('STDOUT') ? STDOUT : \fopen('php://stdout', 'w');
34+
$this->handle = defined('STDOUT') ? STDOUT : fopen('php://stdout', 'w');
3035
}
3136

3237
protected function process(array $message): void
3338
{
34-
\fwrite($this->handle, \strtr($this->format, $message) . PHP_EOL);
39+
fwrite($this->handle, strtr($this->format, $message) . PHP_EOL);
3540
}
3641
}

Processors/File.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414

1515
use Exception;
1616
use Koded\Exceptions\KodedException;
17+
use function date;
18+
use function file_put_contents;
19+
use function is_dir;
20+
use function is_writable;
21+
use function mkdir;
22+
use function rtrim;
23+
use function strtr;
24+
use function umask;
1725

1826
/**
1927
* Log processor for storing the log messages into text files on the disk.
@@ -40,30 +48,26 @@ class File extends Processor
4048
public function __construct(array $settings)
4149
{
4250
parent::__construct($settings);
43-
44-
\umask(\umask() | 0002);
51+
umask(umask() | 0002);
52+
$this->dir = rtrim((string)$settings['dir'], '/');
4553
$this->ext = (string)($settings['extension'] ?? '.log');
46-
$this->dir = \rtrim((string)$settings['dir'], '/');
47-
48-
if (false === \is_dir($this->dir)) {
54+
if (false === is_dir($this->dir)) {
4955
throw FileProcessorException::directoryDoesNotExist($this->dir);
5056
}
51-
52-
if (false === \is_writable($this->dir)) {
57+
if (false === is_writable($this->dir)) {
5358
throw FileProcessorException::directoryIsNotWritable($this->dir);
5459
}
55-
5660
$this->dir .= '/';
5761
}
5862

5963
protected function process(array $message): void
6064
{
6165
try {
6266
// The filename should be calculated at the moment of writing
63-
$dir = $this->dir . \date('Y/m');
64-
\is_dir($dir) || \mkdir($dir, 0775, true);
67+
$dir = $this->dir . date('Y/m');
68+
is_dir($dir) || mkdir($dir, 0775, true);
6569

66-
\file_put_contents($dir . '/' . \date('d') . $this->ext, \strtr($this->format, $message) . PHP_EOL,
70+
file_put_contents($dir . '/' . date('d') . $this->ext, strtr($this->format, $message) . PHP_EOL,
6771
FILE_APPEND);
6872

6973
// @codeCoverageIgnoreStart

Processors/Memory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace Koded\Logging\Processors;
1414

15+
use function strtr;
16+
1517
/**
1618
* In-memory logger.
1719
*
@@ -20,6 +22,6 @@ class Memory extends Processor
2022
{
2123
protected function process(array $message): void
2224
{
23-
$this->formatted .= PHP_EOL . \strtr($this->format, $message);
25+
$this->formatted .= PHP_EOL . strtr($this->format, $message);
2426
}
2527
}

Processors/Processor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public function __construct(array $settings)
4747
* Receive update from the Log instance.
4848
* This is where the messages are filtered and processed.
4949
*
50-
* @param array $message The message to be processed
50+
* @param array{level: int, levelname: string, message: string, timestamp: string} $message
51+
* The message to be processed
5152
*
5253
* @return void
5354
*/

Processors/Syslog.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
namespace Koded\Logging\Processors;
1414

1515
use Koded\Logging\Logger;
16+
use function closelog;
17+
use function openlog;
18+
use function strtr;
1619

1720
/**
1821
* System log.
@@ -37,10 +40,10 @@ protected function process(array $message): void
3740
];
3841

3942
try {
40-
\openlog(null, LOG_CONS, LOG_USER);
41-
\syslog($levels[$message['level']] ?? LOG_DEBUG, \strtr($this->format, $message));
43+
openlog('', LOG_CONS, LOG_USER);
44+
\syslog($levels[$message['level']] ?? LOG_DEBUG, strtr($this->format, $message));
4245
} finally {
43-
\closelog();
46+
closelog();
4447
}
4548
}
4649
}

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.1.0
1+
3.2.0

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
"prefer-stable": true,
2121
"minimum-stability": "dev",
2222
"require": {
23-
"php": "^8",
24-
"psr/log": "^1",
25-
"koded/stdlib": "^5"
23+
"psr/log": "^3",
24+
"koded/stdlib": "^6.1.0"
2625
},
2726
"autoload": {
2827
"psr-4": {

0 commit comments

Comments
 (0)