Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Neat way of adding functionality to debug::log

Go to End

6 Posts   883 Views


Community Member, 77 Posts

31 August 2012 at 8:25am

Edited: 31/08/2012 8:35am


Out of the box Debug::log writes the date and a specified message to [root]/debug.log. I would like to add the name of the logged-in user and the filename and line number at which the log function was called.

I know how to do this by editing the function. However I want to achieve this by adding code to [my module]/code rather than changing code in sapphire.

Is there a neat way of doing this? Currently I have no idea.



Community Member, 77 Posts

1 September 2012 at 7:27am

No ideas from anyone?


Community Member, 77 Posts

1 September 2012 at 10:38pm

Edited: 01/09/2012 10:54pm

In an attempt to extent debug::log without altering the Version 2.4 framework, I added the following line to mysite/_config.php

Object::add_extension("Debug", "CustomDebug");

created and added the following to mysite\code\CustomDebug.php


class CustomDebug extends Debug{
	// My stuff will go in here

Then in order to make sure that the object gets built before the object is run I commented out the above line in mysite/_config.php,
ran dev/build,
removed the comment, and finally
re-ran dev/build.

However when I re-ran dev/build I got the following message

Fatal error: Object::add_extension() - Extension "CustomDebug" is not a subclass of Extension in [my root]\sapphire\core\Object.php on line 530

I have now spent some time trying to figure out where I screwed up so if anyone can spot anything that would be great. Maybe my line in _config.php is wrong.


Community Member, 77 Posts

1 September 2012 at 11:41pm

OK I have now resorted to directly editing the framework.

Not good and I hate doing this but I was unable to find anything else which worked.


Community Member, 473 Posts

1 September 2012 at 11:50pm

If you want better support than what Debug::log() offers, you should be using SS_Log::log(). Your code doesn't work because Object::add_extension() is used to add extensions to objects, not to replace them. Object::useCustomClass() can be used to replace some classes in some places, but not debug as it is always called statically.


Community Member, 77 Posts

2 September 2012 at 12:10am

Hi Simon,

Thanks for putting me straight about the problem with replacing static classes. I also see that SS_Log::log() takes an extras argument which looks promising. However I am being forced to use Version 2.4 by my client whose hosting service uses PHP 5.2 and I see that this extras argument does not appear to be used by SS_Log::log() in Version 2.4.

I am currently doing some hoovering because I have a guest arriving. However I plan to look at this again a little later today to check that I have not overlooked something here.