Configuration in PHP format

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Configuration in PHP format

Gunnar Lindholm
Hi.

I wonder if you could add some more complex example of a configuration
made in PHP?

It is not obvious how to write a configuration with loggers that you
give explicit names.

Thanks in advance
Gunnar.



Reply | Threaded
Open this post in threaded view
|

Re: Configuration in PHP format

Ivan Habunek
On 13 March 2013 20:49, Gunnar Lindholm
<[hidden email]> wrote:
> I wonder if you could add some more complex example of a configuration
> made in PHP?
>
> It is not obvious how to write a configuration with loggers that you
> give explicit names.

I'm aware of that, and have been meaning to write up better examples
but I haven't had much time recently to work on the project recently.
Do you have something specific that you want to accomplish? Maybe I
can help you there.

Regards,
Ivan
Reply | Threaded
Open this post in threaded view
|

Re: Configuration in PHP format

Gunnar Lindholm
2013-03-13 21:52, Ivan Habunek skrev:

> On 13 March 2013 20:49, Gunnar Lindholm
> <[hidden email]> wrote:
>> I wonder if you could add some more complex example of a configuration
>> made in PHP?
>>
>> It is not obvious how to write a configuration with loggers that you
>> give explicit names.
>
> I'm aware of that, and have been meaning to write up better examples
> but I haven't had much time recently to work on the project recently.
> Do you have something specific that you want to accomplish? Maybe I
> can help you there.


Hi.

Yes, its acctually a simple case.

I want to be able to load two different loggers

$loggerFoo = Logger::getLogger('foo');
$loggerBar = Logger::getLogger('bar');

and they should write to different files.

I have a file with rootLogger defined, but I'd like to define these two
foo/bar loggers also.

Any help would be greatly appreciated.
Thank you.

Reply | Threaded
Open this post in threaded view
|

Re: Configuration in PHP format

Ivan Habunek
On 14 March 2013 09:04, Gunnar Lindholm
<[hidden email]> wrote:

> Yes, its acctually a simple case.
>
> I want to be able to load two different loggers
>
> $loggerFoo = Logger::getLogger('foo');
> $loggerBar = Logger::getLogger('bar');
>
> and they should write to different files.
>
> I have a file with rootLogger defined, but I'd like to define these two
> foo/bar loggers also.
>
> Any help would be greatly appreciated.
> Thank you.
>

Hi Gunnar,

Check out this config file:

<configuration xmlns="http://logging.apache.org/log4php/">
    <appender name="app1" class="LoggerAppenderFile">
        <param name="file" value="foo.log" />
    </appender>

    <appender name="app2" class="LoggerAppenderFile">
        <param name="file" value="bar.log" />
    </appender>

    <logger name="logger1">
        <appender_ref ref="app1" />
    </logger>

    <logger name="logger2">
        <appender_ref ref="app2" />
    </logger>
</configuration>


First, there's 2 appenders, each logs to it's own file:
- appender "app1" logs to foo.log
- appender "app2" logs to bar.log

Then I set up 2 loggers:
- logger "logger1" is linked to appender "app1", and therefore will
log to foo.log
- logger "logger2" is linked to appender "app2", and therefore will
log to bar.log

Try it out by running something like:
Logger::getLogger('logger1')->info('this is logger1');
Logger::getLogger('logger2')->info('this is logger2');

Note that in this configuration the root logger is not linked to any
appender. Since loggers inherit appenders from the root logger, any
loggers apart from logger1 and logger2 will not log anything since
they have no appenders of their own, and no inherited appenders.

To fix that, configure the root logger to log somewhere (for example
file "baz.log"). Here's the new config:

<configuration xmlns="http://logging.apache.org/log4php/">
    <appender name="app1" class="LoggerAppenderFile">
        <param name="file" value="foo.log" />
    </appender>

    <appender name="app2" class="LoggerAppenderFile">
        <param name="file" value="bar.log" />
    </appender>

    <appender name="app3" class="LoggerAppenderFile">
        <param name="file" value="baz.log" />
    </appender>

    <logger name="logger1" additivity="false">
        <appender_ref ref="app1" />
    </logger>

    <logger name="logger2">
        <appender_ref ref="app2" />
    </logger>

    <root>
        <appender_ref ref="app3" />
    </root>
</configuration>

It adds a new appender to log to baz.log and links it to the root logger.

Note that logger1 and logger2 will also inherit app3 from the root
logger, which means they will also log to baz.log, as well as their
own file (foo.log or bar.log). You can prevent that by turning off
their additivity, like this:

<logger name="logger1" additivity="false">
    <appender_ref ref="app1" />
</logger>

Try it out, let me know if it solves your problem or if you have any
further questions.

Regards,
Ivan