0

I need to add query logging in Symfony and in csv file. Sometime database can be busy or not available but I want to log all queries.

This log should be in csv format with columns:

-url

-datasource name

-SQL content

-parameters

-username

-start time of query execution (accuracy in ms)

-end time of query execution (accuracy in ms)

Is any help how can I do this? or what can be done for this?

Maybe Custom Function In which I can create the csv file with the current login user info with the url and time to execute the query to access that particular url?

1 Answer 1

1

In Symfony If you have a common SQL function which runs when each query executes then this one helpful for you.

This is the thing I have implemented in my case. Hope This may help you.

public function execute($parameters = null)
{
if ($this->executed) {
return $this;
}
$executionStartTime = microtime(true);

$stmt = $this->execute($parameters);
$this->result = $stmt->fetchAll();
// Close cursor to allow query caching.
$stmt->closeCursor();
$this->executed = true;;

$executionEndTime = microtime(true);

//below code is added to logging for SQL queries to web server in csv format
$execution_time = $executionEndTime - $executionStartTime;
$getpageParameter = $parameters;
$getusername = $this->getUser()->getUsername();
$url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

$parms = json_encode($getpageParameter);

$data = array(
date ("Y-m-d H:i:s")."|".$url."|".$getusername."|".$parms."|".$executionStartTime."|".$executionEndTime,
);

if(!file_exists('/../../app/logs/querylog.csv')){
$column = array(
"DATE & TIME|URL|USERNAME|PARAMETERS|START TIME|END TIME"
);
$fp = fopen('/../../app/logs/querylog.csv', 'a+');
foreach ( $column as $line ) {
$val = explode("|", $line);
fputcsv($fp, $val);
}
fclose($fp);
}

$fp = fopen('/../../app/logs/querylog.csv', 'a+');
foreach ( $data as $line ) {
$val = explode("|", $line);
fputcsv($fp, $val);
}
fclose($fp);

return $this;
}
Sign up to request clarification or add additional context in comments.

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.