patternphpMinor
Logger class for MVC Framework
Viewed 0 times
mvcloggerforclassframework
Problem
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 -
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 = "
(-)
";
$html .= "
Component
Location
Classname
";
$query .= "
Component
Query
Time
";
foreach (self::$logText as $data => $messageData) {
foreach ($messageData as $key => $message) {
$classname = (isset($message['CLASSNAME'])) ? $message['CLASSNAME'] : "-";
$vars = (isset($message['VARS'])) ? $message['VARS'] : "-";
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 = "
(-)
";
$html .= "
Component
Location
Classname
";
$query .= "
Component
Query
Time
";
foreach (self::$logText as $data => $messageData) {
foreach ($messageData as $key => $message) {
$classname = (isset($message['CLASSNAME'])) ? $message['CLASSNAME'] : "-";
$vars = (isset($message['VARS'])) ? $message['VARS'] : "-";
Solution
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:
Nowdoc:
If you are logging to HTML purely because it's easier to get the data that you want, you may want to check out
When doing multiline strings, you should use either heredoc or nowdoc syntax.
Heredoc:
$query .=
$data
{$message['QUERY']}
{$message['TIME']}
EOHTML;Nowdoc:
return
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;
}
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.Code Snippets
$query .= <<<EOHTML
<tr>
<td>$data</td>
<td colspan="2">{$message['QUERY']}</td>
<td>{$message['TIME']}</td>
</tr>
EOHTML;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;Context
StackExchange Code Review Q#67765, answer score: 2
Revisions (0)
No revisions yet.