diff --git a/apps/contacts/ajax/addproperty.php b/apps/contacts/ajax/addproperty.php
index f888b94e386e18cad4c72d58421b6f26cf2b489a..1b6db0c8f81ac2b20e8ccbf4cec92e7623e74308 100644
--- a/apps/contacts/ajax/addproperty.php
+++ b/apps/contacts/ajax/addproperty.php
@@ -25,11 +25,7 @@ OCP\JSON::checkLoggedIn();
 OCP\JSON::checkAppEnabled('contacts');
 OCP\JSON::callCheck();
 
-function bailOut($msg) {
-	OCP\JSON::error(array('data' => array('message' => $msg)));
-	OCP\Util::writeLog('contacts','ajax/addproperty.php: '.$msg, OCP\Util::DEBUG);
-	exit();
-}
+require_once('loghandler.php');
 
 $id = isset($_POST['id'])?$_POST['id']:null;
 $name = isset($_POST['name'])?$_POST['name']:null;
diff --git a/apps/contacts/ajax/categories/delete.php b/apps/contacts/ajax/categories/delete.php
index bee2dbe3f6b7411d96fafd22e8b163e4ad8fd819..76c23d6487d1747980804fa138230b53b3121862 100644
--- a/apps/contacts/ajax/categories/delete.php
+++ b/apps/contacts/ajax/categories/delete.php
@@ -10,18 +10,7 @@
 OCP\JSON::checkLoggedIn();
 OCP\JSON::checkAppEnabled('contacts');
 
-foreach ($_POST as $key=>$element) {
-	debug('_POST: '.$key.'=>'.print_r($element, true));
-}
-
-function bailOut($msg) {
-	OCP\JSON::error(array('data' => array('message' => $msg)));
-	OCP\Util::writeLog('contacts','ajax/categories/delete.php: '.$msg, OCP\Util::DEBUG);
-	exit();
-}
-function debug($msg) {
-	OCP\Util::writeLog('contacts','ajax/categories/delete.php: '.$msg, OCP\Util::DEBUG);
-}
+require_once('../loghandler.php');
 
 $categories = isset($_POST['categories'])?$_POST['categories']:null;
 
diff --git a/apps/contacts/ajax/categories/rescan.php b/apps/contacts/ajax/categories/rescan.php
index 84a67dec0b1832da19e4c2b83a59abe7719f2790..48ec165381f48ad090ba2ae2d97aa9536736bf18 100644
--- a/apps/contacts/ajax/categories/rescan.php
+++ b/apps/contacts/ajax/categories/rescan.php
@@ -10,18 +10,7 @@
 OCP\JSON::checkLoggedIn();
 OCP\JSON::checkAppEnabled('contacts');
 
-foreach ($_POST as $key=>$element) {
-	debug('_POST: '.$key.'=>'.print_r($element, true));
-}
-
-function bailOut($msg) {
-	OCP\JSON::error(array('data' => array('message' => $msg)));
-	OCP\Util::writeLog('contacts','ajax/categories/rescan.php: '.$msg, OCP\Util::DEBUG);
-	exit();
-}
-function debug($msg) {
-	OCP\Util::writeLog('contacts','ajax/categories/rescan.php: '.$msg, OCP\Util::DEBUG);
-}
+require_once('../loghandler.php');
 
 $addressbooks = OC_Contacts_Addressbook::all(OCP\USER::getUser());
 if(count($addressbooks) == 0) {
diff --git a/apps/contacts/ajax/contactdetails.php b/apps/contacts/ajax/contactdetails.php
index a6ca10f9c942ab591d4dc09eeb81e69e22c31a7b..b697b1a8e5be3433244a69c4f66780758d712e0e 100644
--- a/apps/contacts/ajax/contactdetails.php
+++ b/apps/contacts/ajax/contactdetails.php
@@ -20,11 +20,7 @@
  *
  */
  
-function bailOut($msg) {
-	OCP\JSON::error(array('data' => array('message' => $msg)));
-	OCP\Util::writeLog('contacts','ajax/contactdetails.php: '.$msg, OCP\Util::DEBUG);
-	exit();
-}
+require_once('loghandler.php');
 
 // Check if we are a user
 OCP\JSON::checkLoggedIn();
diff --git a/apps/contacts/ajax/createaddressbook.php b/apps/contacts/ajax/createaddressbook.php
index 616766bb1a0c18c02d09da08606e9da954414168..2ec5f542bb39805cdc3c8944e6e670f2eebf06a3 100644
--- a/apps/contacts/ajax/createaddressbook.php
+++ b/apps/contacts/ajax/createaddressbook.php
@@ -12,25 +12,20 @@
 OCP\JSON::checkLoggedIn();
 OCP\JSON::checkAppEnabled('contacts');
 OCP\JSON::callCheck();
+require_once('loghandler.php');
 
 $userid = OCP\USER::getUser();
 $name = trim(strip_tags($_POST['name']));
 if(!$name) {
-	OCP\JSON::error(array('data' => array('message' => OC_Contacts_App::$l10n->t('Cannot add addressbook with an empty name.'))));
-	OCP\Util::writeLog('contacts','ajax/createaddressbook.php: Cannot add addressbook with an empty name: '.strip_tags($_POST['name']), OCP\Util::ERROR);
-	exit();
+	bailOut('Cannot add addressbook with an empty name.');
 }
 $bookid = OC_Contacts_Addressbook::add($userid, $name, null);
 if(!$bookid) {
-	OCP\JSON::error(array('data' => array('message' => OC_Contacts_App::$l10n->t('Error adding addressbook.'))));
-	OCP\Util::writeLog('contacts','ajax/createaddressbook.php: Error adding addressbook: '.$_POST['name'], OCP\Util::ERROR);
-	exit();
+	bailOut('Error adding addressbook: '.$name);
 }
 
 if(!OC_Contacts_Addressbook::setActive($bookid, 1)) {
-	OCP\JSON::error(array('data' => array('message' => OC_Contacts_App::$l10n->t('Error activating addressbook.'))));
-	OCP\Util::writeLog('contacts','ajax/createaddressbook.php: Error activating addressbook: '.$bookid, OCP\Util::ERROR);
-	//exit();
+	bailOut('Error activating addressbook.');
 }
 $addressbook = OC_Contacts_App::getAddressbook($bookid);
 $tmpl = new OCP\Template('contacts', 'part.chooseaddressbook.rowfields');
diff --git a/apps/contacts/ajax/currentphoto.php b/apps/contacts/ajax/currentphoto.php
index 5f90128f3210a698b705a8ffafb56104020169af..b10e752c453159a696f0926cd2a827813d956e05 100644
--- a/apps/contacts/ajax/currentphoto.php
+++ b/apps/contacts/ajax/currentphoto.php
@@ -24,11 +24,7 @@
 OCP\JSON::setContentTypeHeader('text/plain');
 OCP\JSON::checkLoggedIn();
 OCP\JSON::checkAppEnabled('contacts');
-function bailOut($msg) {
-	OCP\JSON::error(array('data' => array('message' => $msg)));
-	OCP\Util::writeLog('contacts','ajax/currentphoto.php: '.$msg, OCP\Util::ERROR);
-	exit();
-}
+require_once('loghandler.php');
 
 if (!isset($_GET['id'])) {
 	bailOut(OC_Contacts_App::$l10n->t('No contact ID was submitted.'));
diff --git a/apps/contacts/ajax/deletecard.php b/apps/contacts/ajax/deletecard.php
index 2a6bd277d192202e9adb75f606a1803476c82674..1161c18abda22808f022333d184846d9d310d4ce 100644
--- a/apps/contacts/ajax/deletecard.php
+++ b/apps/contacts/ajax/deletecard.php
@@ -19,26 +19,21 @@
  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
  *
  */
-function bailOut($msg) {
-	OCP\JSON::error(array('data' => array('message' => $msg)));
-	OCP\Util::writeLog('contacts','ajax/saveproperty.php: '.$msg, OCP\Util::DEBUG);
-	exit();
-}
-
 // Check if we are a user
 OCP\JSON::checkLoggedIn();
 OCP\JSON::checkAppEnabled('contacts');
 OCP\JSON::callCheck();
+require_once('loghandler.php');
 
 // foreach($_SERVER as $key=>$value) {
 // 	OCP\Util::writeLog('contacts','ajax/saveproperty.php: _SERVER: '.$key.'=>'.$value, OCP\Util::DEBUG);
 // }
-foreach($_POST as $key=>$value) {
-	OCP\Util::writeLog('contacts','ajax/saveproperty.php: _POST: '.$key.'=>'.print_r($value, true), OCP\Util::DEBUG);
-}
-foreach($_GET as $key=>$value) {
-	OCP\Util::writeLog('contacts','ajax/saveproperty.php: _GET: '.$key.'=>'.print_r($value, true), OCP\Util::DEBUG);
-}
+// foreach($_POST as $key=>$value) {
+// 	debug($key.'=>'.print_r($value, true));
+// }
+// foreach($_GET as $key=>$value) {
+// 	debug($key.'=>'.print_r($value, true));
+// }
 
 $id = isset($_POST['id'])?$_POST['id']:null;
 if(!$id) {
diff --git a/apps/contacts/ajax/deleteproperty.php b/apps/contacts/ajax/deleteproperty.php
index 55f7e323083dd19e9e9bb9c2809480482612fd1d..90e5e64903e5058e75fdec6479858be6d57ee3a8 100644
--- a/apps/contacts/ajax/deleteproperty.php
+++ b/apps/contacts/ajax/deleteproperty.php
@@ -24,6 +24,7 @@
 OCP\JSON::checkLoggedIn();
 OCP\JSON::checkAppEnabled('contacts');
 OCP\JSON::callCheck();
+require_once('loghandler.php');
 
 $id = $_POST['id'];
 $checksum = $_POST['checksum'];
@@ -31,16 +32,14 @@ $checksum = $_POST['checksum'];
 $vcard = OC_Contacts_App::getContactVCard( $id );
 $line = OC_Contacts_App::getPropertyLineByChecksum($vcard, $checksum);
 if(is_null($line)){
-	OCP\JSON::error(array('data' => array( 'message' => OC_Contacts_App::$l10n->t('Information about vCard is incorrect. Please reload the page.'))));
+	bailOut(OC_Contacts_App::$l10n->t('Information about vCard is incorrect. Please reload the page.'));
 	exit();
 }
 
 unset($vcard->children[$line]);
 
 if(!OC_Contacts_VCard::edit($id,$vcard)) {
-	OCP\JSON::error(array('data' => array('message' => OC_Contacts_App::$l10n->t('Error deleting contact property.'))));
-	OCP\Util::writeLog('contacts','ajax/deleteproperty.php: Error deleting contact property', OCP\Util::ERROR);
-	exit();
+	bailOut(OC_Contacts_App::$l10n->t('Error deleting contact property.'));
 }
 
 OCP\JSON::success(array('data' => array( 'id' => $id )));
diff --git a/apps/contacts/ajax/editname.php b/apps/contacts/ajax/editname.php
index 62cae894b6f3d3216398c11563b9f27038473980..868ca222e0a71f28b51bf73f1ba645eecaf8dd3e 100644
--- a/apps/contacts/ajax/editname.php
+++ b/apps/contacts/ajax/editname.php
@@ -9,11 +9,7 @@
  
 OCP\JSON::checkLoggedIn();
 OCP\JSON::checkAppEnabled('contacts');
-function bailOut($msg) {
-	OCP\JSON::error(array('data' => array('message' => $msg)));
-	OCP\Util::writeLog('contacts','ajax/editname.php: '.$msg, OCP\Util::DEBUG);
-	exit();
-}
+require_once('loghandler.php');
 
 $tmpl = new OCP\Template("contacts", "part.edit_name_dialog");
 
diff --git a/apps/contacts/ajax/loadphoto.php b/apps/contacts/ajax/loadphoto.php
index eac7ca7d02f18d6184a0482d7777008ddb5a34bc..61b5356edce31c3bb28b3cd6d43c990d85ef037a 100644
--- a/apps/contacts/ajax/loadphoto.php
+++ b/apps/contacts/ajax/loadphoto.php
@@ -24,11 +24,7 @@
 OCP\JSON::checkLoggedIn();
 OCP\JSON::checkAppEnabled('contacts');
 
-function bailOut($msg) {
-	OCP\JSON::error(array('data' => array('message' => $msg)));
-	OCP\Util::writeLog('contacts','ajax/loadphoto.php: '.$msg, OCP\Util::DEBUG);
-	exit();
-}
+require_once('loghandler.php');
 
 $id = isset($_GET['id']) ? $_GET['id'] : '';
 $refresh = isset($_GET['refresh']) ? true : false;
diff --git a/apps/contacts/ajax/loghandler.php b/apps/contacts/ajax/loghandler.php
new file mode 100644
index 0000000000000000000000000000000000000000..831b2e50c1eb7639601d5e779ba0868ecb454c2b
--- /dev/null
+++ b/apps/contacts/ajax/loghandler.php
@@ -0,0 +1,40 @@
+<?php
+/**
+ * ownCloud - Addressbook
+ *
+ * @author Thomas Tanghus
+ * @copyright 2012 Thomas Tanghus <thomas@tanghus.net>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public
+ * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+function bailOut($msg, $tracelevel=1, $debuglevel=OCP\Util::ERROR) {
+	OCP\JSON::error(array('data' => array('message' => $msg)));
+	debug($msg, $tracelevel, $debuglevel);
+	exit();
+}
+
+function debug($msg, $tracelevel=0, $debuglevel=OCP\Util::DEBUG) {
+	if(PHP_VERSION >= "5.4") {
+		$call = debug_backtrace(false, $tracelevel+1);
+	} else {
+		$call = debug_backtrace(false);
+	}
+	error_log('trace: '.print_r($call, true));
+	$call = $call[$tracelevel];
+	if($debuglevel !== false) {
+		OCP\Util::writeLog('contacts', $call['file'].'. Line: '.$call['line'].': '.$msg, $debuglevel);
+	}
+}
diff --git a/apps/contacts/ajax/oc_photo.php b/apps/contacts/ajax/oc_photo.php
index ea25ebf1763ad2d34da5c7d3f37453f31d2decf6..5c50ba92dbe20577916ff30ba72933a3821d2ce4 100644
--- a/apps/contacts/ajax/oc_photo.php
+++ b/apps/contacts/ajax/oc_photo.php
@@ -22,11 +22,7 @@
 // Check if we are a user
 OCP\JSON::checkLoggedIn();
 OCP\JSON::checkAppEnabled('contacts');
-function bailOut($msg) {
-	OCP\JSON::error(array('data' => array('message' => $msg)));
-	OCP\Util::writeLog('contacts','ajax/oc_photo.php: '.$msg, OCP\Util::ERROR);
-	exit();
-}
+require_once('loghandler.php');
 
 if(!isset($_GET['id'])) {
 	bailOut(OC_Contacts_App::$l10n->t('No contact ID was submitted.'));
diff --git a/apps/contacts/ajax/savecrop.php b/apps/contacts/ajax/savecrop.php
index 6faf6a173d5df798f01b96f470536bf8ca648f52..adce6be3b39fa2755a9594a902df6168373e3fd5 100644
--- a/apps/contacts/ajax/savecrop.php
+++ b/apps/contacts/ajax/savecrop.php
@@ -27,11 +27,7 @@ OCP\JSON::callCheck();
 // Firefox and Konqueror tries to download application/json for me.  --Arthur
 OCP\JSON::setContentTypeHeader('text/plain');
 
-function bailOut($msg) {
-	OCP\JSON::error(array('data' => array('message' => $msg)));
-	OCP\Util::writeLog('contacts','ajax/savecrop.php: '.$msg, OCP\Util::DEBUG);
-	exit();
-}
+require_once('loghandler.php');
 
 $image = null;
 
diff --git a/apps/contacts/ajax/saveproperty.php b/apps/contacts/ajax/saveproperty.php
index 6ee9ec90b56c53c6b0c8886286a7e94b76880a39..34fc3cc53512b0dfe7c31f4581e15a9d33210710 100644
--- a/apps/contacts/ajax/saveproperty.php
+++ b/apps/contacts/ajax/saveproperty.php
@@ -19,21 +19,11 @@
  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
  *
  */
-
-function bailOut($msg) {
-	OCP\JSON::error(array('data' => array('message' => $msg)));
-	OCP\Util::writeLog('contacts','ajax/saveproperty.php: '.$msg, OCP\Util::DEBUG);
-	exit();
-}
-function debug($msg) {
-	OCP\Util::writeLog('contacts','ajax/saveproperty.php: '.$msg, OCP\Util::DEBUG);
-}
-
+require_once('loghandler.php');
 // Check if we are a user
 OCP\JSON::checkLoggedIn();
 OCP\JSON::checkAppEnabled('contacts');
 OCP\JSON::callCheck();
-
 $id = isset($_POST['id'])?$_POST['id']:null;
 $name = isset($_POST['name'])?$_POST['name']:null;
 $value = isset($_POST['value'])?$_POST['value']:null;
diff --git a/apps/contacts/ajax/updateaddressbook.php b/apps/contacts/ajax/updateaddressbook.php
index 19b5b3bddce55ed9f0e299ed4bb1282778b1bb14..d3a772c727f71a6dd3b913caa66f5ee6ffbfbf85 100644
--- a/apps/contacts/ajax/updateaddressbook.php
+++ b/apps/contacts/ajax/updateaddressbook.php
@@ -11,27 +11,22 @@
 // Check if we are a user
 OCP\JSON::checkLoggedIn();
 OCP\JSON::checkAppEnabled('contacts');
+require_once('loghandler.php');
 
 $bookid = $_POST['id'];
 OC_Contacts_App::getAddressbook($bookid); // is owner access check
 
 $name = trim(strip_tags($_POST['name']));
 if(!$name) {
-	OCP\JSON::error(array('data' => array('message' => OC_Contacts_App::$l10n->t('Cannot update addressbook with an empty name.'))));
-	OCP\Util::writeLog('contacts','ajax/updateaddressbook.php: Cannot update addressbook with an empty name: '.strip_tags($_POST['name']), OCP\Util::ERROR);
-	exit();
+	bailOut(OC_Contacts_App::$l10n->t('Cannot update addressbook with an empty name.'));
 }
 
 if(!OC_Contacts_Addressbook::edit($bookid, $name, null)) {
-	OCP\JSON::error(array('data' => array('message' => $l->t('Error updating addressbook.'))));
-	OCP\Util::writeLog('contacts','ajax/updateaddressbook.php: Error adding addressbook: ', OCP\Util::ERROR);
-	//exit();
+	bailOut(OC_Contacts_App::$l10n->t('Error updating addressbook.'));
 }
 
 if(!OC_Contacts_Addressbook::setActive($bookid, $_POST['active'])) {
-	OCP\JSON::error(array('data' => array('message' => $l->t('Error (de)activating addressbook.'))));
-	OCP\Util::writeLog('contacts','ajax/updateaddressbook.php: Error (de)activating addressbook: '.$bookid, OCP\Util::ERROR);
-	//exit();
+	bailOut(OC_Contacts_App::$l10n->t('Error (de)activating addressbook.'));
 }
 
 $addressbook = OC_Contacts_App::getAddressbook($bookid);
diff --git a/apps/contacts/ajax/uploadimport.php b/apps/contacts/ajax/uploadimport.php
index 463ca161e566abe79e58a207ae0014be0101ea24..c1e9c8b1ad15e7e4ae6fed34f2374f79d33d4bfa 100644
--- a/apps/contacts/ajax/uploadimport.php
+++ b/apps/contacts/ajax/uploadimport.php
@@ -23,11 +23,7 @@
 // Check if we are a user
 OCP\JSON::checkLoggedIn();
 OCP\JSON::checkAppEnabled('contacts');
-function bailOut($msg) {
-	OCP\JSON::error(array('data' => array('message' => $msg)));
-	OCP\Util::writeLog('contacts','ajax/uploadimport.php: '.$msg, OCP\Util::ERROR);
-	exit();
-}
+require_once('loghandler.php');
 
 $view = OCP\Files::getStorage('contacts');
 $tmpfile = md5(rand());
diff --git a/apps/contacts/ajax/uploadphoto.php b/apps/contacts/ajax/uploadphoto.php
index 889de6a1f8b3e26af6913250f2ad23b50e332f5d..8545ca84eee22653b99a5b2ce1da0faeac56d601 100644
--- a/apps/contacts/ajax/uploadphoto.php
+++ b/apps/contacts/ajax/uploadphoto.php
@@ -27,14 +27,7 @@ OCP\JSON::callCheck();
 
 // Firefox and Konqueror tries to download application/json for me.  --Arthur
 OCP\JSON::setContentTypeHeader('text/plain');
-function bailOut($msg) {
-	OCP\JSON::error(array('data' => array('message' => $msg)));
-	OCP\Util::writeLog('contacts','ajax/uploadphoto.php: '.$msg, OCP\Util::DEBUG);
-	exit();
-}
-function debug($msg) {
-	OCP\Util::writeLog('contacts','ajax/uploadphoto.php: '.$msg, OCP\Util::DEBUG);
-}
+require_once('loghandler.php');
 
 // If it is a Drag'n'Drop transfer it's handled here.
 $fn = (isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : false);