1

What is the correct form to use log4php inside a class with functions?

<?php
include_once 'log4php-2.3.0/src/main/php/Logger.php';

class template
{
  public static temp1;
  public static temp2;
  Logger::configure($path . '\commons\log4php-2.3.0\config.xml');
  $log = Logger::getLogger('myLogger');

  public static function example1()
  {

  }

  public static function example2()
  {

  }

}

How can I call the $log->fatal($error); line for example inside both functions? Should i redeclare the $log in each or how?

Rotimi
  • 4,783
  • 4
  • 18
  • 27

1 Answers1

1

A simple example from Reference

// Include and configure log4php
include('log4php/Logger.php');
Logger::configure('config.xml');

/**
 * This is a classic usage pattern: one logger object per class.
 */
class Foo
{
    /** Holds the Logger. */
    private $log;

    /** Logger is instantiated in the constructor. */
    public function __construct()
    {
        // The __CLASS__ constant holds the class name, in our case "Foo".
        // Therefore this creates a logger named "Foo" (which we configured in the config file)
        $this->log = Logger::getLogger(__CLASS__);
    }

    /** Logger can be used from any member method. */
    public function go()
    {
        $this->log->info("We have liftoff.");
    }
}

$foo = new Foo();
$foo->go();
Dimag Kharab
  • 4,439
  • 1
  • 24
  • 45
  • I'm using this way alredy, problem is i'm getting this error, seems like isn't getting past the construct, Fatal error: Using $this when not in object context. I'm using this class without doing the new Foo, i'm just calling the functions – Cristian Gonzalez Apr 18 '17 at 12:43