Fork me on GitHub

9.1 PHP Uniform Logger Interface

A great logger interface has been describeted at Logger Interface.

9.2 PhalApi Simpler Logger Interface

After many project developments in real world, we found that actually in most time we only use some of these interfaces, not all of them. For simplicity, PhalApi only provide three kinds of interfaces in PhalApi_Logger.

  • ERROR level
  • INFO level
  • DEBUG level

    We will explain them in more details.

(1) ERROR Level

When system exception happens, which means something happening unexpectly, we can log the context as ERROR level. For example, user fail to pay or program fail to insert new data into database.

We can use PhalApi_Logger::error() to log error information as below. It's very simple as you can see.

// only record decription
DI()->logger->error('fail to insert DB');

// record decription and more message
DI()->logger->error('fail to insert DB', 'try to register user dogstar');

// record decription and context data
$data = array('name' => 'dogstar', 'password' => '123456');
DI()->logger->error('fail to insert DB', $data);

These loggers will be saved into log files at default.

$ tailf ./PhalAPi/Runtime/log/201502/20150207.log 
2015-02-07 20:37:55|ERROR|fail to insert DB
2015-02-07 20:37:55|ERROR|fail to insert DB|try to register user dogstar
2015-02-07 20:37:55|ERROR|fail to insert DB|{"name":"dogstar","password":"123456"}

(2) INFO Level

It's necessary to record some business opertaions history at key point in project. These INFO loggs can help us to locate and fix bugs in the future. We can also collect, analyze , dig into these loggs, and do some statistics. In summary, they are useful, because they contain important business information from ou clients.

We can use PhalApi_Logger::info() to log business information as PhalApi_Logger::error() above.

// record infomation
DI()->logger->info('add user exp', array('name' => 'dogstar', 'before' => 10, 'addExp' => 2, 'after' => 12, 'reason' => 'help one more phper'));

// log in file
2015-02-07 20:48:51|INFO|add user exp|{"name":"dogstar","before":10,"addExp":2,"after":12,"reason":"help one more phper"}

(3) DEBUG Level

ERROR and INFO log are usually used in production environment, while DEBUG log is only used in development environment.

Here are some examples of PhalApi_Logger::debug().

DI()->logger->debug('just for test');

DI()->logger->debug('just for test', 'some description ...');

DI()->logger->debug('just for test', array('name' => 'dogstar', 'password' => '******'));

9.3 More Powfull Log Interface

Besides ERROR, INFO, and DEBUG level log, we can use PhalApi_Logger::log() to log anything as we want.

DI()->logger->log('demo', 'add user exp', array('name' => 'dogstar', 'after' => 12));
DI()->logger->log('test', 'add user exp', array('name' => 'dogstar', 'after' => 12));

// related logs in file
2015-02-07 21:13:27|DEMO|add user exp|{"name":"dogstar","before":10,"addExp":2,"after":12,"reason":"help one more phper"}
2015-02-07 21:15:39|TEST|add user exp|{"name":"dogstar","after":12}

Please NOTE that, the first parameter passed to the method will be tranfered into upper case, which is represented for the type of log.

9.4 DON NOT Ferget to Set Log Levels

As we talk above, there are three kinds of log levels.

  • ERROR log: PhalApi_Logger::LOG_LEVEL_ERROR
  • INFO log: PhalApi_Logger::LOG_LEVEL_INFO
  • DEBUG log: PhalApi_Logger::LOG_LEVEL_DEBUG

We can choose to record which kinds of logs by specifying log levels in the entrance file.

DI()->logger = new PhalApi_Logger_File(API_ROOT . '/Runtime',
    PhalApi_Logger::LOG_LEVEL_DEBUG | PhalApi_Logger::LOG_LEVEL_INFO | PhalApi_Logger::LOG_LEVEL_ERROR);

9.5 Extend Your Own Logge

In order to save logs into other storage medium except file, we can extend our own logger interface.

Take store loggs into database for example.

Firstly, implement an new logger.

//$ vim ./Apps/Common/Logger/DB.php

class Common_Logger_DB extends PhalApi_Logger {

    public function log($type, $msg, $data) {
        //TODO save loggs into database ...

Secondly, register DI()->logger with Common_Logger_DB above.

DI()->logger = new Common_Logger_DB($dbConfig,
    PhalApi_Logger::LOG_LEVEL_DEBUG | PhalApi_Logger::LOG_LEVEL_INFO | PhalApi_Logger::LOG_LEVEL_ERROR);

So it is! Check it out by yourself!