I have a web service written in PHP, that returns JSON results. It works well and has been thoroughly tested.
I needed to add logging to the service calls (who called and when). I implemented this using a SQLite database (for very small amount of data) and PDO. The one condition to the logging was that it should not harm the web service itself. The fact that I'm logging his call should not matter to the user, nor should any error in the logging process block any service call.
I implemented a Log class and an "execute" function that expects SQL strings:
function __construct() {
$this->dbConn = 'sqlite:' . realpath("myLog.sqlite");
try {
$this->db = new PDO($this->dbConn);
}
catch (PDOException $e) {
//I don't care
}
}
private function execute($sql) {
try {
$query = $this->db->prepare($sql);
$query->execute();
}
catch(Exception $e) {
//I don't care
}
}
I then added at the end of every service function a call similar to this:
$sql = "INSERT INTO $table(id, Time) VALUES('$id', datetime('now','localtime'))";
$this->execute($sql);
$jsonResult = doSomething($id);
return $jsonReuslt;
The problem is: from time to time, I get a string on the client that contains a fatal error string (pointing to this line $query->execute()
) saying it's not an object.
In all cases so far, I traced it to an erroneous SQL query. But what I need is a way to make sure the original JSON gets returned even if there is an error in the logging function.
How can I ensure that no matter what happens when I call $this->execute($sql)
- $jsonResult will return with the actual JSON results?
TIA,
Guy