ytake / Laravel-FluentLogger

fluent logger for laravel (with Monolog handler for Fluentd)
MIT License
63 stars 23 forks source link

Added option to configure FluentHandler #26

Closed duxthefux closed 6 years ago

duxthefux commented 6 years ago

This merge request introduces configurable FluentHandler-class in fluent.php config. This brings more flexibility when it comes to preparing data.

For example we wanted to have more control about what is sent to our logging instance. JsonPacker was not enough, as you can't access the raw-$record["context"].

We created a class which extends the stock \Ytake\LaravelFluent\FluentHandler.

class FluentHandler extends \Ytake\LaravelFluent\FluentHandler
{
    protected function write(array $record)
    {
        $tag = $this->populateTag($record);

        $this->logger->post(
            $tag,
            [
                'message' => $record['message'],
                'level' => $record['level'],
                'level_name' => $record['level_name'],
                'context' => $this->getContext($record['context']),
                'extra' => $record['extra'],
            ]
        );
    }

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

        return $context;
    }

    protected function getContextException(array $context): array
    {
        return [
            'exceptionTrace' => $context['exception']->getTraceAsString(),
            'exceptionClass' => get_class($context['exception']),
        ];
    }
}

and added that to our fluent.php-config

...
'handler' => FluentHandler::class,
...
duxthefux commented 6 years ago

@ytake tests work on my machine, but fail in CI. Any idea?

duxthefux commented 6 years ago

ping

ytake commented 6 years ago

I will try. thanks

ytake commented 6 years ago

@duxthefux https://github.com/ytake/Laravel-FluentLogger/releases/tag/3.0.1

duxthefux commented 6 years ago

great thanks!