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, biapar, Willr, Ingo, swaiba, simon_w

Neat way of adding functionality to debug::log


Reply

6 Posts   501 Views

Avatar
simples

31 August 2012 at 8:25am (Last edited: 31 August 2012 8:35am), Community Member, 76 Posts

Hi,

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.

Thanks.

Avatar
simples

1 September 2012 at 7:27am Community Member, 76 Posts

No ideas from anyone?

Avatar
simples

1 September 2012 at 10:38pm (Last edited: 1 September 2012 10:54pm), Community Member, 76 Posts

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

<?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.

Avatar
simples

1 September 2012 at 11:41pm Community Member, 76 Posts

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.

Avatar
simon_w

1 September 2012 at 11:50pm Forum Moderator, 474 Posts

If you want better support than what Debug::log() offers, you should be using [url=http://api.silverstripe.org/3.0/framework/dev/SS_Log.html#methodlog]SS_Log::log()[/url]. 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.

Avatar
simples

2 September 2012 at 12:10am Community Member, 76 Posts

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.