You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "Kate Gray (Jira)" <ji...@apache.org> on 2019/11/18 18:03:00 UTC

[jira] [Comment Edited] (LOG4PHP-234) Two plugin using log4php produces Fatal, although include_once used

    [ https://issues.apache.org/jira/browse/LOG4PHP-234?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16976737#comment-16976737 ] 

Kate Gray edited comment on LOG4PHP-234 at 11/18/19 6:02 PM:
-------------------------------------------------------------

By default, they will, as the root configuration is the same.  If that's not what you want, the easiest way to handle it is probably to create an empty base configuration and then add custom appenders.

 

Here's an example:

 
{code:java}
<?php

namespace L4PHP\Test;

// Check that the class does not already exist.  If it does, skip the require.
if (!class_exists('Logger')) {
    require_once(__DIR__ . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'log4php' . DIRECTORY_SEPARATOR . 'Logger.php');
}

use Logger;
// Loggers will inherit from the root logger, so use an empty configuration if we don't want the default (print to console) used.
$emptyConfig = array('rootLogger' => array());
Logger::configure($emptyConfig);

// Create the two separate plugin classes.  Each will instantiate it's own logger based on the base configuration.
$abcPlugin = new ABCPlugin ();
$xyzPlugin = new XYZPlugin ();

// Use the plugin classes to log different messages at different levels
$abcPlugin->log(WPPlugin::LOGLEVEL_WARN, 'ABC Plugin, logging a warning');
$xyzPlugin->log(WPPLUGIN::LOGLEVEL_INFO, 'XYZ Plugin, logging information');

class WPPlugin
{
    /**
     * @var Logger $logger
     */
    protected $logger;

    protected $plugin_name;

    // These just make it easier to have a single function to log without inheriting from the Logger class directly.
    const LOGLEVEL_WARN = 0;
    const LOGLEVEL_INFO = 1;

    /**
     * Log something
     * @param int $level        Logging level constant
     * @param string $message   Logging message
     */
    public function log ($level, $message) {
        switch ($level) {
            case self::LOGLEVEL_WARN:
                $this->logger->warn($message);
                break;
            case self::LOGLEVEL_INFO:
                $this->logger->info($message);
                break;
            default:
                throw new \InvalidArgumentException('Unsupported logging level for this example.');
        }
    }
}

class ABCPlugin extends WPPlugin
{
    protected $plugin_name = 'wp-abc';

    public function __construct()
    {
        // Get a brand new logger instance
        $logger = Logger::getLogger($this->plugin_name);

        // Create a logfile appender
        $appender = new \LoggerAppenderFile('default');

        // Set the log file based on __DIR__, which will put it into the plugin directory
        $appender->setFile (__DIR__ . DIRECTORY_SEPARATOR . $this->plugin_name . '.log');

        // Add the logger to the appender and store it.
        $logger->addAppender ($appender);
        $this->logger = $logger;
    }
}

class XYZPlugin extends WPPlugin
{
    protected $plugin_name = 'wp-xyz';

    public function __construct()
    {
        // Get a brand new logger instance
        $logger = Logger::getLogger($this->plugin_name);

        // Create a logfile appender
        $appender = new \LoggerAppenderFile('default');

        // Set the log file based on __DIR__, which will put it into the plugin directory
        $appender->setFile (__DIR__ . DIRECTORY_SEPARATOR . $this->plugin_name . '.log');

        // Add the logger to the appender and store it.
        $logger->addAppender ($appender);
        $this->logger = $logger;
    }
}
{code}


was (Author: kategray):
By default, they will, as the root configuration is the same.  If that's not what you want, the easiest way to handle it is probably to create an empty base configuration and then add custom appenders.

 

Here's an example:

 
{code:java}
<?php

namespace L4PHP\Test;

// Check that the class does not already exist.  If it does, skip the require.
if (!class_exists('Logger')) {
    require_once(__DIR__ . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'log4php' . DIRECTORY_SEPARATOR . 'Logger.php');
}

use http\Exception\InvalidArgumentException;
use Logger;
// Loggers will inherit from the root logger, so use an empty configuration if we don't want the default (print to console) used.
$emptyConfig = array('rootLogger' => array());
Logger::configure($emptyConfig);

// Create the two separate plugin classes.  Each will instantiate it's own logger based on the base configuration.
$abcPlugin = new ABCPlugin ();
$xyzPlugin = new XYZPlugin ();

// Use the plugin classes to log different messages at different levels
$abcPlugin->log(WPPlugin::LOGLEVEL_WARN, 'ABC Plugin, logging a warning');
$xyzPlugin->log(WPPLUGIN::LOGLEVEL_INFO, 'XYZ Plugin, logging information');

class WPPlugin
{
    /**
     * @var Logger $logger
     */
    protected $logger;

    protected $plugin_name;

    // These just make it easier to have a single function to log without inheriting from the Logger class directly.
    const LOGLEVEL_WARN = 0;
    const LOGLEVEL_INFO = 1;

    /**
     * Log something
     * @param int $level        Logging level constant
     * @param string $message   Logging message
     */
    public function log ($level, $message) {
        switch ($level) {
            case self::LOGLEVEL_WARN:
                $this->logger->warn($message);
                break;
            case self::LOGLEVEL_INFO:
                $this->logger->info($message);
                break;
            default:
                throw new InvalidArgumentException('Unsupported logging level for this example.');
        }
    }
}

class ABCPlugin extends WPPlugin
{
    protected $plugin_name = 'wp-abc';

    public function __construct()
    {
        // Get a brand new logger instance
        $logger = Logger::getLogger($this->plugin_name);

        // Create a logfile appender
        $appender = new \LoggerAppenderFile('default');

        // Set the log file based on __DIR__, which will put it into the plugin directory
        $appender->setFile (__DIR__ . DIRECTORY_SEPARATOR . $this->plugin_name . '.log');

        // Add the logger to the appender and store it.
        $logger->addAppender ($appender);
        $this->logger = $logger;
    }
}

class XYZPlugin extends WPPlugin
{
    protected $plugin_name = 'wp-xyz';

    public function __construct()
    {
        // Get a brand new logger instance
        $logger = Logger::getLogger($this->plugin_name);

        // Create a logfile appender
        $appender = new \LoggerAppenderFile('default');

        // Set the log file based on __DIR__, which will put it into the plugin directory
        $appender->setFile (__DIR__ . DIRECTORY_SEPARATOR . $this->plugin_name . '.log');

        // Add the logger to the appender and store it.
        $logger->addAppender ($appender);
        $this->logger = $logger;
    }
}
{code}

> Two plugin using log4php produces Fatal, although include_once used
> -------------------------------------------------------------------
>
>                 Key: LOG4PHP-234
>                 URL: https://issues.apache.org/jira/browse/LOG4PHP-234
>             Project: Log4php
>          Issue Type: Bug
>         Environment: ubuntu LTS 16
>            Reporter: Max UNGER
>            Priority: Critical
>
> Two wordpress plugins on the same install using log4php, loaded as explained in doc produce fatal error when used together:
> PHP Fatal error:  Cannot declare class LoggerAutoloader, because the name is already in use in /var/www/yyyyyyy.com/wp-content/plugins/myplugin/libs/log4php/2.3.0/LoggerAutoloader.php on line 34
> no change if include_once used instead of include



--
This message was sent by Atlassian Jira
(v8.3.4#803005)