How to use log4php on PHP 8.2?

402 views Asked by At

log4php generates a Deprecated: fwrite(): Passing null to parameter #2 ($data) of type string is deprecated in D:\inc\log4php\appenders\LoggerAppenderFile.php on line 137

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns="http://logging.apache.org/log4php/">
    <appender name="default" class="LoggerAppenderFile">
        <layout class="LoggerLayoutSimple" />
        <param name="file" value="test.log" />
        <param name="append" value="true" />
    </appender>
    <root>
        <appender_ref ref="default" />
    </root>
</configuration>

and initialization

require ROOT . "/inc/log4php/Logger.php";
Logger::configure( ROOT . 'log-config.xml' );
$logger = Logger::getLogger( 'main' );
define( 'LOG', $logger );
LOG->debug( 'utils.php' );

What would be the proper way to start the log4php logger? I copied the settings from the documentation.

3

There are 3 answers

0
Olivier On

The official log4php project is now old and inactive, but there exists a fork that added PHP 8 compatibility (as you can see here):

add support for php > 8.0 (fwrite null)

1
IMSoP On

If this is the only message you're getting, one option is to ignore the message, for now. Don't ignore it forever, but don't panic right now.

A deprecation notice is not an error; it is a message warning you that a future version of PHP will throw an error on that piece of code. Due to PHP's compatibility policy, that will be PHP 9.0 at the earliest. The entire purpose of such messages is to give you time to fix them.

From the context you've given, the message is not about how you're calling the library, it's about how the library is handling some situation. So there are two possibilities:

  • Someone is maintaining the library, or someone is willing to take it over (maybe you?). They have until PHP 9.0 comes out to change the code before it becomes an error.
  • Nobody is maintaining the library (including you). You will need to find a different library, or rewrite your code not to depend on it - but you have until you want to run on PHP 9.0 to do so.

Note that your application might treat this notice as though it as an error, by setting a custom "error handler" - some frameworks do this by default on development mode. Consider whether you think that is a useful configuration, and whether fixing messages like this as early as possible is the right priority for your time.

0
theking2 On

After spending some time with log4php and various forks I came to the conclusion to go wit monolog instead despite its performance

Thanks for the useful comments