Seldaek / monolog

Sends your logs to files, sockets, inboxes, databases and various web services
https://seldaek.github.io/monolog/
MIT License
21.02k stars 1.9k forks source link

Provide a interface and class to handle records as objects #516

Closed dnaber-de closed 3 years ago

dnaber-de commented 9 years ago

While I was writing a processor recently, I stumbled over the array passed to the processors __invoke method. I was not sure about the arrays structure, so I quickly dumped it using var_dump(). It would be nice to have an object here with an interface to work against.

Another benefit of an record object would be, that processors needn't to take care about each other when more of them add extra data for example.

Here's a simple example of an record interface:

<?php # -*- coding: utf-8 -*-

namespace Monolog\Type;

interface RecordInterface {

    /**
     * @param string $name
     */
    public function setMessage( $name );

    /**
     * @return string
     */
    public function getMessage();

    /**
     * @param string $context
     */
    public function setContext( $context );

    /**
     * @return string
     */
    public function getContext();

    /**
     * @param int
     */
    public function setLevel( $level );

    /**
     * @return int
     */
    public function getLevel();

    /**
     * @param string $levelName
     */
    public function setLevelName( $levelName );

    /**
     * @return string
     */
    public function getLevelName();

    /**
     * @param string $channel
     */
    public function setChannel( $channel );

    /**
     * @return string
     */
    public function getChannel();

    /**
     * @param \DateTime $dateTime
     */
    public function setDateTime( \DateTime $dateTime );

    /**
     * @return \DateTime
     */
    public function getDateTime();

    /**
     * @param array $extra
     */
    public function setExtra( array $extra );

    /**
     * @return array
     */
    public function getExtra();
}

It might be discussable whether both setLevel() and setLevelName() are necessary as this might lead to an inconsistency of the object. However It's just an idea for an improvement.

Seldaek commented 9 years ago

This is not really feasible if only for BC reasons, although we could technically implement ArrayAccess but that'd make everything slower for no great benefit.

The concept of record is kind of the only thing to understand about monolog internals so I don't think it's too much to ask :) But I get what you are saying though and I do think it could be documented better for sure.

dnaber-de commented 9 years ago

Well, I didn't had backward compatibility in mind when I thought about that. Of course that is more important.