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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: mbstring,bcmath
extensions: mbstring,bcmath,swoole
tools: composer
- name: Install dependencies
run: composer install --quiet --no-ansi --no-interaction --no-scripts --no-progress
Expand Down
44 changes: 15 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
# Laravel Gelf Logger

[![Build Status](https://github.com/hedii/laravel-gelf-logger/workflows/Tests/badge.svg)](https://github.com/hedii/laravel-gelf-logger/actions)
[![Total Downloads](https://poser.pugx.org/hedii/laravel-gelf-logger/downloads)](//packagist.org/packages/hedii/laravel-gelf-logger)
[![License](https://poser.pugx.org/hedii/laravel-gelf-logger/license)](//packagist.org/packages/hedii/laravel-gelf-logger)
[![Latest Stable Version](https://poser.pugx.org/hedii/laravel-gelf-logger/v)](//packagist.org/packages/hedii/laravel-gelf-logger)

| **Laravel** | **laravel-gelf-logger** |
|-------------|-------------------------|
| 5.6 | ^3.0 |
| 5.8 | ^3.1 |
| 6.0 | ^4.0 |
| 7.0 | ^5.0 |
| 8.0 | ^5.3 |
| 8.0 | ^6.0 (with php 8) |
| 9.0 | ^7.0 |
| 10.0 | ^8.0 |
| 11.0 | ^9.0 |
| 12.0 | ^10.0 |

A package to send [gelf](http://docs.graylog.org/en/2.1/pages/gelf.html) logs to a gelf compatible backend like graylog. It is a Laravel wrapper for [bzikarsky/gelf-php](https://github.com/bzikarsky/gelf-php) package.

It uses the new [Laravel custom log channel](https://laravel.com/docs/master/logging) introduced in Laravel 5.6.
# Hypervel Gelf Logger

A package to send [gelf](http://docs.graylog.org/en/2.1/pages/gelf.html) logs to a gelf compatible backend like graylog. It is a Hypervel wrapper for [bzikarsky/gelf-php](https://github.com/bzikarsky/gelf-php) package.

This package uses Hypervel's custom log channel system.

A gelf receiver like graylog2 must be configured to receive messages with a GELF UDP, TCP or HTTP Input.

Expand All @@ -38,7 +20,7 @@ A gelf receiver like graylog2 must be configured to receive messages with a GELF
Install via [composer](https://getcomposer.org/doc/00-intro.md)

```sh
composer require hedii/laravel-gelf-logger
composer require nuwber/hypervel-gelf-logger
```

Edit `config/logging.php` to add the new `gelf` log channel.
Expand All @@ -59,7 +41,7 @@ return [
'gelf' => [
'driver' => 'custom',

'via' => \Hedii\LaravelGelfLogger\GelfLoggerFactory::class,
'via' => \Nuwber\HypervelGelfLogger\GelfLoggerFactory::class,

// This optional option determines the processors that should be
// pushed to the handler. This option is useful to modify a field
Expand All @@ -68,8 +50,8 @@ return [
// __invoke method: see monolog documentation about processors.
// Default is an empty array.
'processors' => [
\Hedii\LaravelGelfLogger\Processors\NullStringProcessor::class,
\Hedii\LaravelGelfLogger\Processors\RenameIdFieldProcessor::class,
\Nuwber\HypervelGelfLogger\Processors\NullStringProcessor::class,
\Nuwber\HypervelGelfLogger\Processors\RenameIdFieldProcessor::class,
// another processor...
],

Expand Down Expand Up @@ -167,7 +149,7 @@ return [

## Usage

Once you have modified the Laravel logging configuration, you can use the gelf log channel [as any Laravel log channel](https://laravel.com/docs/master/logging#writing-log-messages).
Once you have modified the Hypervel logging configuration, you can use the gelf log channel as any Hypervel log channel.

### Example

Expand All @@ -192,4 +174,8 @@ composer test

## License

laravel-gelf-logger is released under the MIT Licence. See the bundled [LICENSE](https://github.com/hedii/laravel-gelf-logger/blob/master/LICENSE.md) file for details.
hypervel-gelf-logger is released under the MIT Licence. See the bundled LICENSE file for details.

## Credits

This package is a Hypervel port of [hedii/laravel-gelf-logger](https://github.com/hedii/laravel-gelf-logger).
25 changes: 16 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"name": "hedii/laravel-gelf-logger",
"description": "A Laravel package to send logs to a gelf compatible backend like graylog",
"name": "nuwber/hypervel-gelf-logger",
"description": "A Hypervel package to send logs to a gelf compatible backend like graylog",
"type": "library",
"license": "MIT",
"keywords": [
"hedii",
"laravel-gelf",
"nuwber",
"hypervel",
"hypervel-gelf",
"gelf",
"udp",
"log",
"logging",
"graylog",
"graylog2",
"laravel",
"php"
],
"authors": [
Expand All @@ -28,19 +28,26 @@
"require": {
"php": "^8.2",
"graylog2/gelf-php": "^2.0",
"illuminate/log": "^12.0"
"hypervel/log": "^0.3.7",
"hypervel/laminas-mime": "^0.1.0"
},
"require-dev": {
"orchestra/testbench": "^10.0"
"hyperf/testing": "^3.1",
"phpunit/phpunit": "^10.0"
},
"autoload": {
"psr-4": {
"Hedii\\LaravelGelfLogger\\": "src/"
"Nuwber\\HypervelGelfLogger\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Hedii\\LaravelGelfLogger\\Tests\\": "tests/"
"Nuwber\\HypervelGelfLogger\\Tests\\": "tests/"
}
},
"extra": {
"hyperf": {
"config": "Nuwber\\HypervelGelfLogger\\ConfigProvider"
}
},
"config": {
Expand Down
22 changes: 22 additions & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Nuwber\HypervelGelfLogger;

class ConfigProvider
{
public function __invoke(): array
{
return [
'dependencies' => [],
'annotations' => [
'scan' => [
'paths' => [
__DIR__,
],
],
],
];
}
}
46 changes: 32 additions & 14 deletions src/GelfLoggerFactory.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace Hedii\LaravelGelfLogger;
declare(strict_types=1);

namespace Nuwber\HypervelGelfLogger;

use Gelf\Publisher;
use Gelf\Transport\AbstractTransport;
Expand All @@ -9,17 +11,15 @@
use Gelf\Transport\SslOptions;
use Gelf\Transport\TcpTransport;
use Gelf\Transport\UdpTransport;
use Illuminate\Contracts\Container\Container;
use Illuminate\Log\ParsesLogConfiguration;
use Monolog\Formatter\GelfMessageFormatter;
use Monolog\Handler\GelfHandler;
use Monolog\Level;
use Monolog\Logger;
use Psr\Container\ContainerInterface;

class GelfLoggerFactory
{
use ParsesLogConfiguration;

public function __construct(protected Container $app)
public function __construct(protected ContainerInterface $container)
{
}

Expand Down Expand Up @@ -48,7 +48,7 @@ public function __invoke(array $config): Logger
$transport = new IgnoreErrorTransportWrapper($transport);
}

$handler = new GelfHandler(new Publisher($transport), $this->level($config));
$handler = new GelfHandler(new Publisher($transport), $this->parseLevel($config));

$handler->setFormatter(
new GelfMessageFormatter(
Expand All @@ -63,7 +63,7 @@ public function __invoke(array $config): Logger
$handler->pushProcessor(new $processor);
}

return new Logger($this->parseChannel($config), [$handler]);
return new Logger($this->parseChannelName($config), [$handler]);
}

protected function parseConfig(array $config): array
Expand All @@ -89,7 +89,7 @@ protected function parseConfig(array $config): array
$config['ssl_options']['allow_self_signed'] ??= false;
}

if ($config['http_basic_auth']) {
if ($config['http_basic_auth'] && is_array($config['http_basic_auth'])) {
$config['http_basic_auth']['username'] ??= null;
$config['http_basic_auth']['password'] ??= null;
}
Expand Down Expand Up @@ -142,10 +142,10 @@ protected function sslOptions(?array $sslConfig = null): SslOptions
protected function enableBasicAuthentication(AbstractTransport $transport, array $config): bool
{
return $transport instanceof HttpTransport
&& $config['http_basic_auth']
&& $config['http_basic_auth']['username']
&& $config['http_basic_auth']['password'];

&& isset($config['http_basic_auth'])
&& is_array($config['http_basic_auth'])
&& !empty($config['http_basic_auth']['username'])
&& !empty($config['http_basic_auth']['password']);
}

protected function parseProcessors(array $config): array
Expand All @@ -163,6 +163,24 @@ protected function parseProcessors(array $config): array

protected function getFallbackChannelName(): string
{
return $this->app->bound('env') ? $this->app->environment() : 'production';
return $this->container->has('env')
? $this->container->get('env')
: 'production';
}

protected function parseLevel(array $config): Level
{
$level = $config['level'] ?? 'debug';

if ($level instanceof Level) {
return $level;
}

return Level::fromName(ucfirst(strtolower($level)));
}

protected function parseChannelName(array $config): string
{
return $config['name'] ?? $this->getFallbackChannelName();
}
}
4 changes: 3 additions & 1 deletion src/Processors/NullStringProcessor.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace Hedii\LaravelGelfLogger\Processors;
declare(strict_types=1);

namespace Nuwber\HypervelGelfLogger\Processors;

use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;
Expand Down
4 changes: 3 additions & 1 deletion src/Processors/RenameIdFieldProcessor.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace Hedii\LaravelGelfLogger\Processors;
declare(strict_types=1);

namespace Nuwber\HypervelGelfLogger\Processors;

use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;
Expand Down
4 changes: 3 additions & 1 deletion tests/Fake/AnotherTestProcessor.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace Hedii\LaravelGelfLogger\Tests\Fake;
declare(strict_types=1);

namespace Nuwber\HypervelGelfLogger\Tests\Fake;

use Monolog\LogRecord;

Expand Down
4 changes: 3 additions & 1 deletion tests/Fake/TestProcessor.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace Hedii\LaravelGelfLogger\Tests\Fake;
declare(strict_types=1);

namespace Nuwber\HypervelGelfLogger\Tests\Fake;

use Monolog\LogRecord;

Expand Down
Loading