Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Array to string conversion in Zend\Log\Writer\Db #7244

Closed
Martin-P opened this issue Feb 21, 2015 · 5 comments
Closed

Array to string conversion in Zend\Log\Writer\Db #7244

Martin-P opened this issue Feb 21, 2015 · 5 comments

Comments

@Martin-P
Copy link
Contributor

This was already reported in #2589, but that issue got closed 11 days ago (#2589 (comment)) with references to:

I tried to get it reopened, but without any success.

Testcase which shows the problem:

/**
 * @group 2589
 */
public function testMapEventIntoColumnDoesNotTriggerArrayToStringConversion()
{
    $this->writer = new DbWriter($this->db, $this->tableName, array(
        'priority' => 'new-priority-field',
        'message'  => 'new-message-field' ,
        'extra'    => array(
            'file'  => 'new-file',
            'line'  => 'new-line',
            'trace' => 'new-trace',
        )
    ));

    // log to the mock db adapter
    $priority = 2;
    $message  = 'message-to-log';
    $extra    = array(
        'file'  => 'test.php',
        'line'  => 1,
        'trace' => array(
            array(
                'function' => 'Bar',
                'class'    => 'Foo',
                'type'     => '->',
                'args'     => array(
                    'baz',
                ),
            ),
        ),
    );
    $this->writer->write(array(
        'priority' => $priority,
        'message'  => $message,
        'extra'    => $extra,
    ));

    $this->assertContains('query', array_keys($this->db->calls));
    $this->assertEquals(1, count($this->db->calls['query']));

    foreach ($this->db->calls['execute'][0][0] as $fieldName => $fieldValue) {
        $this->assertInternalType('string', $fieldName);
        $this->assertInternalType('string', (string) $fieldValue);
    }
}

Result:

  1. ZendTest\Log\Writer\DbTest::testMapEventIntoColumnDoesNotTriggerArrayToStringConversion
    Array to string conversion
@weierophinney
Copy link
Member

@Martin-P Are you working on a PR for this, by any chance?

@Martin-P
Copy link
Contributor Author

@weierophinney
Looking at it right now, but there are some issues:

  • serialize(): not an option, because it can't handle closures
  • var_export(): not an option, because it will create a huge string which leads to situations as in Undefined indexes and huge args-data in Zend\Log\Formatter\ExceptionHandler::format #2592 (every class with all its properties is exported)
  • print_r(): not an option, because it also creates a huge string where all (nested) classes and properties are exported
  • json_encode(): works, but objects are stored as stdClass and this causes loss of data

I think a custom routine is the best approach to do this. The main issues are with the depth of the array and size due to possible objects inside the array. I will create a PR for this with a suggested fix.

@Martin-P
Copy link
Contributor Author

I think I will go for var_export() after all. My use case which creates a huge trace is when Zend\Log\Writer\Db has to save an Exception, but I think it is better to format the Exception correctly in Zend\Log\Formatter\ExceptionHandler.

@Martin-P
Copy link
Contributor Author

I created #7282 for this.

weierophinney added a commit that referenced this issue Mar 10, 2015
@weierophinney
Copy link
Member

Fixed with #7282.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants