views:

224

answers:

1

I have a PHP application containing multiple modules and various settings within each module. I'd like to enable logging using log4php, but each individual module has different settings (ie. Log level, log file, etc). Some of these settings need to be changed on the fly by end users (log level), and I'd like to store these properties in the database.

Right now I can set loglevel dynamically. I can not determine how to set other properties dynamically though. Documentation on the configuration of log4php is almost exclusively done via properties/xml files - though it says all settings can be changed programmatically.

Has any done something like this? If so, how do I do so? I've looked through the documentation at http://incubator.apache.org/log4php/index.html, but I need more assistance.

To clarify - I do NOT want to use configuration files, as the end users will not have direct access to these. I will provide a front end to change certain logging settings, and all settings will be stored in the database.

A: 

Looking at the API Docs tells me they are using Config Adapters and a Strategy Pattern to configure the loggers. The adapters have a single method configure, which is called by the main Logger. There is a LoggerConfiguratorPhp, which will read/include a PHP file returning a regular PHP array. I could imagine this could easily be modified to accept an array returned from the database instead. Have a look at the source code of this class for starting point. What you will want to create is a LoggerConfiguratorDB, which you can then pass to the main Logger class like this:

$configurator = dirname(__FILE__).'/../resources/configurator_db.php';
Logger::configure( $configurator, 'LoggerConfiguratorDb');

I never worked with Log4Php and this is just assuming by looking at the API. I have absolutely no clue, how the new adapter would have to look like. But maybe this gets you into the right direction.

If you manage to come up with something why not donate it to the project :)

Gordon