4
\$\begingroup\$

I have created a logger class for my own framework. Now I am trying to identify the components which can be done in a better way. The logger class doesn't log anything inside a file but it logs the files that are getting loaded (say controller, model, etc.) and displays it in HTML. I think it's bad practice to output HTML in a logger class but I have not read this anywhere. The reason I am outputting HTML is because when you are in a URL like - localhost/controller/method and the logger is turned on, then on this page it will display all the files (filename, classname and method) that has been loaded to fulfill the request.

Is it ok to do it this way?

class EH_Logger {

    /**
     * Holds logs
     * @var array 
     */
    static $logText = array();

    /**
     * Holds the time taken for rendering the application
     * @var string 
     */
    static $loadingTime;


    /**
     * Structures the table that needs to be displayed.
     */
    static function printLog() {

        $time = microtime(true);
        $query = "";
        $query_exist = false;

        self::$loadingTime  = $time - STARTTIME;

        $html  = "<div id='logTableHolder'>
                    <a href='' 
                        onClick=\"var displayObj = document.getElementById('logtable');
                                    if(displayObj.style.display=='none') {
                                        displayObj.style.display ='';
                                        this.innerHTML='(-)';
                                    } else {
                                        displayObj.style.display='none'; 
                                        this.innerHTML='(+)';
                                    }
                                    return false\">
                        (-)
                    </a>";
        $html .= "<table id='logtable' border='1'><thead>
                        <tr class='log_header'>
                            <th>Component</th>                            
                            <th>Location</th>
                            <th>Classname</th>                             
                        </tr></thead>";

        $query .= "     <tbody><tr></tr>
                        <tr class='log_header'>
                            <th>Component</th>                            
                            <th colspan='2'>Query</th>
                            <th>Time</th>                            
                        </tr>";

        foreach (self::$logText as $data => $messageData) {

            foreach ($messageData as $key => $message) {

                $classname = (isset($message['CLASSNAME'])) ? $message['CLASSNAME'] : "-";
                $vars = (isset($message['VARS'])) ? $message['VARS'] : "-";

                if($data == "DATABASE") {

                    $query .= "<tr>
                                <td>$data</td>                                
                                <td colspan='2'>" . $message['QUERY'] . "</td>
                                <td>" . $message['TIME'] . "</td>                                
                              </tr>";

                    $query_exist = true;

                } else {

                    $html .= "<tr>
                                    <td>$data</td>                                
                                    <td>" . str_replace('\\', '/', $message['PAGENAME']) . "</td>
                                    <td>" . $classname . "</td>

                            </tr>";

                }
            }
        }

        $html .= ($query_exist)?$query:"";
        $html .= "<tr><td colspan='4'>".sprintf ("This page took <strong>%f</strong> seconds to load.", self::$loadingTime );".</td></tr>";
        $html .= "</tbody></table></div>";

        echo $html;

        echo self::applyLoggerCSS();
    }

    /**
     * Apply CSS to the table
     * @return string 
     */
    public static function applyLoggerCSS() {

        return '<style type="text/css">   
                    table#logtable {
                        background: none repeat scroll 0 0 #FFFFFF;
                        border: 6px solid #EEEEEE;
                        border-collapse: collapse;
                        box-shadow: 0 0 30px 0 #454545;
                        color: #6C6C6C;
                        font: 11px/24px Verdana,Arial,Helvetica,sans-serif;
                        width: 100%;
                        text-align: left;

                    }
                    #logTableHolder {
                        position: absolute;
                        width: 60%;
                        left: 50%;
                        margin-left: -30%;
                        top: 25px;
                        z-index: 999;
                    }

                    #logTableHolder a {
                        background: none repeat scroll 0 0 #EEEEEE;
                        border-radius: 2px 2px 2px 2px;
                        left: 97%;
                        margin-bottom: 29px;
                        padding: 0 4px;
                        position: absolute;
                    }

                    #logtable th {
                        padding: 0 0.5em;
                        text-align: left;
                    }

                    #logtable tr.yellow td {
                        border-top: 1px solid #FB7A31;
                        border-bottom: 1px solid #FB7A31;
                        background: #FFC;
                    }

                    #logtable td {
                        border-bottom: 1px solid #CCC;
                        padding: 0 0.5em;
                    }

                    #logtable td:first-child {
                        width: 190px;
                    }

                    #logtable td+td {
                        border-left: 1px solid #CCC;
                        text-align: left;
                    }
                </style>';

    }
}
\$\endgroup\$
2
\$\begingroup\$

As a general rule, if you are logging to the screen, you aren't production ready. There may be situations where you can limit the display to just when you are logged in, but in general, you log to files so that only you can view them.

When doing multiline strings, you should use either heredoc or nowdoc syntax.

Heredoc:

                $query .= <<<EOHTML
  <tr>
    <td>$data</td>                                
    <td colspan="2">{$message['QUERY']}</td>
    <td>{$message['TIME']}</td>                                
  </tr>

EOHTML;

Nowdoc:

    return <<<'EOCSS'
<style type="text/css">   
  table#logtable {
    background: none repeat scroll 0 0 #FFFFFF;
    border: 6px solid #EEEEEE;
    border-collapse: collapse;
    box-shadow: 0 0 30px 0 #454545;
    color: #6C6C6C;
    font: 11px/24px Verdana,Arial,Helvetica,sans-serif;
    width: 100%;
    text-align: left;
  }
</style>
EOCSS;

If you are logging to HTML purely because it's easier to get the data that you want, you may want to check out debug_backtrace. That will give you more verbose output that you can log.

\$\endgroup\$
  • \$\begingroup\$ if you are logging to the screen, you aren't production ready.Very well said. Use Heredoc. I will follow both the instructions. \$\endgroup\$ – Abhishek Saha Oct 24 '14 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.