Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8876025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:07:01+00:00 2026-06-14T19:07:01+00:00

I am looking for a way to extend our default logging class without making

  • 0

I am looking for a way to extend our default logging class without making changes to the whole application or library. We have a number of places where we write logs. E.g:

App_Log::getInstance()->write(
    $name,
    $type,
    "LOGOUT",
    $url
);

Auth_Log

<?php
class App_Auth_Log {

    /**
     * Singleton instance
     *
     * Marked only as protected to allow extension of the class. To extend,
     * simply override {@link getInstance()}.
     *
     * @var App_Auth_Log
     */
    protected static $_instance = null;


    /**
     * Auth logging enabled flag.
     *
     * @var boolean
     */
    protected $_enabled = false;


    /**
     * If flag is true then cleanup will not remove login records.
     *
     * @var boolean
     */
    protected $_keepLoginRecords = false;


    /**
     * Class constructor.
     */
    public function __construct() {
        if(App_Front::getInstance()->hasParam("withAuthLog"))
            $this->_enabled = true;

        if(App_Front::getInstance()->hasParam("withKeepLoginRecords"))
            $this->_keepLoginRecords = true;

        $this->cleanup();
    }


    /**
     * Singleton instance
     *
     * @return App_Auth_Log
     */
    public static function getInstance() {
        if (is_null(self::$_instance))
            self::$_instance = new self();
        return self::$_instance;
    }


    /**
     * Write new auth log record with given details. if succesful then method
     * returns true otherwise returns false.
     *
     * @param string $class
     * @param string $ident
     * @param string $action
     * @param string $url
     * @param string $ipaddr
     * @return boolean
     * @throws Exception
     */
    public function write($class,$ident,$action,$url,$ipaddr=null) {

        if($this->isEnabled())  {
            $db = App_Db_Connections::getInstance()->getConnection();
            try {

                // if address not specificed get remote addr
                $ipaddr = ($ipaddr == null) ? $_SERVER['REMOTE_ADDR'] : $ipaddr;

                // manual insert so we can take advantage of insert delayed
                $stmnt = "INSERT INTO accesslogs
                    VALUES('',NOW(),'$class','$ident','$action','$url','$ipaddr')";

                // execute insert
                $db->query($stmnt);

            } catch (Exception $e) {
                throw $e;
            }
            return true;
        }
        return false;
    }

    /**
     * Cleanup old accesslog records. Cached to run once a day.
     *
     * @return boolean - returns true if run false if not.
     */
    public function cleanup() {

        $cache  = App_Cache::getInstance()->newObject(86400);

        if($this->isEnabled()) {

            if (!$res = $cache->load(App_Cache::getCacheName(__CLASS__. "cleanup"))) {
                // add cache
                $db = App_Db_Connections::getInstance()->getConnection();
                try {
                    $where = $db->quoteInto("DATEDIFF(NOW(),accesslog_datetime) > ?", 6);
                    $and = ($this->_keepLoginRecords) ? " AND accesslog_action != 'LOGIN'" : "";
                    $db->query("DELETE LOW_PRIORITY FROM accesslogs WHERE $where $and");
                } catch (Exception $e) {
                    throw $e;
                }


                $cache->save($res,App_Cache::getCacheName(__CLASS__. "cleanup"));
            } // end cache
        }
        return;
    }

    /**
     * Returns boolean check if auth log enabled.
     *
     * @return boolean
     */
    public function isEnabled() {
        return ($this->_enabled) ? true : false;
    }

    /**
     * Enabled disable the auth log process.
     *
     * @param boolean $boolean
     * @return App_Auth_Log
     */
    public function setEnabled($boolean) {
        $this->_enabled = ($boolean) ? true : false;
        return $this;
    }
}
?>

This is the default behaviour of the core code. But for this specific project I need to be able to extend/overwrite the write method, e.g with extra parameters.

Q: How can I make changes to this App_Auth_Log class so that its backwards compatible with previous projects that call App_Log::getInstance()->write?

How I think it should work(but dont know how to do it).
If App_Front::getInstance()->hasParam("withAuthLog") passes a custom class name e.g: My_Custom_Auth_Log which overwrites the original write method. Just not sure how to modify the singleton part

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:07:02+00:00Added an answer on June 14, 2026 at 7:07 pm

    You have no choice. You have to modify your App_Log code, because everything statically makes calls to it. For minimal changes, you could extend the App_Log class and make App_Log::getInstance return an instance of that child class; but that’s pretty messy, since a parent should never know about its children.

    Maybe you can prevent the default implementation of App_Log to be loaded and load a different implementation of it from a different file. That’s pretty messy too though.

    This is exactly (one of) the reason(s) why singletons and static calls are very frowned upon. See How Not To Kill Your Testability Using Statics.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a way to extend a PHP class to add a custom
I'm looking for a way to extend a TextField that's allready on the stage
Greetings, I'm looking for a way to extend the functionality of the HTMLEditor control
I'm looking a way to enable IP logging with log4net in ASP.NET. I found
I'm looking for a way to extend Ember's Object to include some additional methods,
Is there an easy/straightforward way to extend the file upload class to encrypt files
Introduction : Hello Everyone, I have been looking for days for a way to
I'm looking for an easy way to extend existing JPA mapping. The idea is
I'm looking for a way to add a javascript object to the DOM without
Currently I'm looking for a way to either extend a standard <asp:TextBox /> control

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.