Skip to content

Commit 0541b36

Browse files
authored
Merge pull request #3 from kodedphp/3
v3.0.0
2 parents 87eb15d + bb8e907 commit 0541b36

31 files changed

+212
-185
lines changed

.scrutinizer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ build:
77
- php-scrutinizer-run
88
environment:
99
php:
10-
version: '7.3'
10+
version: '8.0.1'
1111

1212
before_commands:
1313
- 'composer update -o --prefer-source --no-interaction'

.travis.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
language: php
22
os: linux
3-
dist: xenial
3+
dist: bionic
44

55
notifications:
66
email: false
77

88
php:
9-
- 7.2
10-
- 7.3
11-
- 7.4
9+
- 8.0
1210
- nightly
1311

1412
cache:

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
BSD 3-Clause License
22

3-
Copyright (c) 2019, Mihail Binev
3+
Copyright (c) 2021, Mihail Binev
44
All rights reserved.
55

66
Redistribution and use in source and binary forms, with or without

Log.php

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

1515
use Koded\Logging\Processors\{Cli, Processor};
16+
use DateTimeZone;
1617
use Psr\Log\LoggerTrait;
1718
use Throwable;
1819

@@ -65,27 +66,27 @@ class Log implements Logger
6566
/**
6667
* @var bool Flag to control the messages processing
6768
*/
68-
private $deferred = false;
69+
private bool $deferred = false;
6970

7071
/**
7172
* @var string The date format for the message.
7273
*/
73-
private $dateFormat;
74+
private string $dateFormat;
7475

7576
/**
76-
* @var string Valid timezone for the message.
77+
* @var DateTimeZone Valid timezone for the message.
7778
*/
78-
private $timezone = 'UTC';
79+
private DateTimeZone|bool $timezone;
7980

8081
/**
8182
* @var Processor[] Hash with all registered log processors.
8283
*/
83-
private $processors = [];
84+
private array $processors = [];
8485

8586
/**
8687
* @var array List with all accumulated messages.
8788
*/
88-
private $messages = [];
89+
private array $messages = [];
8990

9091
/**
9192
* Creates all requested log processors.
@@ -96,70 +97,48 @@ public function __construct(array $settings)
9697
{
9798
$this->deferred = (bool)($settings['deferred'] ?? false);
9899
$this->dateFormat = (string)($settings['dateformat'] ?? 'd/m/Y H:i:s.u');
99-
$this->timezone = (string)($settings['timezone'] ?? $this->timezone);
100-
100+
if (false === $this->timezone = @\timezone_open((string)($settings['timezone'] ?? 'UTC'))) {
101+
$this->timezone = \timezone_open('UTC');
102+
}
101103
foreach ((array)($settings['loggers'] ?? []) as $processor) {
102104
$this->attach(new $processor['class']($processor));
103105
}
104-
105106
if ($this->deferred) {
106-
register_shutdown_function([$this, 'process']);
107+
\register_shutdown_function([$this, 'process']);
107108
}
108109
}
109110

110111
public function attach(Processor $processor): Logger
111112
{
112113
if (0 !== $processor->levels()) {
113-
$this->processors[spl_object_hash($processor)] = $processor;
114+
$this->processors[\spl_object_hash($processor)] = $processor;
114115
}
115-
116116
return $this;
117117
}
118118

119119
public function log($level, $message, array $context = [])
120120
{
121121
try {
122-
$levelname = strtoupper($level);
123-
$level = constant('self::' . $levelname);
124-
} catch (Throwable $e) {
125-
$levelname = 'LOG';
122+
$levelName = \strtoupper($level);
123+
$level = \constant('static::' . $levelName);
124+
} catch (Throwable) {
125+
$levelName = 'LOG';
126126
$level = -1;
127127
}
128-
129128
$this->messages[] = [
130129
'level' => $level,
131-
'levelname' => $levelname,
130+
'levelname' => $levelName,
132131
'message' => $this->formatMessage($message, $context),
133-
'timestamp' => date_create_immutable('now', timezone_open($this->timezone) ?: null)->format($this->dateFormat),
132+
'timestamp' => \date_create_immutable('now', $this->timezone ?: null)->format($this->dateFormat),
134133
];
135-
136134
$this->deferred || $this->process();
137135
}
138136

139-
/**
140-
* Parses the message as in the interface specification.
141-
*
142-
* @param string|object $message A string or object that implements __toString
143-
* @param array $params [optional] Arbitrary data with key-value pairs replacements
144-
*
145-
* @return string
146-
*/
147-
private function formatMessage($message, array $params = []): string
148-
{
149-
$replacements = [];
150-
foreach ($params as $k => $v) {
151-
$replacements['{' . $k . '}'] = $v;
152-
}
153-
154-
return strtr((string)$message, $replacements);
155-
}
156-
157137
public function process(): void
158138
{
159139
foreach ($this->processors as $processor) {
160140
$processor->update($this->messages);
161141
}
162-
163142
$this->messages = [];
164143
}
165144

@@ -175,8 +154,24 @@ public function exception(Throwable $e, Processor $processor = null): void
175154

176155
public function detach(Processor $processor): Logger
177156
{
178-
unset($this->processors[spl_object_hash($processor)]);
179-
157+
unset($this->processors[\spl_object_hash($processor)]);
180158
return $this;
181159
}
160+
161+
/**
162+
* Parses the message as in the interface specification.
163+
*
164+
* @param object|string $message A string or object that implements __toString
165+
* @param array $params [optional] Arbitrary data with key-value pairs replacements
166+
*
167+
* @return string
168+
*/
169+
private function formatMessage(object|string $message, array $params = []): string
170+
{
171+
$replacements = [];
172+
foreach ($params as $k => $v) {
173+
$replacements['{' . $k . '}'] = $v;
174+
}
175+
return \strtr((string)$message, $replacements);
176+
}
182177
}

Processors/Cli.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,24 @@
1313
namespace Koded\Logging\Processors;
1414

1515
/**
16-
* Log processor for CLI apps.
16+
* Log processor for CLI.
1717
*
1818
*/
1919
class Cli extends Processor
2020
{
21-
/** @var string Message format */
22-
protected $format = '> [timestamp][levelname] - message';
21+
protected string $format = '> [timestamp][levelname] - message';
2322

24-
/** @var bool */
25-
private $buffer;
23+
/** @var resource */
24+
private $handle;
2625

2726
public function __construct(array $settings)
2827
{
2928
parent::__construct($settings);
30-
$this->buffer = defined('STDERR');
29+
$this->handle = \defined('STDERR') ? STDERR : \fopen('php://stderr', 'w');
3130
}
3231

33-
protected function parse(array $message): void
32+
protected function process(array $message): void
3433
{
35-
if ($this->buffer) {
36-
fwrite(STDERR, strtr($this->format, $message) . PHP_EOL);
37-
}
34+
\fwrite($this->handle, \strtr($this->format, $message) . PHP_EOL);
3835
}
3936
}

Processors/ErrorLog.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
*/
1919
class ErrorLog extends Processor
2020
{
21-
protected function parse(array $message): void
21+
protected function process(array $message): void
2222
{
23-
error_log(strtr($this->format, $message), 0);
23+
\error_log(strtr($this->format, $message), 0);
2424
}
2525
}

Processors/File.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
*/
3232
class File extends Processor
3333
{
34-
private $dir = '';
35-
private $ext = '';
34+
private string $dir = '';
35+
private string $ext = '';
3636

3737
/**
3838
* {@inheritdoc}
@@ -41,29 +41,29 @@ public function __construct(array $settings)
4141
{
4242
parent::__construct($settings);
4343

44-
umask(umask() | 0002);
44+
\umask(\umask() | 0002);
4545
$this->ext = (string)($settings['extension'] ?? '.log');
46-
$this->dir = rtrim((string)$settings['dir'], '/');
46+
$this->dir = \rtrim((string)$settings['dir'], '/');
4747

48-
if (false === is_dir($this->dir)) {
48+
if (false === \is_dir($this->dir)) {
4949
throw FileProcessorException::directoryDoesNotExist($this->dir);
5050
}
5151

52-
if (false === is_writable($this->dir)) {
52+
if (false === \is_writable($this->dir)) {
5353
throw FileProcessorException::directoryIsNotWritable($this->dir);
5454
}
5555

5656
$this->dir .= '/';
5757
}
5858

59-
protected function parse(array $message): void
59+
protected function process(array $message): void
6060
{
6161
try {
6262
// 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);
63+
$dir = $this->dir . \date('Y/m');
64+
\is_dir($dir) || \mkdir($dir, 0775, true);
6565

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

6969
// @codeCoverageIgnoreStart
@@ -78,20 +78,21 @@ protected function parse(array $message): void
7878
class FileProcessorException extends KodedException
7979
{
8080
private const
81-
E_DIRECTORY_DOES_NOT_EXIST = 1, E_DIRECTORY_NOT_WRITABLE = 2;
81+
E_DIRECTORY_DOES_NOT_EXIST = 1,
82+
E_DIRECTORY_NOT_WRITABLE = 2;
8283

83-
protected $messages = [
84+
protected array $messages = [
8485
self::E_DIRECTORY_DOES_NOT_EXIST => 'Log directory ":dir" must exist',
8586
self::E_DIRECTORY_NOT_WRITABLE => 'Log directory ":dir" must be writable',
8687
];
8788

88-
public static function directoryDoesNotExist(string $directory): self
89+
public static function directoryDoesNotExist(string $directory): static
8990
{
90-
return new self(self::E_DIRECTORY_DOES_NOT_EXIST, [':dir' => $directory]);
91+
return new static(static::E_DIRECTORY_DOES_NOT_EXIST, [':dir' => $directory]);
9192
}
9293

93-
public static function directoryIsNotWritable(string $directory): self
94+
public static function directoryIsNotWritable(string $directory): static
9495
{
95-
return new self(self::E_DIRECTORY_NOT_WRITABLE, [':dir' => $directory]);
96+
return new static(static::E_DIRECTORY_NOT_WRITABLE, [':dir' => $directory]);
9697
}
9798
}

Processors/Memory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
*/
1919
class Memory extends Processor
2020
{
21-
protected function parse(array $message): void
21+
protected function process(array $message): void
2222
{
23-
$this->formatted .= PHP_EOL . strtr($this->format, $message);
23+
$this->formatted .= PHP_EOL . \strtr($this->format, $message);
2424
}
2525
}

Processors/Processor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ abstract class Processor
2727
/**
2828
* @var string The log message format.
2929
*/
30-
protected $format = 'timestamp [levelname]: message';
30+
protected string $format = 'timestamp [levelname]: message';
3131

3232
/**
3333
* @var string Keeps all formatted log messages in this property.
@@ -55,7 +55,7 @@ public function update(array $messages): void
5555
{
5656
foreach ($messages as $message) {
5757
if ($message['level'] & $this->levels) {
58-
$this->parse($message);
58+
$this->process($message);
5959
}
6060
}
6161
}
@@ -68,7 +68,7 @@ public function update(array $messages): void
6868
*
6969
* @return void
7070
*/
71-
abstract protected function parse(array $message): void;
71+
abstract protected function process(array $message): void;
7272

7373
/**
7474
* Returns all enabled log levels for the processor object.

Processors/Syslog.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
*/
2222
class Syslog extends Processor
2323
{
24-
protected $format = '[levelname] message';
24+
protected string $format = '[levelname] message';
2525

26-
protected function parse(array $message): void
26+
protected function process(array $message): void
2727
{
2828
$levels = [
2929
Logger::DEBUG => LOG_DEBUG,
@@ -37,10 +37,10 @@ protected function parse(array $message): void
3737
];
3838

3939
try {
40-
openlog(null, LOG_CONS, LOG_USER);
41-
syslog($levels[$message['level']] ?? LOG_DEBUG, strtr($this->format, $message));
40+
\openlog(null, LOG_CONS, LOG_USER);
41+
\syslog($levels[$message['level']] ?? LOG_DEBUG, \strtr($this->format, $message));
4242
} finally {
43-
closelog();
43+
\closelog();
4444
}
4545
}
4646
}

0 commit comments

Comments
 (0)