Learn DooPHP: High performance PHP framework

Using DooPHP Logging Tools

DooPHP comes with its own profiler and logging tool by default. The class that handle this in the framework is DooLog (dooframework/app/logging/DooLog.php)

There are a few methods in this file where you can use for profiling and logging queries or important message in your application. You can log a message by calling:

Doo::logger()->log('Something fishy here!', DooLog::Alert);

Or you can use the Alias methods instead of passing the log level:

Doo::logger()->alert('Something fishy here!');
Doo::logger()->emerg('message...');
Doo::logger()->crit('message...');
Doo::logger()->err('message...');
Doo::logger()->warn('message...');
Doo::logger()->notice('message...');
Doo::logger()->info('message...');
Doo::logger()->trace('message...');

All of the log messages can be organized by category, simply pass in another parameter at the end of the methods:

Doo::logger()->log('Something fishy here!', DooLog::ALERT, 'editpost');
Doo::logger()->emerg('message...', 'editpost');
Doo::logger()->alert('message...', 'editpost');

To view the logged messages, you just have to call showLogs(). By default DooLog will return a neatly formatted XML log which can be filtered by level or category. You can get a plain text log if you need so:

//display all logs
echo Doo::logger()->showLogs();

//display plain text log
echo Doo::logger()->showLogs(false);

//display only logs in category editpost
echo Doo::logger()->showLogs(true, null, 'editpost');

//display only logs in level Alert and category editpost
echo Doo::logger()->showLogs(true, DooLog::ALERT, 'editpost');

When you have to write the log messages into file, all you have to do is call writeLogs(). Similar to showLogs(), it writes the XML string to file by default.


//Creates a log file log.xml
Doo::logger()->writeLogs();

//You can write as plain text
Doo::logger()->writeLogs('log.txt', false);

You can write logs of certain level or category as well


Doo::logger()->writeLogs('log.xml', true, DooLog::ALERT, 'editpost');

If you are worried that the log files get bigger over time, all you have to do is to specify a file size for log rotation. When this file is reached, the logs will automatically be purged and new messages will be written.


//maximum log file size is 20kb

Doo::logger()->rotateFile(20);

Doo::logger()->writeLogs();

The logger class writes the file to the where your app located by default (SITE_PATH). If you would like to change where the log files will be stored, simply add the LOG_PATH setting in common.conf.php:


//this will store log files at /var/logs/, eg. /var/logs/log.xml
$config['LOG_PATH'] = '/var/logs/';

That’s not all! After you have your log files stored, DooPHP has a handy little tool for you to monitor your logs. If you have gone through the default application App, there’s a route call debug which looks like this:


$route['*']['/debug/:filename'] = array('MainController', 'debug');

This route is for you to use with the log viewer tool done in Flex (yea you need Flash Player to view this).  The route parameter ‘filename’ would be the name of your log file which is log.xml by default. Alright, now let’s test it out, I am using the Blog demo as example and the address to the demo is http://localhost/amfdemo/blog/ (yes, there’ll be a Amfphp + DooPHP tutorial later on)

All you need to do is visit http://localhost/amfdemo/blog/tools/logviewer.html. Just enter the debug route + the log filename as address (http://localhost/amfdemo/blog/debug/log) and this is what you will get.

Log Viewer Tool

With this tool, you can easily monitor your log view. You can do sorting on the date time, log level, category, access URI, message of your log as well as some filtering.

Filter by category:

Filter by Category - log viewer

Filter by log level/type:

filter by log level/type - log viewer

Filter by URI:

filter by URI - log viwer

You can filter with the 3 combination as well. One thing to take note is when you use the log level TRACE, it will only be logged if the debug mode is on (DEBUG_ENABLED is true). This is good for testing during development and you can simply disabled the trace log by setting


$config['DEBUG_ENABLED'] = False;

Here download this blog + logging demo, there’s already a log.xml in the blog folder so that you can have a quick look at the log viewer tool. The logging messages makes no sense as they just for demo purpose :P

That’s all for logging, next I will explain how to do profiling and database profiling with DooPHP. If you wish to discus further, there’s a thread on this tutorial in the forum here.


You can follow any responses to this entry through the RSS 2.0 feed.