Logging > CakePHP > Simplify debugging

When you are dealing with a complicated programming task, the best way to simplify is to create clear and concise logs

Follow these isntructions how to create a great logging system to you can identify what your program is doing and find bugs quickly and efficiently. 


# 1 > Create function

Create this PHP function which will be used to call the LOGS

$filename = which name of file to create in CakePHP tmp location
$message = What to display in the log
$newLine = If you want to put many logs onto one line (keeps your logs clean) put FALSE here

public function writeToLog($filename, $message, $newLine = true) {
if ($newLine) {
$message = "\n".date('Ymd-His').' > '.$message;
} else {
$message = ' > '.$message;
file_put_contents(APP.'tmp/logs/'.$filename, $message, FILE_APPEND);


# 2 > Call the log

Then simply place calls to the log to identify what your program is doing

If you want to see a new line each time

$this->writeToLog('Simulators', 'Started at the top found '.$variableFromCode.' db rows', TRUE);

If you want to keep all the info on the same line

$this->writeToLog('Simulators', 'Started at the top found '.$variableFromCode.' db rows', FALSE);


# 3 > View Log

You can now view the log


open terminal (in phpstorm use the following path)

tail -f app/tmp/logs/Simulators

change 'Simuators to any other file you want'

when you are done

'Control + c'