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...");
3
  • Can you show your configuration file, please? Commented Jan 22, 2020 at 13:59
  • Included configuration file Commented Jan 22, 2020 at 14:02
  • You might need to check to see if the script is being run via browser or console and only run the logger if it's cli. If the script is being run via browser, stdout is the browser, not the system. Commented Jan 22, 2020 at 14:08

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks :) I also found that there is an issue in my appender, I fixed it. I will include the updated code.
I am Glad you solved ! let me translate I dont understand, not english :)
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
Yes! its right way! you can check here if need more info : svn.apache.org/repos/asf/logging/log4php/trunk/src/main/php
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.