4

I'm looking for a decent, easy to use logging helper class-set/framework.

I discovered Analog and find it to be exactly what I need, despite the fact that it seems to be usable for one logfile at a time only.

Am I wrong ?!

Do you know some similar (in size/functionality) project that allows multiple logs to be written? An Analog-Branch maybe? I had a look at log4php, KLogger and Monolog already.

Robert G.
  • 61
  • 6

2 Answers2

5

Judging from the source code at

you should be able to use several file handlers at the same time. Try something along the lines of this:

Analog::handler(Analog\Handler\Multi::init(array(
    Analog::ERROR   => Analog\Handler\File::init('/path/to/logs/errors.log'),
    Analog::WARNING => Analog\Handler\File::init('/path/to/logs/warnings.log'),
    Analog::DEBUG   => Analog\Handler\File::init('/path/to/logs/debug.log')
)));

If you cannot make it work with Analog\Handler\Multi, you can still write your own Composite Logger, adapting the Analog File Handler. To do that, first create an Interface defining how you want to use Loggers in your application:

interface Logger
{
    const ERROR = 'error';
    const WARNING = 'warning';
    const DEBUG = 'debug';

    public function log($message, $level);
}

Then create the Adapter for Analog so that it satisfies the Interface:

class AnalogAdapter implements Logger
{
    private $adaptee;

    public function __construct(Analog $analog)
    {
        $this->adaptee = $analog;
    }

    public function log($message, $level)
    {
        $adaptee = $this->adaptee;
        $adaptee::log($message, $adaptee::$level);
    }
}

Finally, write the Composite Logger:

class CompositeLogger implements Logger
{
    private $loggers = array;

    public function registerLogger(Logger $logger)
    {
        $this->loggers[] = $logger;
    }

    public function log($message, $level)
    {
        foreach ($this->loggers as $logger)
        {
            $logger->log($message, $level);
        }
    }
}

Then you create your Analog file handlers and register them with the Composite:

$logger = new CompositeLogger;
$logger->registerLogger(
    new AnalogAdapter(
        Analog\Handler\File::init('/path/to/logs/errors.log')
    )
);

// … add more Loggers in the same way

$logger->log('This is a warning', Logger::WARNING);

The warning will then get written to all the registered Loggers.

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • Whoo! Thanks for that detailed answer, i'll have closer look on that for sure. But what i realize by having a quick glance is, that is does not allow to write mulitple logfiles eiter.. o) I meant to write different log files, with different content. I'd like to have a "error.log" and a "audit.log" file, logging different things. It seems to me, i can't do this with that static implementation of Analog. – Robert G. Apr 05 '13 at 10:44
  • @RobertG. I am afraid, I don't know Analog well enough to tell. – Gordon Apr 05 '13 at 11:30
1

Yes it does work great. And you can create different log functions for different log types.

EG. This will email errors. But write warnings to a log.

Analog::handler(Analog\Handler\Multi::init(array(
    Analog::ERROR   => Analog\Handler\Buffer::init (
    Analog\Handler\Mail::init (
        'you@example.com',
        'Log messages',
        'noreply@example.com'
    )
)
    ,Analog::WARNING => Analog\Handler\File::init(__DIR__.'/log/warning.log')
    //Analog::DEBUG   => Analog\Handler\File::init('/path/to/logs/debug.log')
)));
John Ballinger
  • 7,380
  • 5
  • 41
  • 51