log4php

Log4php naming loggers based on namespace and logging to specific files


I'm using log4php and I have basically wrapped it so when any of my classes call Logger they also initalise it with getLogger and name the logger based on the namespace of the calling class

e.g.

namespace my\class\space

Logger::GetInstance(NAME_SPACE)

will create a logger with the name my\class\space (it actually gets prefixed as well inside my wrapper to something like instance\my\class\space).

How do I write a log4php config to dump all log files to the default (root) logger but then specify other loggers based on the certain namespaces I'm interested in.

e.g. I would like to do something like

<logger name="instance\my\class\space">

and that logs those class and sub class messages to a different file to everything else.

Any ideas?

Thanks

UPDATE

<logger name="instance\my\class\space" additivity="false">

will log my instance\my\class\space to a different file, but it is not including sub packages e.g.

instance\my\class\space\sub is still being logged in root. I thought log4j automatically handled sub packages, does log4php not support this?


Solution

  • Ok. so it looks as if log4php does support sub classes but you use the log4j dot package separator notation and not the php namespace separator notation.

    i.e.

    instance\my\class\space is the php namespace

    but log4php seems to need that converting into a java package namespace of

    instance.my.class.space

    then log4php will automatically read sub classes of any sub named loggers such as

    instance.my.class.space.sub

    without adding any more lines to the config file