On 14 March 2013 09:04, Gunnar Lindholm <[email protected]> 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
