1

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...");
Abel
  • 2,371
  • 3
  • 15
  • 29
  • Can you show your configuration file, please? – aynber Jan 22 '20 at 13:59
  • Included configuration file – Abel Jan 22 '20 at 14:02
  • You might need to check to see if [the script is being run via browser or console](https://stackoverflow.com/questions/1042501/how-to-check-with-php-if-the-script-is-being-run-from-the-console-or-browser-req) and only run the logger if it's cli. If the script is being run via browser, stdout is the browser, not the system. – aynber Jan 22 '20 at 14:08

2 Answers2

1

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

  • Thanks :) I also found that there is an issue in my appender, I fixed it. I will include the updated code. – Abel Jan 22 '20 at 14:17
  • I am Glad you solved ! let me translate I dont understand, not english :) –  Jan 22 '20 at 14:18
  • One small doubt, To verify whether it is running in console. I tried "php filename.php" it displays the warning message. While "curl url_path_to_file" Is not showing any. Am I verifying it right – Abel Jan 22 '20 at 14:19
  • Yes! its right way! you can check here if need more info : https://svn.apache.org/repos/asf/logging/log4php/trunk/src/main/php/ –  Jan 22 '20 at 14:21
0

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.

Nathanael
  • 870
  • 5
  • 11