Skip to content

Commit

Permalink
supported Laravel5.6
Browse files Browse the repository at this point in the history
  • Loading branch information
ytake committed Feb 17, 2018
1 parent 69d7862 commit 4c53e3a
Show file tree
Hide file tree
Showing 15 changed files with 287 additions and 195 deletions.
1 change: 0 additions & 1 deletion .travis.yml
@@ -1,7 +1,6 @@
sudo: false
language: php
php:
- 7
- 7.1
- 7.2
before_script:
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015-2017 Yuuki Takezawa
Copyright (c) 2015-2018 Yuuki Takezawa

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
82 changes: 75 additions & 7 deletions README.md
Expand Up @@ -29,10 +29,12 @@ or composer.json

```json
"require": {
"ytake/laravel-fluent-logger": "^2.0"
"ytake/laravel-fluent-logger": "^3.0"
},
```

**Supported Auto-Discovery(^Laravel5.5)**

## for laravel
your config/app.php
```php
Expand Down Expand Up @@ -61,15 +63,81 @@ $ php artisan vendor:publish --tag=log
$ php artisan vendor:publish --provider="Ytake\LaravelFluent\LogServiceProvider"
```

### Always Added Push Fluentd Handler


### Config

edit config/fluent.php
```php
/**
* always added fluentd log handler
* example. true => daily and fluentd
*/
'always' => true,
return [

'host' => env('FLUENTD_HOST', '127.0.0.1'),

'port' => env('FLUENTD_PORT', 24224),

/** @see https://github.com/fluent/fluent-logger-php/blob/master/src/FluentLogger.php */
'options' => [],

/** @see https://github.com/fluent/fluent-logger-php/blob/master/src/PackerInterface.php */
// specified class name
'packer' => null,

'tagFormat' => '{{channel}}.{{level_name}}',
];

```

added config/logging.php

```php
return [
'channels' => [
'stack' => [
'driver' => 'stack',
// always added fluentd log handler
// 'channels' => ['single', 'fluent'],
// fluentd only
'channels' => ['fluent'],
],

'fluent' => [
'driver' => 'fluent',
'level' => 'debug',
],

'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
],

'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
'days' => 7,
],

'slack' => [
'driver' => 'slack',
'url' => env('LOG_SLACK_WEBHOOK_URL'),
'username' => 'Laravel Log',
'emoji' => ':boom:',
'level' => 'critical',
],

'syslog' => [
'driver' => 'syslog',
'level' => 'debug',
],

'errorlog' => [
'driver' => 'errorlog',
'level' => 'debug',
],
],
];

```

### All logs to fluentd
Expand Down
17 changes: 9 additions & 8 deletions composer.json
Expand Up @@ -17,18 +17,19 @@
}
],
"require": {
"php": "^7.0",
"php": "^7.1.3",
"fluent/logger": "^1.0",
"illuminate/log": "^5.5",
"illuminate/support": "5.5.*",
"illuminate/config": "5.5.*",
"illuminate/contracts": "5.5.*",
"illuminate/container": "5.5.*"
"illuminate/log": "^5.6",
"illuminate/support": "^5.6",
"illuminate/config": "^5.6",
"illuminate/contracts": "^5.6",
"illuminate/container": "^5.6",
"illuminate/events": "^5.6"
},
"require-dev": {
"phpunit/phpunit": "^6.0",
"phpunit/phpunit": "~7.0",
"satooshi/php-coveralls": "*",
"illuminate/filesystem": "^5.5"
"illuminate/filesystem": "^5.6"
},
"autoload": {
"psr-4": {
Expand Down
16 changes: 12 additions & 4 deletions src/FluentHandler.php
Expand Up @@ -13,7 +13,7 @@
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*
* Copyright (c) 2015-2017 Yuuki Takezawa
* Copyright (c) 2015-2018 Yuuki Takezawa
*
*/

Expand Down Expand Up @@ -103,40 +103,48 @@ protected function processFormat(array $record, string $tag): string

/**
* returns the context
* @return array | string
*
* @param mixed $context
*
* @return array|string
*/
protected function getContext($context)
{
if ($this->contextHasException($context)) {
return $this->getContextExceptionTrace($context);
}

return $context;
}

/**
* Identifies the content type of the given $context
*
* @param mixed $context
*
* @return bool
*/
protected function contextHasException($context): bool
{
return (
is_array($context)
is_array($context)
&& array_key_exists('exception', $context)
&& $context['exception'] instanceof \Exception
);
}

/**
* Returns the entire exception trace as a string
*
* @param array $context
*
* @return string
*/
protected function getContextExceptionTrace(array $context): string
{
return $context['exception']->getTraceAsString();
}

/**
* @return LoggerInterface
*/
Expand Down
66 changes: 66 additions & 0 deletions src/LogManager.php
@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);

/**
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*
* Copyright (c) 2015-2018 Yuuki Takezawa
*
*/

namespace Ytake\LaravelFluent;

use Fluent\Logger\FluentLogger;
use Monolog\Logger as Monolog;
use Psr\Log\LoggerInterface;

/**
* Class LogManager
*/
class LogManager extends \Illuminate\Log\LogManager
{
/**
* @var \Illuminate\Contracts\Container\Container
*/
protected $app;

/**
* @param array $config
*
* @return LoggerInterface
*/
protected function createFluentDriver(array $config): LoggerInterface
{
$configure = $this->app['config']['fluent'];
$packer = null;
if (!is_null($configure['packer'])) {
if (class_exists($configure['packer'])) {
$packer = $this->app->make($configure['packer']);
}
}

return new Monolog($this->parseChannel($config), [
$this->prepareHandler(
new FluentHandler(
new FluentLogger(
$configure['host'] ?? FluentLogger::DEFAULT_ADDRESS,
(int)$configure['port'] ?? FluentLogger::DEFAULT_LISTEN_PORT,
$configure['options'] ?? [],
$packer
),
$configure['tagFormat'] ?? null,
$this->level($config)
)
),
]);
}
}
49 changes: 4 additions & 45 deletions src/LogServiceProvider.php
Expand Up @@ -13,16 +13,12 @@
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*
* Copyright (c) 2015-2017 Yuuki Takezawa
* Copyright (c) 2015-2018 Yuuki Takezawa
*
*/

namespace Ytake\LaravelFluent;

use Fluent\Logger\FluentLogger;
use Monolog\Logger as Monolog;
use Psr\Log\LoggerInterface;

/**
* Class LogServiceProvider
*/
Expand All @@ -39,45 +35,8 @@ public function register()
$configPath = __DIR__ . '/config/fluent.php';
$this->mergeConfigFrom($configPath, 'fluent');
$this->publishes([$configPath => config_path('fluent.php')], 'log');
parent::register();

$configure = $this->app['config']->get('fluent');
if ($configure['always']) {
$this->configureFluentHandler($this->app->make(LoggerInterface::class));
}
}

/**
* {@inheritdoc}
*/
public function createLogger()
{
$log = new Writer(new Monolog($this->channel()), $this->app['events']);
if ($this->app->hasMonologConfigurator()) {
call_user_func($this->app->getMonologConfigurator(), $log->getMonolog());
} else {
$this->configureHandler($log);
}

return $log;
}

/**
* @param Writer $log
*/
protected function configureFluentHandler(Writer $log)
{
$configure = $this->app['config']->get('fluent');
if (!is_null($configure['packer'])) {
if (class_exists($configure['packer'])) {
$log->setPacker($this->app->make($configure['packer']));
}
}
$log->useFluentLogger(
$configure['host'] ?? FluentLogger::DEFAULT_ADDRESS,
(int)$configure['port'] ?? FluentLogger::DEFAULT_LISTEN_PORT,
$configure['options'] ?? [],
$configure['tagFormat'] ?? null
);
$this->app->singleton('log', function ($app) {
return new LogManager($app);
});
}
}

0 comments on commit 4c53e3a

Please sign in to comment.