A great logger interface has been describeted at 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.
DEBUG level
We will explain them in more details.
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"}
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"}
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' => '******'));
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.
As we talk above, there are three kinds of log levels.
PhalApi_Logger::LOG_LEVEL_ERROR
PhalApi_Logger::LOG_LEVEL_INFO
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);
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!