First, please install Log4PHP .
The log4* framework is quite flexible, e.g. you can have a all log messages of a certain importance go to one big logfile, all warnings from the database classes send via e-mail and all everything, even debug output from some other class going to a separate file. The configuration can be done using a standard property file (.ini), an XML file or an PHP array.
Before we start explaining how to configure it, we first explain some terms that are used all over the documentation:
require_once('log4php/Logger.php');
$logger = Logger::getLogger("main");
$logger->info("foo");
$logger->warn("bar");
This gives:
Sun Jul 26 01:40:23 2009,021 [10093] INFO main - foo Sun Jul 26 01:40:23 2009,030 [10093] WARN main - bar
Here is an advanced, yet still simple, log4php.properties configuration:
log4php.appender.default = LoggerAppenderEcho
log4php.appender.default.layout = LoggerLayoutSimple
log4php.rootLogger = WARN, default
log4php.logger.mylogger = INFO, default
log4php.additivity.mylogger = "false"
This configures the so called root logger at WARN level with the default appender. An additional Logger named "mylogger" is configured at INFO level. If you would give one of your loggers a name which hasn't been defined, like "main" in the below source, in your config file, the root logger is used. Once you have created such a file, you need to define it's location by setting a constant: Log4PHP will look up the configuration file and prepare the framework for logging.
require_once('log4php/Logger.php');
Logger::configure('log4php.properties');
class MyClass {
private $logger;
public function __construct() {
$this->logger = Logger::getLogger(__CLASS__);
$this->logger->debug('currently in constructor');
}
}
$logger = Logger::getLogger('main');
$logger->info('below warn and thus not printed');
new MyClass();
The output looks like this. It is very brief as we used SimpleLayout and hides one message because the default treshhold is set to WARN:
DEBUG - currently in constructor