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 350729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:34:38+00:00 2026-05-12T11:34:38+00:00

To clarify: I’m building a Logger class that allows me to easily log messages:

  • 0

To clarify:

I’m building a Logger class that allows me to easily log messages:

lib.Logger.php:

<?php
class Logger {
    private $handle;

    public function __construct($log_name, $log_path) {

        if ( ! is_dir($log_path))
            throw new Exception('Log path does not exist.');

        if ( ! in_array(strtolower(substr($log_name, 0, -4)), array('.log', '.txt')))
            $log_name = "{$log_name}.log";

        $this->handle = fopen("{$log_path}/{$log_name}", 'a');

        $this->log('------------- Initializing ------------- '.get_parent_class($this));
    }

    // --------------------------------------------------------------------

    public function __destruct() {
        fclose($this->handle);
    }

    // --------------------------------------------------------------------

    public function log($message) {
        $time = date(DATE_RFC822);

        $log = "[{$time}] {$message}\n";

        fwrite($this->handle, $log);
    }    

}

?>

And I call this using:

MyController.php:

<?php
class MyController extends Controller {
    $logger = new Logger('testlog','/path/to/logs/');
    $logger->log('Logs are fun!');
}
?>

When I initialize the object:

 $this->log('------------- Initializing ------------- '.get_parent_class($this));

I want to log the name of the object (or file) that is calling log() — in this case, either

MyController

or

/path/to/MyController.php

.

I tried using get_parent_class(), but of course this doesn’t work because Logger does not have a parent class per se.

Any ideas? thank you so much for the help!

Alex B

  • 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-05-12T11:34:38+00:00Added an answer on May 12, 2026 at 11:34 am

    The only way to handle this implicitly is to capture the output from debug_backtrace as the others have suggested.

    If you are interested in an explicit approach, maybe this could be it

    in Logger

    public function initialize( $context )
    {
      $logMessage = '------------- Initializing ------------- ';
      if ( is_object( $context ) )
      {
        $logMessage .= get_class( $context );
      } else {
        // do something else?
      }
      $this->log( $logMessage );
    }
    

    and then

    class MyController extends Controller
    {
      public function someFunc()
      {
        $logger = new Logger('testlog','/path/to/logs/');
        $logger->initialize( $this );
        $logger->log('Logs are fun!');
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me clarify: I am building a relatively simple brochure site that has multiple
Let me clarify this a bit... I have a class that handles XML files.
Please clarify my doubt. class A{ Long weight = 1000L; } public class B
Could someone please clarify what the code means: public abstract class BaseJobProcessor<T> : JobProcessor
To clarify, here's the situation: I'm building a really simple CMS that will become
Let me first clarify that I'm not much familiar with PHP language, since I
To clarify, I currently have a live app that does not implement in app
I should clarify that I am looking for a client-side solution. Alternatively, is there
To clarify: I'm not using the gettext php module /extension, reason being because I
could you please clarify my issue? I have a starting page ListBox1.aspx that calls

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.