Skip to content

Commit b6d456b

Browse files
authored
[Logger] New Component (#120)
0 parents  commit b6d456b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2058
-0
lines changed

.gitattributes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
composer.lock
2+
phpunit.xml
3+
vendor/

Context.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SonsOfPHP\Component\Logger;
6+
7+
use SonsOfPHP\Contract\Logger\ContextInterface;
8+
9+
/**
10+
* @author Joshua Estes <[email protected]>
11+
*/
12+
class Context implements ContextInterface
13+
{
14+
public function __construct(
15+
private array $context = [],
16+
) {}
17+
18+
public function all(): array
19+
{
20+
return $this->context;
21+
}
22+
23+
public function offsetExists(mixed $offset): bool
24+
{
25+
if (!is_string($offset)) {
26+
throw new \InvalidArgumentException('Only strings are supported as keys');
27+
}
28+
29+
return array_key_exists($offset, $this->context);
30+
}
31+
32+
public function offsetGet(mixed $offset): mixed
33+
{
34+
if (!is_string($offset)) {
35+
throw new \InvalidArgumentException('Only strings are supported as keys');
36+
}
37+
38+
return $this->context[$offset] ?? null;
39+
}
40+
41+
public function offsetSet(mixed $offset, mixed $value): void
42+
{
43+
if (!is_string($offset)) {
44+
throw new \InvalidArgumentException('Only strings are supported as keys');
45+
}
46+
47+
if (is_object($value) && !$value instanceof \Stringable) {
48+
throw new \InvalidArgumentException('Only Stringable Objects are supported');
49+
}
50+
51+
$this->context[$offset] = $value;
52+
}
53+
54+
public function offsetUnset(mixed $offset): void
55+
{
56+
if (!is_string($offset)) {
57+
throw new \InvalidArgumentException('Only strings are supported as keys');
58+
}
59+
60+
if ($this->offsetExists($offset)) {
61+
unset($this->context[$offset]);
62+
}
63+
}
64+
65+
public function getIterator(): \Traversable
66+
{
67+
return new \ArrayIterator($this->context);
68+
}
69+
}

Enricher/GroupIdEnricher.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SonsOfPHP\Component\Logger\Enricher;
6+
7+
use SonsOfPHP\Contract\Logger\EnricherInterface;
8+
use SonsOfPHP\Contract\Logger\RecordInterface;
9+
10+
/**
11+
* @author Joshua Estes <[email protected]>
12+
*/
13+
class GroupIdEnricher implements EnricherInterface
14+
{
15+
public function __invoke(RecordInterface $record): RecordInterface
16+
{
17+
$context = $record->getContext();
18+
$context['group_id'] = getmygid();
19+
20+
return $record->withContext($context);
21+
}
22+
}

Enricher/InodeEnricher.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SonsOfPHP\Component\Logger\Enricher;
6+
7+
use SonsOfPHP\Contract\Logger\EnricherInterface;
8+
use SonsOfPHP\Contract\Logger\RecordInterface;
9+
10+
/**
11+
* @author Joshua Estes <[email protected]>
12+
*/
13+
class InodeEnricher implements EnricherInterface
14+
{
15+
public function __invoke(RecordInterface $record): RecordInterface
16+
{
17+
$context = $record->getContext();
18+
$context['inode'] = getmyinode();
19+
20+
return $record->withContext($context);
21+
}
22+
}

Enricher/MaskContextValueEnricher.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SonsOfPHP\Component\Logger\Enricher;
6+
7+
use SonsOfPHP\Contract\Logger\EnricherInterface;
8+
use SonsOfPHP\Contract\Logger\RecordInterface;
9+
10+
/**
11+
* This enricher allows you to mask data in the context.
12+
*
13+
* Examples:
14+
* new MaskContextValue('credit_card_number');
15+
* new MaskContextValue('cvv');
16+
* new MaskContextValue(['credit_card_number', 'cvv', 'password'], '*');
17+
*
18+
* @author Joshua Estes <[email protected]>
19+
*/
20+
class MaskContextValueEnricher implements EnricherInterface
21+
{
22+
/**
23+
* You can add a single key or you can add an array of keys.
24+
*
25+
* If you pass in an array for the key, it will search the context keys and
26+
* mask the values if any are found.
27+
*
28+
* The mask value can be changed as well.
29+
*/
30+
public function __construct(
31+
private array|string $key,
32+
private string $maskValue = '****',
33+
) {}
34+
35+
public function __invoke(RecordInterface $record): RecordInterface
36+
{
37+
$context = $record->getContext();
38+
39+
$keys = $this->key;
40+
41+
if (!is_array($keys)) {
42+
$keys = [$keys];
43+
}
44+
45+
foreach ($keys as $key) {
46+
if (isset($context[$key])) {
47+
$context[$key] = $this->maskValue;
48+
}
49+
}
50+
51+
return $record->withContext($context);
52+
}
53+
}

Enricher/NullEnricher.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SonsOfPHP\Component\Logger\Enricher;
6+
7+
use SonsOfPHP\Contract\Logger\EnricherInterface;
8+
use SonsOfPHP\Contract\Logger\RecordInterface;
9+
10+
/**
11+
* The handler that says, "fuck your message"
12+
*
13+
* @author Joshua Estes <[email protected]>
14+
*/
15+
class NullEnricher implements EnricherInterface
16+
{
17+
public function __invoke(RecordInterface $record): RecordInterface
18+
{
19+
return $record;
20+
}
21+
}

Enricher/ProcessIdEnricher.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SonsOfPHP\Component\Logger\Enricher;
6+
7+
use SonsOfPHP\Contract\Logger\EnricherInterface;
8+
use SonsOfPHP\Contract\Logger\RecordInterface;
9+
10+
/**
11+
* @author Joshua Estes <[email protected]>
12+
*/
13+
class ProcessIdEnricher implements EnricherInterface
14+
{
15+
public function __invoke(RecordInterface $record): RecordInterface
16+
{
17+
$context = $record->getContext();
18+
$context['process_id'] = getmypid();
19+
20+
return $record->withContext($context);
21+
}
22+
}

Enricher/ScriptOwnerEnricher.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SonsOfPHP\Component\Logger\Enricher;
6+
7+
use SonsOfPHP\Contract\Logger\EnricherInterface;
8+
use SonsOfPHP\Contract\Logger\RecordInterface;
9+
10+
/**
11+
* @author Joshua Estes <[email protected]>
12+
*/
13+
class ScriptOwnerEnricher implements EnricherInterface
14+
{
15+
public function __invoke(RecordInterface $record): RecordInterface
16+
{
17+
$context = $record->getContext();
18+
$context['script_owner'] = get_current_user();
19+
20+
return $record->withContext($context);
21+
}
22+
}

Enricher/UserIdEnricher.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SonsOfPHP\Component\Logger\Enricher;
6+
7+
use SonsOfPHP\Contract\Logger\EnricherInterface;
8+
use SonsOfPHP\Contract\Logger\RecordInterface;
9+
10+
/**
11+
* @author Joshua Estes <[email protected]>
12+
*/
13+
class UserIdEnricher implements EnricherInterface
14+
{
15+
public function __invoke(RecordInterface $record): RecordInterface
16+
{
17+
$context = $record->getContext();
18+
$context['user_id'] = getmyuid();
19+
20+
return $record->withContext($context);
21+
}
22+
}

0 commit comments

Comments
 (0)