Skip to content

Commit cf62d1b

Browse files
committed
refactor FileHandler
1 parent b89bebe commit cf62d1b

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

system/Log/Handlers/FileHandler.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,43 @@ class FileHandler extends BaseHandler
2828
*
2929
* @var string
3030
*/
31-
protected $path;
31+
protected $path = WRITEPATH . 'logs/';
3232

3333
/**
3434
* Extension to use for log files
3535
*
3636
* @var string
3737
*/
38-
protected $fileExtension;
38+
protected $fileExtension = 'log';
3939

4040
/**
4141
* Permissions for new log files
4242
*
4343
* @var int
4444
*/
45-
protected $filePermissions;
45+
protected $filePermissions = 0644;
4646

4747
/**
48-
* Constructor
48+
* @param array{handles?: list<string>, path?: string, fileExtension?: string, filePermissions?: int} $config
4949
*/
5050
public function __construct(array $config = [])
5151
{
5252
parent::__construct($config);
5353

54-
$this->path = empty($config['path']) ? WRITEPATH . 'logs/' : $config['path'];
54+
$config = [
55+
...['path' => WRITEPATH . 'logs/', 'fileExtension' => 'log', 'filePermissions' => 0644],
56+
...$config,
57+
];
5558

56-
$this->fileExtension = empty($config['fileExtension']) ? 'log' : $config['fileExtension'];
57-
$this->fileExtension = ltrim($this->fileExtension, '.');
59+
if ($config['path'] !== '') {
60+
$this->path = $config['path'];
61+
}
62+
63+
if ($config['fileExtension'] !== '') {
64+
$this->fileExtension = ltrim($config['fileExtension'], '.');
65+
}
5866

59-
$this->filePermissions = $config['filePermissions'] ?? 0644;
67+
$this->filePermissions = $config['filePermissions'];
6068
}
6169

6270
/**
@@ -108,10 +116,8 @@ public function handle($level, $message): bool
108116

109117
for ($written = 0, $length = strlen($msg); $written < $length; $written += $result) {
110118
if (($result = fwrite($fp, substr($msg, $written))) === false) {
111-
// if we get this far, we'll never see this during travis-ci
112-
// @codeCoverageIgnoreStart
113-
break;
114-
// @codeCoverageIgnoreEnd
119+
// if we get this far, we'll never see this during unit testing
120+
break; // @codeCoverageIgnore
115121
}
116122
}
117123

0 commit comments

Comments
 (0)