Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .ci/docker/gren/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ RUN apt-get update -qq -y \
RUN npm install github-release-notes@0.17.3 -g
WORKDIR /app

USER node

ENTRYPOINT [ "/app/.ci/docker/gren/entrypoint.sh" ]
CMD [ "release" ]
CMD [ "release" ]
4 changes: 0 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ jobs:
fail-fast: false
matrix:
php-image:
- 7.2-alpine
- 7.3-alpine
- 7.4-alpine
- 8.0-alpine
- 8.1-alpine
- 8.2-alpine
runs-on: ubuntu-latest
Expand Down
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
## Artifactory Composer (required)

All package resolution must go through Artifactory `composer-virt`. Packagist is disabled.

```bash
export COMPOSER_AUTH='{"http-basic":{"gdartifactory1.jfrog.io":{"username":"<USER>","password":"<IDENTITY_TOKEN>"}}}'
composer install
```

Or store credentials in `~/.composer/auth.json` (never commit tokens).

------

[![Build Status](https://apm-ci.elastic.co/buildStatus/icon?job=apm-agent-php%2Fecs-logging-php-mbp%2Fmain)](https://apm-ci.elastic.co/job/apm-agent-php/job/ecs-logging-php-mbp/job/main/)

# ECS Logging for PHP
Expand All @@ -16,10 +29,10 @@ composer require elastic/ecs-logging
```

## Examples and Usage
* [Monolog v2.0](https://github.com/elastic/ecs-logging-php/blob/main/docs/Monolog_v2.md)
* [Monolog v3.0](https://github.com/elastic/ecs-logging-php/blob/main/docs/Monolog_v3.md)

## Library Support
* Currently only [Monolog:2.*](https://github.com/Seldaek/monolog) is supported.
* Currently only [Monolog:3.*](https://github.com/Seldaek/monolog) is supported.
* The major version of this library is compatible with the major version of ECS.

## References
Expand Down
13 changes: 10 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
}
],
"require": {
"php": "^7.2||^8.0",
"psr/log": "^1.0.1",
"php": ">=8.1",
"psr/log": "^1|^2|3",
"ext-json": "*"
},
"require-dev": {
"php-parallel-lint/php-parallel-lint": "^1.3.2",
"squizlabs/php_codesniffer": "3.*",
"monolog/monolog": "^2.0",
"monolog/monolog": "^3.0",
"phpunit/phpunit": "^8.5||^9.5||^10"
},
"autoload": {
Expand All @@ -46,5 +46,12 @@
},
"config": {
"sort-packages": true
},
"repositories": {
"packagist.org": false,
"artifactory": {
"type": "composer",
"url": "https://gdartifactory1.jfrog.io/artifactory/api/composer/composer-virt/"
}
}
}
2 changes: 1 addition & 1 deletion docs/Monolog_v2.md → docs/Monolog_v3.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Monolog v2.x
# Monolog v3.x

## Initialize the Formatter
```php
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@

use Elastic\Types\Error as EcsError;
use Monolog\Formatter\NormalizerFormatter;
use Monolog\LogRecord;
use Throwable;

/**
* Serializes a log message to the Elastic Common Schema (ECS)
*
* @version Monolog v2.x
* @version Monolog v3.x
* @version ECS v1.x
*
* @see https://www.elastic.co/guide/en/ecs/1.4/ecs-log.html
Expand Down Expand Up @@ -55,7 +56,7 @@ public function useLogOriginFromContext(bool $useLogOriginFromContext): self
}

/** @inheritDoc */
protected function normalize($data, int $depth = 0)
protected function normalize(mixed $data, int $depth = 0): mixed
{
if ($depth > $this->maxNormalizeDepth) {
return parent::normalize($data, $depth);
Expand All @@ -79,9 +80,9 @@ protected function normalize($data, int $depth = 0)
* @link https://www.elastic.co/guide/en/ecs/1.1/ecs-base.html
* @link https://www.elastic.co/guide/en/ecs/current/ecs-tracing.html
*/
public function format(array $record): string
public function format(LogRecord $record): string
{
$inRecord = $this->normalize($record);
$inRecord = $this->normalize($record->toArray());

// Build Skeleton with "@timestamp" and "log.level"
$outRecord = [
Expand Down
138 changes: 66 additions & 72 deletions tests/Elastic/Monolog/Formatter/ElasticCommonSchemaFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
use Elastic\Tests\BaseTestCase;
use Elastic\Tests\HelperForMonolog;
use Elastic\Types\{Error, Service, Tracing, User};
use Monolog\Level;
use Monolog\Logger;
use Monolog\LogRecord;
use Monolog\Processor\IntrospectionProcessor;
use Monolog\Processor\TagProcessor;
use Throwable;
Expand All @@ -39,15 +41,14 @@ class ElasticCommonSchemaFormatterTest extends BaseTestCase
*/
public function testFormat()
{
$msg = [
'level' => Logger::INFO,
'level_name' => 'INFO',
'channel' => 'ecs',
'datetime' => new DateTimeImmutable("@0"),
'message' => md5(uniqid()),
'context' => [],
'extra' => [],
];
$msg = new LogRecord(
datetime: new DateTimeImmutable("@0"),
channel: 'ecs',
level: Level::Info,
message: md5(uniqid()),
context: [],
extra: []
);

$formatter = new ElasticCommonSchemaFormatter();
$doc = $formatter->format($msg);
Expand Down Expand Up @@ -79,15 +80,14 @@ public function testFormat()

public function testTimezone()
{
$msg = [
'level' => Logger::INFO,
'level_name' => 'INFO',
'channel' => 'ecs',
'datetime' => new DateTimeImmutable('2013-11-28T12:34:56.98765', new DateTimeZone('-03:45')),
'message' => md5(uniqid()),
'context' => [],
'extra' => [],
];
$msg = new LogRecord(
datetime: new DateTimeImmutable('2013-11-28T12:34:56.98765', new DateTimeZone('-03:45')),
channel: 'ecs',
level: Level::Info,
message: md5(uniqid()),
context: [],
extra: []
);

$formatter = new ElasticCommonSchemaFormatter();
$doc = $formatter->format($msg);
Expand All @@ -108,15 +108,14 @@ public function testTimezone()
public function testContextWithTracing()
{
$tracing = new Tracing($this->generateTraceId(), $this->generateTransactionId());
$msg = [
'level' => Logger::NOTICE,
'level_name' => 'NOTICE',
'channel' => 'ecs',
'datetime' => new DateTimeImmutable("@0"),
'message' => md5(uniqid()),
'context' => ['tracing' => $tracing],
'extra' => [],
];
$msg = new LogRecord(
datetime: new DateTimeImmutable("@0"),
channel: 'ecs',
level: Level::Notice,
message: md5(uniqid()),
context: ['tracing' => $tracing],
extra: []
);

$formatter = new ElasticCommonSchemaFormatter();
$doc = $formatter->format($msg);
Expand All @@ -143,15 +142,14 @@ public function testContextWithService()
$service->setId(rand(100, 999));
$service->setName('funky-service-01');

$msg = [
'level' => Logger::NOTICE,
'level_name' => 'NOTICE',
'channel' => 'ecs',
'datetime' => new DateTimeImmutable("@0"),
'message' => md5(uniqid()),
'context' => ['service' => $service],
'extra' => [],
];
$msg = new LogRecord(
datetime: new DateTimeImmutable("@0"),
channel: 'ecs',
level: Level::Notice,
message: md5(uniqid()),
context: ['service' => $service],
extra: []
);

$formatter = new ElasticCommonSchemaFormatter();
$doc = $formatter->format($msg);
Expand All @@ -177,15 +175,14 @@ public function testContextWithUser()
$user->setId(rand(100, 999));
$user->setHash(md5(uniqid()));

$msg = [
'level' => Logger::NOTICE,
'level_name' => 'NOTICE',
'channel' => 'ecs',
'datetime' => new DateTimeImmutable("@0"),
'message' => md5(uniqid()),
'context' => ['user' => $user],
'extra' => [],
];
$msg = new LogRecord(
datetime: new DateTimeImmutable("@0"),
channel: 'ecs',
level: Level::Notice,
message: md5(uniqid()),
context: ['user' => $user],
extra: []
);

$formatter = new ElasticCommonSchemaFormatter();
$doc = $formatter->format($msg);
Expand Down Expand Up @@ -223,15 +220,14 @@ public static function dataProviderForTestContextWithError(): iterable
*/
public function testContextWithError(Throwable $throwable, bool $shouldWrap): void
{
$msg = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'ecs',
'datetime' => new DateTimeImmutable("@0"),
'message' => md5(uniqid()),
'context' => ['error' => $shouldWrap ? new Error($throwable) : $throwable],
'extra' => [],
];
$msg = new LogRecord(
datetime: new DateTimeImmutable("@0"),
channel: 'ecs',
level: Level::Error,
message: md5(uniqid()),
context: ['error' => $shouldWrap ? new Error($throwable) : $throwable],
extra: []
);

$formatter = new ElasticCommonSchemaFormatter();
$doc = $formatter->format($msg);
Expand Down Expand Up @@ -265,15 +261,14 @@ public function testContextWithError(Throwable $throwable, bool $shouldWrap): vo
*/
public function testTags()
{
$msg = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'ecs',
'datetime' => new DateTimeImmutable("@0"),
'message' => md5(uniqid()),
'context' => [],
'extra' => [],
];
$msg = new LogRecord(
datetime: new DateTimeImmutable("@0"),
channel: 'ecs',
level: Level::Error,
message: md5(uniqid()),
context: [],
extra: []
);

$tags = [
'one',
Expand Down Expand Up @@ -332,15 +327,14 @@ public function testSanitizeOfLabelKeys()
$inContext['top_level_' . $key] = $key;
}

$inRecord = [
'level' => Logger::NOTICE,
'level_name' => 'NOTICE',
'channel' => 'ecs',
'datetime' => new DateTimeImmutable("@0"),
'message' => md5(uniqid()),
'context' => $inContext,
'extra' => [],
];
$inRecord = new LogRecord(
datetime: new DateTimeImmutable("@0"),
channel: 'ecs',
level: Level::Notice,
message: md5(uniqid()),
context: $inContext,
extra: []
);

$formatter = new ElasticCommonSchemaFormatter();
$doc = $formatter->format($inRecord);
Expand Down
7 changes: 4 additions & 3 deletions tests/Elastic/Monolog/Formatter/MockHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
namespace Elastic\Tests\Monolog\Formatter;

use Monolog\Handler\AbstractProcessingHandler;
use Monolog\LogRecord;

class MockHandler extends AbstractProcessingHandler
{
/** @var array<array> */
public $records = [];
/** @var array<LogRecord> */
public array $records = [];

protected function write(array $record): void
protected function write(LogRecord $record): void
{
$this->records[] = $record;
}
Expand Down