Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Rails type logging to file of queries with times and colorization in terminal #1075

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions lib/db/sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class SQL {
//! SQL log
$log;

// SQL Logging
use SQL\Logger;

/**
* Begin SQL transaction
* @return bool
Expand Down Expand Up @@ -194,12 +197,14 @@ function exec($cmds,$args=NULL,$ttl=0,$log=TRUE,$stamp=FALSE) {
$keys[]='/'.preg_quote(is_numeric($key)?chr(0).'?':$key).
'/';
}
if ($log)
if ($log) {
$this->log_query(preg_replace($keys, $vals, str_replace('?',chr(0).'?',$cmd),1), 1e3*(microtime(TRUE)-$now), "CACHED");
$this->log.=($stamp?(date('r').' '):'').'('.
sprintf('%.1f',1e3*(microtime(TRUE)-$now)).'ms) '.
'[CACHED] '.
preg_replace($keys,$vals,
str_replace('?',chr(0).'?',$cmd),1).PHP_EOL;
sprintf('%.1f',1e3*(microtime(TRUE)-$now)).'ms) '.
'[CACHED] '.
preg_replace($keys,$vals,
str_replace('?',chr(0).'?',$cmd),1).PHP_EOL;
}
}
elseif (is_object($query=$this->pdo->prepare($cmd))) {
foreach ($arg as $key=>$val) {
Expand All @@ -219,15 +224,17 @@ function exec($cmds,$args=NULL,$ttl=0,$log=TRUE,$stamp=FALSE) {
$keys[]='/'.preg_quote(is_numeric($key)?chr(0).'?':$key).
'/';
}
if ($log)
if ($log)
$this->log.=($stamp?(date('r').' '):'').' (-0ms) '.
preg_replace($keys,$vals,
str_replace('?',chr(0).'?',$cmd),1).PHP_EOL;
$query->execute();
if ($log)
if ($log) {
$this->log_query(preg_replace($keys, $vals, str_replace('?',chr(0).'?',$cmd),1), 1e3*(microtime(TRUE)-$now));
$this->log=str_replace('(-0ms)',
'('.sprintf('%.1f',1e3*(microtime(TRUE)-$now)).'ms)',
$this->log);
}
$error=$query->errorinfo();
if ($error[0]!=\PDO::ERR_NONE) {
// Statement-level error occurred
Expand Down
47 changes: 47 additions & 0 deletions lib/db/sql/logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace DB\SQL;

/**
* Rails Type Logging with color in terminal (tail -f sql.log).
* To use enable or disable
* DB\SQL::$log_sql_enabled = $_ENV['ENVIRONMENT'] != 'production' ? true : false;
* By defaul sets to sql.log or you can set your own log file
* DB\SQL::$logger = new \Log("sql.log");
**/
trait Logger {

public static $log_line_max_length = 50000;
public static $log_color = 35;
public static $log_sql_enabled = false;
public static $logger = null;
public $model_class = null;

protected function log_query($query, $duration = null, $name = null) {
if(self::$log_sql_enabled && $query) {
self::$log_color = self::$log_color == 35 ? 36 : 35;
$color = self::$log_color;
$bold = $color == 36 ? "\e[1m" : '';
$duration = is_numeric($duration) ? " (".round($duration, 1)."ms)" : '';
$name = $name ? $name : $this->model_class." Load";
self::_log("\e[{$color}m{$name}{$duration}\e[0m {$bold}".$query."\e[0m");
}
}

protected static function _log($message) {
if(self::$log_sql_enabled && $message) {
if(strlen($message) > self::$log_line_max_length) {
$message = substr($message, 0, self::$log_line_max_length)."...";
}
if(!self::$logger) {
self::$logger = new \Log("sql.log");
}
if(is_object(self::$logger)) {
self::$logger->write($message);
} else {
error_log($message);
}
}
}

}