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

  • Home
  • SEARCH
  • 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 9154973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:30:53+00:00 2026-06-17T12:30:53+00:00

Question I’ve created a logging class and want to stamp each line with the

  • 0

Question

I’ve created a logging class and want to stamp each line with the class::method that called the static method of the logger class.

Is it possible to tell what called the current method in PHP?

Solution

Thanks for the help. Here’s the PHP code for my logging method.

/**
 * Writes a line into the log.
 * @param string $level The logging level.
 * @param string $message The message.
 */
protected static function write($level,$message)
{
    self::scope_init();
    if(self::$scope[$level])
    {
        $e = new Exception;
        $stack = $e->getTrace();
        $caller = '';
        if(isset($stack[2]))
        {
            $class = isset($stack[2]['class']) ? $stack[2]['class'] : false;
            $method =  isset($stack[2]['function']) ? $stack[2]['function'] : '';
            if($class !== false)
            {
                $caller = $class.'::'.$method;
            }
            else
            {
                $caller = $method;
            }
        }

        $caller = substr(str_pad($caller,20),0,20);

        $msg = date('m.d.y h:i:sa').' ['.$level.'] ['.$caller.'] '.$message;
        self::$lines[] = $msg;
        echo "$msg\n";
    }
}
  • 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-17T12:30:54+00:00Added an answer on June 17, 2026 at 12:30 pm

    This answer about printing stack traces gives a way to access the stack trace. From there, you can get just the second-to-last item and record that.

    $e = new Exception;
    var_dump($e->getTraceAsString());
    
    #2 /usr/share/php/PHPUnit/Framework/TestCase.php(626): SeriesHelperTest->setUp()
    #3 /usr/share/php/PHPUnit/Framework/TestResult.php(666): PHPUnit_Framework_TestCase->runBare()
    #4 /usr/share/php/PHPUnit/Framework/TestCase.php(576): PHPUnit_Framework_TestResult->run(Object(SeriesHelperTest))
    #5 /usr/share/php/PHPUnit/Framework/TestSuite.php(757): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
    #6 /usr/share/php/PHPUnit/Framework/TestSuite.php(733): PHPUnit_Framework_TestSuite->runTest(Object(SeriesHelperTest), Object(PHPUnit_Framework_TestResult))
    #7 /usr/share/php/PHPUnit/TextUI/TestRunner.php(305): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult), false, Array, Array, false)
    #8 /usr/share/php/PHPUnit/TextUI/Command.php(188): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array)
    #9 /usr/share/php/PHPUnit/TextUI/Command.php(129): PHPUnit_TextUI_Command->run(Array, true)
    #10 /usr/bin/phpunit(53): PHPUnit_TextUI_Command::main()
    #11 {main}"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question Is it possible to stop a Message Driven Bean (programmatically), so that it
Question says it all - is there a class somewhere in Windows Forms that
Question context: let say that there is some really important row in config/locales/en.yml that
Question: I wrote a method to retrieve a SQL result as a list of
Question is similar to this one How to write a MySQL query that returns
Question concerning the JMM and the semantics concerning volatile fields that are written to
Question: Why there is extra space in 2.3.3? This is a strange behaviour that
Question Is the set up for foreign keys I want to do for Answer
Question: is there a better way to do that? VB.Net Function GuidToBase64(ByVal guid As
Question in title... In short - I have a WCF service exposing operations that

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.