Note: each tutorial contains information about one specific part of Spoon. If something is unclear, post a comment.

Logging

Written by Tijs Verkoyen -

Prerequisites

What will you learn?

  • How to log data.

Features

When developing an application, or when it is live, in some cases you want to log data. SpoonLog provides this functionality.

It will rotate the logs automatically, this means if the logfile exceeds a predefined size (500kb) it will be archived.

Basics

Using SpoonLog is very easy, just require the class and you're ready to start logging. In the example below you can see how we store a simple message.

// required classes
require_once 'spoon/spoon.php';

// store a cool quote
SpoonLog::write('Guards! Arrest all Gypsies driving fire engines.');

If you look in the folder that is on the same level as the spoon-folder you will see a new file called error.log. Offcourse a quote isn't a real error, in the example below you'll see how to fix this.

// required classes
require_once 'spoon/spoon.php';

// store a cool quote
SpoonLog::write('With a 22mm shell, "just wounding" may be difficult.', 'custom');

As you can see a new file is created, called custom.log. In the example above we added an extra parameters which indicates the type of message, possible values are: error and custom.

All of this logging is really cool, but the logfiles should have a decent location, this can be achieved by setting the logpath. The path will be used for all errors/message you log from then on. In the example below you can see how to set the path.

// required classes
require_once 'spoon/spoon.php';

// set the path for the logfiles
SpoonLog::setPath('/Users/tijs/Sites/spoon.local/logs');

// store a cool quote
SpoonLog::write('God Moaning. The resist-once have accqo-aired a bum. They are
 going to ex-plod the whaleway brodge.', 'custom');

Conclusion

You're able to log (custom) data.

3 comments

Jim Burlison wrote 2 years ago

<b>Strict Standards</b>: Non-static method SpoonLog::setPath() should not be called statically in <b>C:\xampp\htdocs\trunk\public\index.php</b> on line <b>199</b><br />

<br />
<b>Fatal error</b>: Using $this when not in object context in <b>C:\xampp\htdocs\trunk\php\spoon\log\log.php</b> on line <b>140</b><br />


any Idea?

Bauffman wrote 2 years ago

The example above isn't very clear. It's better if you create an instance of the class.

$log = new SpoonLog();
$log->setPath...

Jim Burlison wrote 2 years ago

Yeah I read up on it, if you set the function as static it works fine.