Simple and easy way for logging in php
First, install Apache log4php.
create a configuration file named config.xml containing:
First, install Apache log4php.
create a configuration file named config.xml containing:
1.<configuration xmlns="http://logging.apache.org/log4php/"> 2. <appender name="myAppender"> 3. <param name="file" value="myLog.log" /> 4. </appender> 5. <root> 6. <level value="WARN" /> 7. <appender_ref ref="myAppender" /> 8. </root> 9.</configuration>
This configuration file does the following:
line 2: Creates an appender named myAppender using appender class LoggerAppenderFile which is used for logging to a file.
line 3: Sets the file parameter, which tells the appender to which file to write.
line 6: Sets the root logger level to WARN. This means that logging requests with the level lower than WARN will not be logged by the root logger.
line 7: Links myAppender to the root logger so that all events recieved by the root logger will be forwarded to myAppender and written into the log file.
line 3: Sets the file parameter, which tells the appender to which file to write.
line 6: Sets the root logger level to WARN. This means that logging requests with the level lower than WARN will not be logged by the root logger.
line 7: Links myAppender to the root logger so that all events recieved by the root logger will be forwarded to myAppender and written into the log file.
How to use in code
// Insert the path where you unpacked log4php
include('log4php/Logger.php');
// Tell log4php to use our configuration file.
Logger::configure('config.xml');
// Fetch a logger, it will inherit settings from the root logger
$log = Logger::getLogger('myLogger');
// Start logging
$log->trace("My first message."); // Not logged because TRACE < WARN
$log->debug("My second message."); // Not logged because DEBUG < WARN
$log->info("My third message."); // Not logged because INFO < WARN
$log->warn("My fourth message."); // Logged because WARN >= WARN
$log->error("My fifth message."); // Logged because ERROR >= WARN
$log->fatal("My sixth message."); // Logged because FATAL >= WARN
All material is taken from
http://logging.apache.org/log4php/quickstart.html
http://logging.apache.org/log4php/quickstart.html
Comments
Post a Comment