Welcome Guest, Not a member yet? Register   Sign In
CI4 Whoops additions if and only if LOCALHOST
#1

(This post was last modified: 09-25-2019, 11:07 PM by John_Betong.)

I use Ubuntu 19.04 and have added these lines in index.php to clear the LOCALHOST log file:

file: index.php
PHP Code:
# MAYBE EMPTY TRASH IF AND ONLY IF LOCALHOST
  if( 'localhost'===$_SERVER['SERVER_NAME'] ) :
    $ok = @unlink('../writable/logs/log-' .date('Y-m-d') .'.php');
    $ok = @array_map('unlink'glob("../writable/debugbar/*.json"));
  endif; 

And also modified the following file:
  /app/Views/errors/html/production.php

PHP Code:
<?php // DECLARE(STRICT_TYPES=1);


?><!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="robots" content="noindex">

    <title>Whoops!</title>

    <style type="text/css">
        <?= preg_replace('#[\r\n\t ]+#'' 'file_get_contents(__DIR__ DIRECTORY_SEPARATOR 'debug.css')) ?>
        .whoops {
            background-color: #ddffff; color: #000;
            border: solid 1px #888;
            font-size: small;
            margin:0 auto; padding: 0.42em;
            width:88%; max-width:88em;
        }
        .ooo {margin: 0 0 2em 0; padding: 0;}
    </style>
</head>
<body>

    <div class="container text-center">
        <h1 class="XXXheadline"> Whoops! </h1>

        <p class="lead">We seem to have hit a snag. Please try again later...</p>
    </div>

    <?php 
        
# better if LOCALHOST defined in 'index.php'
        
if( 'localhost'===$_SERVER['SERVER_NAME'] ) :
            
$today 'logs/log-' .date('Y-m-d') .'.php';
            
$today highlight_file(WRITEPATH .$todayTRUE);

            echo 
'<div class="whoops">'
                        
.    '<dl>'
                        
.    '<dt><b> The last error which must be fixed </b></dt>'
                      .        '<dd><br>' .$today .'</dd>'
                        
.'</dl>'
                    
'</div>'
                    
;    
    endif;
    
?>    
    </div>    
</body>
</html> 

Output:
[Image: Whoops.png]I use Ubuntu 19.04 and have modified the following file for a better Whoops:
Reply
#2

Great
Reply
#3

whoops appear if your app in production mode. Solution for this, set ENVIRONTMENT in develope mode
Reply
#4

Why would you need to clear the log directory? Do you need disk space so badly?
Also @titounnes is right, just set the environnement to development and it will display the errors in a much better format!
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#5

>>> Why would you need to clear the log directory?

The log directory is only cleared on LOCALHOST. It is much simpler view the errors for this particular page rather than include all the historical logs - which no doubt are not relevant.

>>> Do you need disk space so badly?

No but why complicate the issue when debugging - only relevant errors are shown


>>> Also @titounnes is right, just set the environnement to development and it will display the errors in a much better format!

When the environment is set to development the scripts called to produce the page are not identical and may not even show any errors. Try adding this incorrect syntax:

file: /app/Config/Boot/production.php

$badScript = $x / ' A non-numeric value encountered';


Environment Results:

production show blank screen or default message.

development does not show any errors because "/app/Config/Boot/production.php" not called.
Reply
#6

(09-29-2019, 07:44 AM)John_Betong Wrote: When the environment is set to development the scripts called to produce the page are not identical and may not even show any errors. Try adding this incorrect syntax:

file: /app/Config/Boot/production.php

$badScript = $x / ' A non-numeric value encountered';


Environment Results:

production show blank screen or default message.

development does not show any errors because "/app/Config/Boot/production.php" not called.

Of course if you have an error in your production config file, the error will only happen on production environment.

I personally don't like having a script auto-deleting files. But if this works for you, that's ok too!
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#7

@includebeer

> Of course if you have an error in your production config file,
> the error will only happen on production environment.

The supplied example demonstrates that setting "environment to development..." will not detect any errors. I wonder if there are other situations where this could occur. I think sending either "production" or "development" to a common file would have solved the problem.


> I personally don't like having a script auto-deleting files.
> But if this works for you, that's ok too!

I personally don't like having to delete historical log files on my localhost and prefer a cleaner file structure.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB