Code Monkey home page Code Monkey logo

fluent-logger-php's Introduction

Fluent Logger PHP

fluent-logger-php is a PHP library to record events to fluentd from a PHP application.

Build Status

Requirements

  • PHP 5.6 or higher
  • fluentd v0.9.20 or higher

Installation

Using Composer

composer.json

{
    "require": {
        "fluent/logger": "v1.0.0"
    }
}

Backward Compatibility Changes

As of v1, all loggers but FluentLogger are removed.

Monolog is recommended in such use cases.

Usage

PHP side

<?php

require_once __DIR__.'/vendor/autoload.php';

use Fluent\Logger\FluentLogger;
$logger = new FluentLogger("localhost","24224");
$logger->post("debug.test",array("hello"=>"world"));

Fluentd side

Use in_forward.

<source>
  @type forward
</source>

Todos

  • Stabilize method signatures.
  • Improve performance and reliability.

Restrictions

  • Buffering and re-send support

PHP does not have threads. So, I strongaly recommend you use fluentd as a local fluent proxy.

apache2(mod_php)
fluent-logger-php
                 `-----proxy-fluentd
                                    `------aggregator fluentd

License

Apache License, Version 2.0

Contributors

  • Daniele Alessandri
  • Hiro Yoshikawa
  • Kazuki Ohta
  • Shuhei Tanuma
  • Sotaro KARASAWA
  • edy
  • kiyoto
  • sasezaki
  • satokoma
  • DQNEO

fluent-logger-php's People

Contributors

alewex avatar alexmanno avatar brianlmoon avatar byplayer avatar chobid avatar chobie avatar dqneo avatar fvovan avatar hashbrowncipher avatar hiroyoshikawa avatar kzk avatar nrk avatar nyholm avatar repeatedly avatar s-edy avatar sasezaki avatar satokoma avatar travail avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fluent-logger-php's Issues

'message' => 'fwrite(): send of 123 bytes failed with errno=110 Connexion terminée par expiration du délai d\'attente',

Hi,

I'm encountering following error message several times:

unhandled error detected. please report this issue to http://github.com/fluent/fluent-logger-php/issues: array (
'type' => 8,
'message' => 'fwrite(): send of 123 bytes failed with errno=110 Connexion terminée par expiration du délai d'attente',
'file' => '/home/production/fr/libPartage/FluentLogger/FluentLogger.php',
'line' => 432,
)

Connexion terminée par expiration du délai d'attente means "connection terminated by timeout expiration"

Is there a way to deal with this kind of error? Setting connection retries?

Thanks

more specifically fwrite error handling and improvement for Fluent\Logger\FluentLogger::postImpl.

  • fwrite returns:
FALSE => invalid parameters
integer => number of bytes written

why i misunderstood fwrite return null string. o.O

  • fwrite outputs errors:

only 1. we should catch this as PHP is able to change errors to exception.

php_sockop_write:
  php_error_docref(NULL TSRMLS_CC, E_NOTICE, "send of %ld bytes failed with errno=%ld %s",(long)count, err, estr);
  • trace
fwrite -> php_stream_write -> _php_stream_write_buffer -> php_sockop_write

also i concerned about retry policy.

following lines are excerpted codes from PHP5_3.

/* {{{ proto int fwrite(resource fp, string str [, int length])
   Binary-safe file write */
PHPAPI PHP_FUNCTION(fwrite)
{
    zval *arg1;
    char *arg2;
    int arg2len;
    int ret;
    int num_bytes;
    long arg3 = 0;
    char *buffer = NULL;
    php_stream *stream;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &arg1, &arg2, &arg2len, &arg3) == FAILURE) {
        RETURN_FALSE;
    }

    if (ZEND_NUM_ARGS() == 2) {
        num_bytes = arg2len;
    } else {
        num_bytes = MAX(0, MIN((int)arg3, arg2len));
    }

    if (!num_bytes) {
        RETURN_LONG(0);
    }

    PHP_STREAM_TO_ZVAL(stream, &arg1);

    if (PG(magic_quotes_runtime)) {
        buffer = estrndup(arg2, num_bytes);
        php_stripslashes(buffer, &num_bytes TSRMLS_CC);
    }

    ret = php_stream_write(stream, buffer ? buffer : arg2, num_bytes);
    if (buffer) {
        efree(buffer);
    }

    RETURN_LONG(ret);
}
/* }}} */

PHPAPI size_t _php_stream_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
{
    if (buf == NULL || count == 0 || stream->ops->write == NULL) {
        return 0;
    }

    if (stream->writefilters.head) {
        return _php_stream_write_filtered(stream, buf, count, PSFS_FLAG_NORMAL TSRMLS_CC);
    } else {
        return _php_stream_write_buffer(stream, buf, count TSRMLS_CC);
    }
}

/* Writes a buffer directly to a stream, using multiple of the chunk size */
static size_t _php_stream_write_buffer(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
{
    size_t didwrite = 0, towrite, justwrote;

    /* if we have a seekable stream we need to ensure that data is written at the
     * current stream->position. This means invalidating the read buffer and then
     * performing a low-level seek */
    if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0 && stream->readpos != stream->writepos) {
        stream->readpos = stream->writepos = 0;

        stream->ops->seek(stream, stream->position, SEEK_SET, &stream->position TSRMLS_CC);
    }


    while (count > 0) {
        towrite = count;
        if (towrite > stream->chunk_size)
            towrite = stream->chunk_size;

        justwrote = stream->ops->write(stream, buf, towrite TSRMLS_CC);

        /* convert justwrote to an integer, since normally it is unsigned */
        if ((int)justwrote > 0) {
            buf += justwrote;
            count -= justwrote;
            didwrite += justwrote;

            /* Only screw with the buffer if we can seek, otherwise we lose data
             * buffered from fifos and sockets */
            if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) {
                stream->position += justwrote;
            }
        } else {
            break;
        }
    }
    return didwrite;

}


main/stream/xp_socket.c

php_sockop_write
/* {{{ Generic socket stream operations */
static size_t php_sockop_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
{
    php_netstream_data_t *sock = (php_netstream_data_t*)stream->abstract;
    int didwrite;
    struct timeval *ptimeout;

    if (sock->socket == -1) {
        return 0;
    }

    if (sock->timeout.tv_sec == -1)
        ptimeout = NULL;
    else
        ptimeout = &sock->timeout;

retry:
    didwrite = send(sock->socket, buf, count, (sock->is_blocked && ptimeout) ? MSG_DONTWAIT : 0);

    if (didwrite <= 0) {
        long err = php_socket_errno();
        char *estr;

        if (sock->is_blocked && err == EWOULDBLOCK) {
            int retval;

            sock->timeout_event = 0;

            do {
                retval = php_pollfd_for(sock->socket, POLLOUT, ptimeout);

                if (retval == 0) {
                    sock->timeout_event = 1;
                    break;
                }

                if (retval > 0) {
                    /* writable now; retry */
                    goto retry;
                }

                err = php_socket_errno();
            } while (err == EINTR);
        }
        estr = php_socket_strerror(err, NULL, 0);
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "send of %ld bytes failed with errno=%ld %s",
                (long)count, err, estr);
        efree(estr);
    }

    if (didwrite > 0) {
        php_stream_notify_progress_increment(stream->context, didwrite, 0);
    }

    if (didwrite < 0) {
        didwrite = 0;
    }

    return didwrite;
}

#define php_stream_notify_progress_increment(context, dsofar, dmax) do { if ((context) && (context)->notifier && (context)->notifier->mask & PHP_STREAM_NOTIFIER_PROGRESS) { \
    (context)->notifier->progress += (dsofar); \
    (context)->notifier->progress_max += (dmax); \
    php_stream_notify_progress((context), (context)->notifier->progress, (context)->notifier->progress_max); } } while (0)

#define php_stream_notify_progress(context, bsofar, bmax) do { if ((context) && (context)->notifier) { \
    php_stream_notification_notify((context), PHP_STREAM_NOTIFY_PROGRESS, PHP_STREAM_NOTIFY_SEVERITY_INFO, \
            NULL, 0, (bsofar), (bmax), NULL TSRMLS_CC); } } while(0)

PHPAPI void php_stream_notification_notify(php_stream_context *context, int notifycode, int severity,
        char *xmsg, int xcode, size_t bytes_sofar, size_t bytes_max, void * ptr TSRMLS_DC)
{
    if (context && context->notifier)
        context->notifier->func(context, notifycode, severity, xmsg, xcode, bytes_sofar, bytes_max, ptr TSRMLS_CC);
}

Fluentd TCP Input not working

Hello,
I have plan using Fluentd with PHP 5.6.20 on my system.
The problem I have is that : PHP use fluent-logger-php but can not push message to FLuentd TCP Input .

Here is my Fluentd Config :

@type tcp tag tcp.events # required format json port 20001 # optional. 5170 by default bind 0.0.0.0 # optional. 0.0.0.0 by default log_level trace

<match **>
@type stdout

And I used the your sample code .

post("tcp.events",array("hello"=>"world")); ?>

BUT there is nothing in the output except fluent trace :

2016-04-14 10:59:32 +0700 [trace]: accepted fluent socket object_id=70064413431380
2016-04-14 10:59:32 +0700 [trace]: closed fluent socket object_id=70064413431380
2016-04-14 10:59:32 +0700 fluent.trace: {"message":"accepted fluent socket object_id=70064413431380"}
2016-04-14 10:59:32 +0700 fluent.trace: {"message":"closed fluent socket object_id=70064413431380"}

Could you please show me how to fix it.
Thank you very much.

P.S: It works well with Unix Socket.

Propose to abandon other loggers than FluentLogger

First, thank you for creating great softwares Fluentd and fluent-logger-php.
They are both amazing and helped us to improve our web infrastructures a lot.

Well, I hear Fluentd is a kind of Unix pipe on the web infrastructure and the design looks to be influenced by the Unix philosophy. Do one thing and do it well

But this project looks to have 2 roles.

  • a logger to send logs to a Fluentd daemon
  • a bridge to send logs to multiple facilities (File, HTTP, Console)

As for the second function, Monolog has become a de-facto standard in the modern PHP world.

If we focused on the first role, we could remove many class files and make the project much simpler, thinner, and more readable.
For example, see the perl counterpart fluent-logger-perl. It has only one pm file so it's very easy to read.

So my proposition is to focus on sending logs only to Fluentd and forget about File ,HTTP, Console etc.
As a reference implementation, I created a project called fluent-simplelogger
https://github.com/DQNEO/php-fluent-simplelogger/tree/master/src

Pros

  • easy to read and understand
  • easy to maintain
  • less bugs, less issues

Cons

  • breaks backward compatibility ( requires a major version change)

If you are welcome to accept it , I will send a Pull Request.

Thanks again for such a nice library.

Seeking New Maintainers for this repository

Call for New Maintainers

Hello Community,

First of all, we apologize for recent inactivity.
We (@travail and I ) have decided to step down as maintainers. As a matter of fact, we no longer have the capacity to give this library the attention it deserves.
Given this, we are seeking new maintainers who are passionate about the project and want to lead its future development.

Ideal Candidate:

  • Familiarity with the library's codebase and the language/framework it uses.
  • Some experience with open source projects (either as a contributor or maintainer).

If you believe you could serve as a passionate and dedicated maintainer, please respond to this issue.

Thank you for your contributions and support so far!

Error Reported

Hi I got this error and i am posting it here as requested,
'type' => 8,
'message' => 'fwrite(): send of 558 bytes failed with errno=104 Connection reset by peer',
'file' => 'XXX',
'line' => 466,

This is the Match part at the td_agent.conf:

@type bigquery num_threads 4 flush_interval 30 buffer_queue_limit 10240 buffer_chunk_records_limit 300 auth_method json_key # default email XXX json_key XXX # private_key_passphrase notasecret # default project XXXX dataset XXXX_logs fetch_schema false auto_create_table true table logs_%Y%m%d schema_path /logs/path/logs.json

testing matrix

Basically, fluent-logger-php uses under apache2, fcgi or php-fpm.
so test cases should do those environment and also it should have acceptance test.

now, I have a mac mini server and UPS so I can construct it.
I'll do this in end of the month.

Support for microsecond resolution in timestamp

The current timestamp is enforced to be an integer, thus leading to a maximum time resolution of 1 second. Even when passed as an argument in the Entity constructor, it is validated on an integer (long) type.

Adding support for float values in the constructor would help increase the log timestamp accuracy.

Losing logs + lots of lines in stderr (error.log nginx)

Hi,
We have a problem with the fluentd-logger-php class. Sometimes it goes in error (we haven't realize yet why, but we think it could be a socket connection error) and it writes in stderr a blob of things:

  1. It writes what we are trying to send to fluentd (application logs)
  2. Plus, it writes the php errors collected during the script, like notices for example (and our error level configured in php.ini file excludes the notices, so it is strange).
    Writing in stderr causes us a lot of noise in the nginx error.log, and, furthermore, we loose the application logs forever.
    The fluentd, even if is in debug mode, doesn't write anything when this problem happens.
    We experimentally caused a socket connection error modifying the library code (in development environment) and we got the problematic behaviour (wrote lines in error.log nginx). This is why we believe that this could be the problem.

Now, we want to understand some things:

  1. we would like to know precisely why this is happening and how we can detect the exact problem.
  2. we cannot have these lines of logs wrote in stderr, because in this way we cannot navigate the nginx logs to analyze them easily.
  3. we would like to not loose the log, but have something to retry the writing if something goes wrong, is this possible?

Thanks,
Gioia

Breaking MongoDate

hello
using fluent-logger-php and encountering the following issue when using it with mongodb:

php:

$a = new \MongoDate();
$logger->post('someTag', ['a' => $a]);

yields:

    "a" : {
        "sec" : 1418898182,
        "usec" : 0
    }

Object type is lost.
Should it not be something like {"a" : ISODate("2014-12-18T10:23:03Z")} ?

thank you for the work you've done, cheers

Monolog and/or PSR-3

Hey guys! In the spirit of being able to integrate this with my project already using Monolog and handing out PSR-3 LoggerInterface's, don't you think it would be cool to add a fluentd handler for those things?

WARNING: Trying to access array offset on value of type null

When submit a new request to a offline fluent server, got an exception but that exception never show an error message becouse on file
vendor/fluent/logger/src/FluentLogger.php:328 the function $errors = error_get_last(); doesn't work and the array is NULL

i think is better to fix and show the right error message

just now i've fixed with
if(empty($errors)) {
$errors['message'] = "Unable to connect to the server";
}

if ok i will commit the fix

This Runs Slower than just running cURL to post logs to the HTTP Listener

Benchmarking on 50 users with a 100 loop count, I get far worse results using this vs cURL. Whereas cURL consistently gives me response times of around 10ms total load time per page on a page where not much is happening, using fluent-logger-php instead immediately increases average load times to about 600ms (all goes well with 10ms response times until about the 200th sample, at which point response times erratically grow into the thousands of ms. I'm guessing this is a buffering issue?

adjust FluentLogger::post signature to other one.

Currently, that method signature is not easy to use for Application.

FluentLogger::post(array $data[, string $additional_tag])

this should be following signature.

FluentLogger::post(string $tag, array $data)

i know this breaks current fluent-logger-php API. so i'll make development branch and fix on it.

Seems like the post() method of FluentLogger can not handle numeric arrays

This problem was costing me some time to find that only string indicies in the array are allowed:

/**
     * send a message to specified fluentd.
     *
     * @param string $tag
     * @param array  $data
     * @return bool
     *
     * @api
     */
    public function post($tag, array $data)
    {
        $entity = new Entity($tag, $data);

        return $this->postImpl($entity);
    }

I do now know the exact reason for this behaviour, but can you please just apply PHPs strval() function for posted arrays with numeric indicies?

If this problem does not result from the library (I am not 100% sure) please sorry for bothering the wrong implementors ;-)

Feature Request - batch logging in FluentLogger->post() function

Hello,
I want to be able to write batch logs (50 items) atomically.
Currently, I'm posting logs in foreach loop in batch requests. However, if td agent stops unexpectedly, I need to write the complete request(50 items) to csv. Is it possible in the current code base ? Does fluentd interface support batch logging ?

Thanks
Aras

unable to connect to tcp://localhost:24224

I'm trying send logs from PHP to Fluentd, but it's failing and i have no idea what is happening.

Let's see some code.

Versions
PHP 7.0
Fluentd 1.10-1

Fluentd config file

<source>
  @type forward
  port 24224
  bind 0.0.0.0
</source>
<match *.**>
    @type stdout
</match>

PHP function

<?php
require_once __DIR__.'/vendor/autoload.php';
use Fluent\Logger\FluentLogger;
$logger = new FluentLogger("localhost", "24224");
$logger->post("debug.test", array("hello"=>"world"));

So, when I execute the code, this error happens:
Fluent\Logger\FluentLogger stream_socket_client(): unable to connect to tcp://localhost:24224 (Cannot assign requested address) debug.test: { "hello":"world"}

adjust interface name

Currently, interface Fluent\Logger and Fluent\Logger\PackerInterface uses different naming rule.
for now, we should adjust Fluent\Logger to Fluent\Logger\LoggerInterface.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.