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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:20:53+00:00 2026-05-27T04:20:53+00:00

My home-made simple logger (just a wrapper around fopen , fwrite , fclose )

  • 0

My home-made simple logger (just a wrapper around fopen, fwrite, fclose) should be enabled or disabled on the fly. I think the way i’m checking if logging is enable is overkill (every call to a logging method requires to evaluate LOGGING):

config.inc.php

define('LOGGING', true);

logger.php

public function __construct($filename)
{
    $this->fp = fopen($filename, 'w+');
}

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

public function warn($message)
{
   $this->log('WARN', $message);
}

private function log($type, $message)
{
   if(!defined(LOGGING) || !LOGGING) return;

   fwrite($this->fp, "[$type] $message\n");
}

Question: how to initialize file handler to something like null and remove the check for LOGGING? I mean something like this:

public function __construct($filename)
{
   // Create a fake stream if logging is disabled
   $this->fp = !defined(LOGGING) || !LOGGING ? null : fopen($filename, 'w+);
}

Future call to $logger->warn('Ops..') should do nothing, without triggering any error or notice.

  • 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-27T04:20:54+00:00Added an answer on May 27, 2026 at 4:20 am

    I mentioned it as a comment above. However, now once again as an answer, because now I think it is one.

    You can create a special logger subclass, something like

    class NullLogger implements Logger {
      public function log ($message) { /* No operation */ }
    }
    

    When LOGGING is false, just use this log class and every call with just doing nothing, what will reduce the overhead to the absolute minimum.

    This approach has even an own name/pattern: http://en.wikipedia.org/wiki/Null_Object_pattern

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

Sidebar

Related Questions

I have been playing around a bit with a fairly simple, home-made search engine,
I have just ported several of our home-made Outlook COM-addins from Delphi 2007 to
I've made a simple Web Application for the iPhone, it's just 6 pages each
I made a really simple MVC application with VS 2010 beta. just a view
I learning Perl and doing a home made project to my family (a subscription
So I'm going to be working on a home made blog system in PHP
For some months I've been working on a home-made operating system. Currently, it boots
Here is a simple Tell a Friend form I made in HTML, it's correctly
I'm trying out the new ASP.NET MVC 4 Mobile Features. I made a simple
I have made a few attempts at this and they ended up just getting

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.