Jump to:

22983 Posts in 11709 Topics by 2827 members

General Questions

SilverStripe Forums » General Questions » Neat way of adding functionality to debug::log

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

Page: 1
Go to End
Author Topic: 429 Views
  • simples
    Avatar
    Community Member
    76 Posts

    Neat way of adding functionality to debug::log Link to this post

    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.

  • simples
    Avatar
    Community Member
    76 Posts

    Re: Neat way of adding functionality to debug::log Link to this post

    No ideas from anyone?

  • simples
    Avatar
    Community Member
    76 Posts

    Re: Neat way of adding functionality to debug::log Link to this post

    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.

  • simples
    Avatar
    Community Member
    76 Posts

    Re: Neat way of adding functionality to debug::log Link to this post

    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.

  • simon_w
    Avatar
    Forum Moderator
    467 Posts

    Re: Neat way of adding functionality to debug::log Link to this post

    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.

  • simples
    Avatar
    Community Member
    76 Posts

    Re: Neat way of adding functionality to debug::log Link to this post

    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.

    429 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.