PHP Logger
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Logger.php
README.md
test.php

README.md

log4php

PHP Logger

$logger = Logger::getLogger(__CLASS__, $options);

where $options is an associative array of options, below are some possible options, if $options is null it inherits default parameters, Logging Level INFO, and writes to /tmp/output.log


$options = array(
   'log_level' => 'INFO',
   'handler' => new LogFileHandler()
);

// you can
$options = array(
   'log_level' => 'TRACE',
   'handlers' => array(
   	new LogFileHandler('/tmp/app/output.log'), 	// write to specific log file
   	new LogConsoleHandler(), 			// echo out log message
   	new LogApacheErrorLogHandler(),			// write to apache error log
   	new LogSQLHandler($db)				// write to DB, default version uses PDO
   	)
);

Log Levels

FATALA serious, and unrecoverable error occured
ERRORA serious error, i.e. a DB call failed, but perhaps is recoverable
WARNNotify of potential bugs, that while don't break the site, could result in undefined behavior
INFOGeneral level, default logging level
DEBUGDetailed code primarily used to help debug general problems
TRACEVery fine tuned in depth messages

Currently Log Formatters must be set at the handler level.

%LLog Level
%TTime
%RResource
%MMessage
%OObject
```php $formatter = new LogFormatter("%L [%T] %R %M %O"); $formatter->setDateFormat('Y-m-d H:i:s');

$handler = new LogFileHandler('/tmp/app/output.log'); $handler->setFormatter($formatter);

By Default they inherit `LogDefaultFormatter`
 

Sample Usage
```php
// A simple Logger with default Formatter and a Default LogFileHandler (prints out to /tmp/output.log
         
$logger = Logger::getLogger(__CLASS__, array("log_level" => Logger::TRACE));

$logger->trace("A Trace Statement (Level INFO)");
$logger->debug("A Debug Statement (Level INFO)");
$logger->info("An Info level msg (Level INFO)");
$logger->warn("A Warning Occured (Level INFO)");
$logger->error("An Error Occured (Level INFO)");
$logger->fatal("A Fatal Error occured (Level INFO)");
        
$logger->setLevel(Logger::TRACE);
$logger->trace("A Trace Statement (Level TRACE)");
$logger->debug("A Debug Statement (Level TRACE)");
$logger->info("An Info level msg (Level TRACE)", $this);
$logger->warn("A Warning Occured (Level TRACE)");
$logger->error("An Error Occured (Level TRACE)");
$logger->fatal("A Fatal Error occured (Level TRACE)");

$logger->setLevel(Logger::FATAL);
$logger->trace("A Trace Statement (Level FATAL)");
$logger->debug("A Debug Statement (Level FATAL)");
$logger->info("An Info level msg (Level FATAL)");
$logger->warn("A Warning Occured (Level FATAL)", $this);
$logger->error("An Error Occured (Level FATAL)");
$logger->fatal("A Fatal Error occured (Level FATAL)");

if($logger->isDebugEnabled()) {
     $logger->debug("First Debug Statement (shouldn't be printed)");
}
$logger->setLevel(Logger::DEBUG);
if($logger->isDebugEnabled()) {
  	$logger->debug("Second Debug Statement (should be printed)");
}
if($logger->isTraceEnabled()) {
	$logger->trace("First Trace Statement (shouldn't be printed)");
}
$logger->setLevel(Logger::TRACE);
if($logger->isTraceEnabled()) {
 	$logger->trace("Second Trace Statement (should be printed)");
}