log php errors on console using log4php but it display on browser as well

686 Views Asked by At

i am using log4php library log errors only to console (stdout). But when I run my code on browser, it outputs on the browser, which I don't want to happen.

I already referred and tried logs php error but not display it in browser, but none of those answers worked out for me my code is simple.

<?php
error_reporting(E_ERROR | E_PARSE);
error_reporting(0);ini_set('display_errors', 'Off');
require_once("log4php/Logger.php");
$logger = Logger::getLogger("main");
$logger->info("This is an informational message.");
$logger->warn("I'm not feeling so good...");

Configuration file:

<configuration xmlns="http://logging.apache.org/log4php/">
<appender name="default" class="LoggerAppenderConsole">
    <layout class="LoggerLayoutSimple" />
</appender>
<root>
    <appender_ref ref="default" />
</root>

This outputs on the browser,

INFO - This is an informational message. WARN - I'm not feeling so good...

I want it to be only on stdout. Please let me know if my understanding is wrong about stodut in this case

EDIT MY WORKING CODE AFTER THE ANSWER:

<?php
require_once("log4php/Logger.php");
Logger::configure("log4php/config.xml");
$logger = Logger::getLogger("default");
$logger->info("This is an informational message.");
$logger->warn("I'm not feeling so good...");
2

There are 2 best solutions below

4
AudioBubble On BEST ANSWER

I think you are not calling file proper way ! or missing xml file

require_once('/path/to/log4php.xml');

So it should be something like this

error_reporting(E_ERROR | E_WARNING | E_PARSE);
error_reporting(0);
ini_set('display_errors', 'Off');
require_once('/path/to/log4php/Logger.php');
//xml file here
$logger = Logger::getLogger(basename(__FILE__));
$logger->info('This is an informational message');
$logger->warn("I'm not feeling so good...");

See this question is well : log4php : Cannot create log file

0
Nathanael On

try

ini_set('display_errors',0);

or set that directive in the php.ini file

see https://www.php.net/manual/en/errorfunc.configuration.php#ini.display-errors

in the documentation it says Although display_errors may be set at runtime (with ini_set()), it won't have any effect if the script has fatal errors. This is because the desired runtime action does not get executed.