1. Go to this page and download the library: Download hamidrezaniazi/pecs library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hamidrezaniazi / pecs example snippets
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Hamidrezaniazi\Pecs\Monolog\EcsFormatter;
use Hamidrezaniazi\Pecs\Fields\Event;
$log = new Logger('logger name');
$handler = new StreamHandler('ecs.logs');
$log->pushHandler($handler->setFormatter(new EcsFormatter()));
$log->info('message', [
new Event(action: 'test event'),
]);
public function __construct(LoggerInterface $ecsLogger)
{
$ecsLogger->info('sample message', [
new Event(kind: EventKind::METRIC),
]);
}
use Illuminate\Log\Logger;
use Monolog\Handler\FormattableHandlerInterface;
use Hamidrezaniazi\Pecs\Monolog\EcsFormatter;
class LaravelEcsFormatter
{
public function __invoke(Logger $logger): void
{
foreach ($logger->getHandlers() as $handler) {
/** @var FormattableHandlerInterface $handler */
$handler->setFormatter(app(EcsFormatter::class));
}
}
}
use Hamidrezaniazi\Pecs\EcsFieldsCollection;
use Hamidrezaniazi\Pecs\Fields\Base;
use Hamidrezaniazi\Pecs\Properties\ValueList;
(new EcsFieldsCollection([
new Base(message: 'Hello World'),
new Base(message: 'test', tags: (new ValueList())->push('staging')),
]))->render()->toArray();
use Hamidrezaniazi\Pecs\Fields\Ecs;
use Hamidrezaniazi\Pecs\Monolog\EcsFormatter;
class CustomEcsFormatter extends EcsFormatter
{
protected function prepare(array $record): EcsFieldsCollection
{
return parent::prepare($record)->push(new Ecs(version: '1.0.0'));
}
}
use Hamidrezaniazi\Pecs\EcsFieldsCollection;
use Hamidrezaniazi\Pecs\Fields\Base;
use Hamidrezaniazi\Pecs\Fields\Log;
(new EcsFieldsCollection([
new Base(message: 'Hello World'),
new Log(level: 'info'),
]))->render()->toArray();