Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Paramtamtam committed Apr 11, 2019
1 parent c16e61f commit 2a2d832
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 32 deletions.
10 changes: 3 additions & 7 deletions src/Logging/DefaultLogstashLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,17 @@

class DefaultLogstashLogger implements LoggerContract
{
use Traits\AppNameTrait;

/**
* {@inheritdoc}
*
* @throws Exception
*/
public function __invoke(array $config): Logger
{
try {
$app_name = config()->get('app.name');
} catch (\Throwable $e) {
//
}

$formatter = new DefaultLogstashFormatter(
$config['formatter']['app_name'] ?? $app_name ?? 'app',
$config['formatter']['app_name'] ?? $this->getAppName() ?? 'app',
$config['formatter']['system_name'] ?? null,
$config['formatter']['extra_prefix'] ?? false,
$config['formatter']['context_prefix'] ?? null,
Expand Down
12 changes: 4 additions & 8 deletions src/Logging/DefaultUdpLogstashLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

class DefaultUdpLogstashLogger implements LoggerContract
{
use Traits\AppNameTrait;

/**
* {@inheritdoc}
*
Expand All @@ -21,17 +23,11 @@ class DefaultUdpLogstashLogger implements LoggerContract
public function __invoke(array $config): Logger
{
if (! isset($config['host'], $config['port'])) {
throw new InvalidArgumentException('[host] and [port] values is required for this logger');
}

try {
$app_name = config()->get('app.name');
} catch (\Throwable $e) {
//
throw new InvalidArgumentException('[host] and [port] values are required for this logger');
}

$formatter = new DefaultLogstashFormatter(
$config['formatter']['app_name'] ?? $app_name ?? 'app',
$config['formatter']['app_name'] ?? $this->getAppName() ?? 'app',
$config['formatter']['system_name'] ?? null,
$config['formatter']['extra_prefix'] ?? false,
$config['formatter']['context_prefix'] ?? null,
Expand Down
10 changes: 3 additions & 7 deletions src/Logging/EventsLogstashLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,17 @@

class EventsLogstashLogger implements LoggerContract
{
use Traits\AppNameTrait;

/**
* {@inheritdoc}
*
* @throws Exception
*/
public function __invoke(array $config): Logger
{
try {
$app_name = config()->get('app.name');
} catch (\Throwable $e) {
//
}

$formatter = new EventsLogstashFormatter(
$config['formatter']['app_name'] ?? $app_name ?? 'app',
$config['formatter']['app_name'] ?? $this->getAppName() ?? 'app',
$config['formatter']['system_name'] ?? null,
$config['formatter']['extra_prefix'] ?? false,
$config['formatter']['context_prefix'] ?? null,
Expand Down
12 changes: 4 additions & 8 deletions src/Logging/EventsUdpLogstashLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

class EventsUdpLogstashLogger implements LoggerContract
{
use Traits\AppNameTrait;

/**
* {@inheritdoc}
*
Expand All @@ -21,17 +23,11 @@ class EventsUdpLogstashLogger implements LoggerContract
public function __invoke(array $config): Logger
{
if (! isset($config['host'], $config['port'])) {
throw new InvalidArgumentException('[host] and [port] values is required for this logger');
}

try {
$app_name = config()->get('app.name');
} catch (\Throwable $e) {
//
throw new InvalidArgumentException('[host] and [port] values are required for this logger');
}

$formatter = new EventsLogstashFormatter(
$config['formatter']['app_name'] ?? $app_name ?? 'app',
$config['formatter']['app_name'] ?? $this->getAppName() ?? 'app',
$config['formatter']['system_name'] ?? null,
$config['formatter']['extra_prefix'] ?? false,
$config['formatter']['context_prefix'] ?? null,
Expand Down
4 changes: 2 additions & 2 deletions src/Logging/Handlers/UdpHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ public function close(): void
*/
public function recordToString(array $record): string
{
if (isset($record['formatted']) && \is_string($record['formatted'])) {
return $record['formatted'];
if (isset($record['formatted']) && \is_string($formatted = $record['formatted'])) {
return $formatted;
}

return 'ERROR: FORMATTER NOT SET';
Expand Down
24 changes: 24 additions & 0 deletions src/Logging/Traits/AppNameTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types = 1);

namespace AvtoDev\EventsLogLaravel\Logging\Traits;

trait AppNameTrait
{
/**
* Get application name.
*
* @return string|null
*/
protected function getAppName(): ?string
{
try {
return config()->get('app.name');
} catch (\Throwable $e) {
//
}

return null;
}
}

0 comments on commit 2a2d832

Please sign in to comment.