Apache log4php™是一个通用的日志框架为PHP,可以通过xml或php文件来进行配置。可以应用在cms、crm等php系统中。
Level | Severity | Description |
---|---|---|
FATAL 致命 | Highest | Very severe error events that will presumably lead the application to abort.(非常严重的错误可能会导致应用程序中止的事件。 |
ERROR错误 | ... | Error events that might still allow the application to continue running.(错误的事件仍可能使应用程序继续运行。 |
WARN警告 | ... | Potentially harmful situations which still allow the application to continue running.(潜在的有害的情况下,仍然允许应用程序继续运行。 |
INFO信息 | ... | Informational messages that highlight the progress of the application at coarse-grained level.(信息消息强调在粗粒度级别应用程序的进展。 |
DEBUG调试 | ... | Fine-grained informational events that are most useful to debug an application.(细粒度的信息事件最有用的调试应用程序。 |
TRACE运行轨迹 | Lowest | Finest-grained informational events.(最细致的获得时间信息 |
1.版本:目前最新版本为 2.3.0.
2.下载:http://logging.apache.org/log4php/download.html
3.安装:将下载下来的压缩包内/src/main文件夹,放到你的项目指定位置,进行简单配置就可以进行应用。
1.面向过程简易使用
include ('./main/php/Logger.php'); $logger = Logger::getLogger("main"); $logger->info("This is an informational message."); echo "<br>"; $logger->warn("I'm not feeling so good...");
//配置方法一:PHP数组配置格式 $config = array( 'appenders' => array( 'default' => array( //'LoggerAppenderFile','LoggerAppenderDailyFile','LoggerAppenderEcho','LoggerAppenderPDO' 'class' => 'LoggerAppenderDailyFile',//$two:Appenders(输出源) 'layout' => array( //'LoggerLayoutPattern','LoggerLayoutSimple','LoggerLayoutSerialized','LoggerLayoutXml' 'class' => 'LoggerLayoutSimple',//$three:Layouts(布局), ), 'params' => array( /*以下Layouts(布局)Pattern时才能用*/ // 'conversionPattern' => '%date %logger %-5level %msg%n', //,用来自定义日志内容的格式 /*以下Layouts(布局)LoggerLayoutSimple*/ /*case:1 Appenders(输出源)Echo时 */ // 'htmlLineBreaks' => 'true', /*case:1Appenders(输出源)Echo时 */ /*case:2 Appenders(输出源)DailyFile时 */ 'datePattern' => 'Y-m-d', //去掉该参数,则文件名称时间为:201600412 'file' => 'file-%s.log', //文件名称 'append'=>true , //没填默认true输出内容将追加,,若为false,文件内容将被覆盖。 /*case:2Appenders(输出源)file时*/ /*case:3 Appenders(输出源)File时 */ // 'file' => 'file.log',//文件名称 // 'append' => false//不追加 /*case:3Appenders(输出源)File时*/ /*case:4 Appenders(输出源)PDO时 */ // 'dsn' => 'mysql:host=localhost;dbname=logdb', // 'user' => 'root', // 'password' => 'secret', // 'table' => 'log', /*case:4 Appenders(输出源)PDO时*/ ), ) ), 'rootLogger' => array( 'appenders' => array('default') ) );
include ('./main/php/Logger.php'); Logger::configure($config);//PHP数组配置是开启 // Logger::configure('config.xml');//XML配置 /** * This is a classic usage pattern: one logger object per class. */ class Foo { /** Holds the Logger. */ private $log; /** Logger is instantiated in the constructor. */ public function __construct() { // The __CLASS__ constant holds the class name, in our case "Foo". // Therefore this creates a logger named "Foo" (which we configured in the config file) $this->log = Logger::getLogger(__CLASS__); var_dump($this->log); } /** Logger can be used from any member method. */ public function go() { /*同业执行脚本中,不能出现两次相同函数*/ $this->log->trace("<------------------>\r\n My first message.111<br>\r\n"); // Not logged because TRACE < WARN $this->log->debug("My second message.111<br>\r\n"); // Not logged because DEBUG < WARN $this->log->info("My third message.1111<br>\r\n"); // Not logged because INFO < WARN $this->log->warn("My fourth message.111<br>\r\n"); // Logged because WARN >= WARN $this->log->error("My fifth message.111<br>\r\n"); // Logged because ERROR >= WARN $this->log->fatal("My sixth message.1111<br>\r\n<------------------>"); // Logged because FATAL >= WARN } } $foo = new Foo(); $foo->go(); /*END面向对象*/