diff --git a/classes/Endpoint.php b/classes/Endpoint.php
index 0474d5fb30eda42ed590aac63c89bb5b71196111..49a3dbedca8bd31d0940930170eca92c2be751f8 100644
--- a/classes/Endpoint.php
+++ b/classes/Endpoint.php
@@ -34,7 +34,7 @@ class Endpoint{
     if(is_int($conf['cache']['global']) && $conf['cache']['global'] > 0 && $modified + $conf['cache']['global'] > $now){
      if($conf['debug']){
        $msg = "Taking data from cache ($cacheFile). Renewal in ".($modified + $conf['cache']['global'] - $now)." seconds\n";
-       trigger_error($msg);
+       Utils::log($msg);
        echo $msg;
      }
        $aux = (file_get_contents($cacheFile));
@@ -53,7 +53,7 @@ class Endpoint{
       $aux = curl_exec($c); // execute the curl command 
       if($conf['debug']){
         if($aux == false){
-          trigger_error("Error executing SPARQL query (".$this->sparqlUrl."): ".curl_error($c), E_USER_ERROR);
+          Utils::log("Error executing SPARQL query (".$this->sparqlUrl."): ".curl_error($c), E_USER_ERROR);
           echo("Error executing SPARQL query (".$this->sparqlUrl."): ".curl_error($c));
         }
       }
@@ -64,7 +64,7 @@ class Endpoint{
         file_put_contents($cacheFile,($aux), LOCK_EX);
         if($conf['debug']){
           $msg = "Notice: Writing results in ".$cacheFile."\n";
-          trigger_error($msg, E_USER_NOTICE);
+          Utils::log($msg, E_USER_NOTICE);
           echo($msg);
         }
       }
@@ -73,7 +73,7 @@ class Endpoint{
       $r = json_decode($aux, true);
       if($conf['debug']){
         if($r == false){
-          trigger_error("Warning: Results from a SELECT sparql query couldn't get parsed", E_USER_WARNING);
+          Utils::log("Warning: Results from a SELECT sparql query couldn't get parsed", E_USER_WARNING);
           echo("Warning: Results from a SELECT sparql query couldn't get parsed");
         }
       }
diff --git a/classes/Importer.php b/classes/Importer.php
index 1835537bcd99c6da9a7a7821ba35a77095c88e0f..3a4374b8c5e2e48040efe0f670f25ddbf2e59204 100644
--- a/classes/Importer.php
+++ b/classes/Importer.php
@@ -127,7 +127,7 @@ class Importer{
  	  }elseif($compType == LS."LodspeakrVisualComponent"){
  	  	$this->createViews($inputs);
  	  }else{
- 	  	trigger_error("Component '$component' (of type $compType) not supported", E_USER_WARNING);
+ 	  	Utils::log("Component '$component' (of type $compType) not supported", E_USER_WARNING);
  	  }
  	}
  	$content .= "?>\n";
diff --git a/classes/MetaDb.php b/classes/MetaDb.php
index 136b8a7715bd1104a937abe3b65d798497a3274b..1ac1ea19428ba15ca04a641a1991297875432729 100644
--- a/classes/MetaDb.php
+++ b/classes/MetaDb.php
@@ -35,7 +35,7 @@ class MetaDb{
 			$db = NULL;
 		}catch(Exception $e){
 				echo "Can't write in SQLite database. Please check you have granted write permissions to <tt>meta/</tt> and <tt>meta/db.sqlite</tt>.<br/>Also you can check a list of <a href='https://github.com/alangrafu/lodspeakr/wiki/CommonErrors'>common errors</a> and how to <a href='https://github.com/alangrafu/lodspeakr/wiki/Wipe-out-the-database'>wipe out the database</a>'";
-		  		trigger_error('Exception exec: '.$e->getMessage(), E_USER_ERROR);
+		  		Utils::log('Exception exec: '.$e->getMessage(), E_USER_ERROR);
 				exit(1);
 		}
         return $results;
diff --git a/classes/Utils.php b/classes/Utils.php
index 545975e8dfbd51f9c99e61cbf96ff8649f1c8d40..95ab1cdc44e9a5a2ebf79286b65ed1000969a8cb 100644
--- a/classes/Utils.php
+++ b/classes/Utils.php
@@ -270,7 +270,7 @@ class Utils{
   	$lodspk['model'] = $modelDir;
   	$originalDir = getcwd();
   	$subDirs= array();
-  	trigger_error("Entering $strippedModelDir from ".getcwd(), E_USER_NOTICE);
+  	Utils::log("Entering $strippedModelDir from ".getcwd(), E_USER_NOTICE);
   	chdir($modelDir);
   	$handle = opendir('.');
   	
@@ -278,16 +278,16 @@ class Utils{
   	  if($modelFile != "." && $modelFile != ".." && strpos($modelFile, ".") !== 0){
   	  	if(is_dir($modelFile)){
   	  	  if(strpos('endpoint.', $modelFile) == 0){
-  	  	  	trigger_error("Save $modelFile for later, after all the queries in the current directory has been resolved", E_USER_NOTICE);
+  	  	  	Utils::log("Save $modelFile for later, after all the queries in the current directory has been resolved", E_USER_NOTICE);
   	  	  	$subDirs[]=$modelFile;
   	  	  }
   	  	}else{
   	  	  if(preg_match('/\.query$/', $modelFile)){
   	  	    $e = null;
   	  	    if(!isset($endpoints[$strippedModelDir])){
-  	  	      trigger_error("Creating endpoint for $strippedModelDir", E_USER_NOTICE);
+  	  	      Utils::log("Creating endpoint for $strippedModelDir", E_USER_NOTICE);
   	  	      if(!isset($conf['endpoint'][$strippedModelDir])){
-  	  	        trigger_error("Couldn't find $strippedModelDir as a list of available endpoints. Will continue using local", E_USER_WARNING);
+  	  	        Utils::log("Couldn't find $strippedModelDir as a list of available endpoints. Will continue using local", E_USER_WARNING);
   	  	        $e = $endpoints['local'];
   	  	      }else{  
   	  	        $endpoints[$strippedModelDir] = new Endpoint($conf['endpoint'][$strippedModelDir], $conf['endpoint']['config']);
@@ -432,16 +432,15 @@ class Utils{
 	  	}
 	  }
   	  if($conf['debug']){
-  	  	echo "\n-------------------------------------------------\nIn ".getcwd()."\n";
-  	    echo "$modelFile (against ".$e->getSparqlUrl().")\n";
-  	  	echo $query."\n";
+  	    Utils::log($modelFile." against ".$e->getSparqlUrl());
+  	  	Utils::log($query);
+    	  Utils::log("Running query from ".$modelFile." on endpoint ".$e->getSparqlURL(), E_USER_NOTICE);
   	  }
-  	  trigger_error("Running query from ".$modelFile." on endpoint ".$e->getSparqlURL(), E_USER_NOTICE);
   	  $initTime = microtime(true);
   	  $aux = $e->query($query, Utils::getResultsType($query));
   	  $endTime = microtime(true);
   	  if($conf['debug']){
-  	    echo "Execution time: ".($endTime - $initTime)." seconds\n-------------------------------------------------\n\n";
+  	    Utils::log("Execution time: ".($endTime - $initTime)." seconds");
   	  }
   	  $timeObj = new stdClass();
   	  $timeObj->query = new stdClass();
@@ -473,7 +472,7 @@ class Utils{
   	}else{
   	  if(strpos('endpoint.', $modelFile) == 0){
   	  	
-  	  	trigger_error("$modelFile is a directory, will process it later", E_USER_NOTICE);
+  	  	Utils::log("$modelFile is a directory, will process it later", E_USER_NOTICE);
   	  	if($modelFile != $lodspk['type']){
   	  	  if(!isset($rPointer[$strippedModelFile])){
   	  	  	$rPointer[$strippedModelFile] = array();
@@ -565,7 +564,7 @@ class Utils{
   	//unset($lodspk);
   	$vars = compact('uri','lodspk', 'conf',  'models', 'first');
  	if($conf['debug']){
- 	  var_dump($vars); 	
+ 	  //Utils::log(var_export($vars, true)); 	
  	}
 	if(is_string($data)){
 	  echo($data);
@@ -637,6 +636,12 @@ class Utils{
     return $result;
   }
   
+  public static function log($msg){
+    global $conf;
+    if($conf['logfile'] != null){
+      fwrite($conf['logfile'], time()."\t".$msg."\n");
+    }
+  }
 }
 
 ?>
diff --git a/classes/modules/serviceModule.php b/classes/modules/serviceModule.php
index 3dd168c2499bc343a2e3615a74d65e6de51a8e48..1a541a0a0250e0e99a7ad423b8633ec948c57ea2 100644
--- a/classes/modules/serviceModule.php
+++ b/classes/modules/serviceModule.php
@@ -196,7 +196,7 @@ class ServiceModule extends abstractModule{
   	  Utils::processDocument($viewFile, $lodspk, $results);    	  
   	}catch (Exception $ex){
   	  echo $ex->getMessage();
-  	  trigger_error($ex->getMessage(), E_ERROR);
+  	  Utils::log($ex->getMessage(), E_ERROR);
   	  HTTPStatus::send500($uri);
   	}
   	exit(0);	
diff --git a/classes/modules/sparqlFilterModule.php b/classes/modules/sparqlFilterModule.php
index c2dc660e5e0b7097948251838919af9faf9ca2db..5751c2220bb2371e67dacd33d5fd34ed5e3c7a0b 100644
--- a/classes/modules/sparqlFilterModule.php
+++ b/classes/modules/sparqlFilterModule.php
@@ -185,7 +185,7 @@ public function execute($pair){
   	  	  $lodspk['transform_select_query'] = true;
   	  	  $objResult['viewFile'] = null;
   	  	}
-  	  	trigger_error("LODSPeaKr can't find the proper query. Using HTML query instead.", E_USER_NOTICE);
+  	  	Utils::log("LODSPeaKr can't find the proper query. Using HTML query instead.", E_USER_NOTICE);
   	  	break;
   	  }
   	}
diff --git a/classes/modules/staticModule.php b/classes/modules/staticModule.php
index 594b6c17e6f9e833948e97fb623b1c0acd20bdc5..3c41ac98750b507cfb5fecf9718328f6ebc31892 100644
--- a/classes/modules/staticModule.php
+++ b/classes/modules/staticModule.php
@@ -30,8 +30,7 @@ class StaticModule extends abstractModule{
   	header("Content-type: ".$ct);
   	$uri = $localUri;
   	if($conf['debug']){
-  	  echo "\n-------------------------------------------------\nIn ".$conf['static']['directory']."\n";
-  	  echo "Static file $file\n-------------------------------------------------\n\n";
+  	  Utils::log("In ".$conf['static']['directory']." static file $file");
 	  }
 	  $htmlExtension = 'html';
 	  if($conf['static']['haanga'] && substr_compare($file, $htmlExtension, -strlen($htmlExtension), strlen($htmlExtension)) === 0){
diff --git a/classes/modules/typeModule.php b/classes/modules/typeModule.php
index 38b7a279ea91ad88e0d8c9e2a27304496164b48a..c2682765511f0200ac4aeb961a17674ffc03d59b 100644
--- a/classes/modules/typeModule.php
+++ b/classes/modules/typeModule.php
@@ -155,7 +155,7 @@ class TypeModule extends abstractModule{
   	  	  $lodspk['transform_select_query'] = true;
   	  	  $objResult['viewFile'] = null;
   	  	}
-  	  	trigger_error("LODSPeaKr can't find the proper query. Using HTML query instead.", E_USER_NOTICE);
+  	  	Utils::log("LODSPeaKr can't find the proper query. Using HTML query instead.", E_USER_NOTICE);
   	  	break;
   	  }else{
   	    $found = false;
diff --git a/classes/modules/uriModule.php b/classes/modules/uriModule.php
index fd933a4324a56e638048aa840bf427687a299694..78be32985a523b13e9d2e6dc0d07f299f8326707 100644
--- a/classes/modules/uriModule.php
+++ b/classes/modules/uriModule.php
@@ -120,7 +120,7 @@ class UriModule extends abstractModule{
   	}
   	//chdir($conf['home']);
   	if($conf['debug']){
-  	  trigger_error("Using template ".$viewFile, E_USER_NOTICE);
+  	  Utils::log("Using template ".$viewFile, E_USER_NOTICE);
   	  echo("TEMPLATE: ".$viewFile."\n\n");
   	}
   	Utils::processDocument($viewFile, $lodspk, $resultsObj);
diff --git a/index.php b/index.php
index 87e511187282caaecbf5d66f0037b7bf1529d33a..56d75648366fe38e827df7614ae803769762514b 100755
--- a/index.php
+++ b/index.php
@@ -1,7 +1,7 @@
 <?php
 
 //Import
-if($_GET['q'] == 'import'){
+if(isset($_GET['q']) && $_GET['q'] == 'import'){
   include_once('classes/Importer.php');
   $imp = new Importer();
   $imp->run();
@@ -15,10 +15,12 @@ if(!file_exists('settings.inc.php')){
 }
 
 include_once('common.inc.php');
-
 //Debug output
+
+$conf['logfile'] = null;
 if($conf['debug']){
-  error_reporting(E_ALL);
+  $conf['logfile'] = fopen("cache/log_".time().rand().".log", "w");
+  //error_reporting(E_ALL);
 }else{
   error_reporting(E_ERROR);
 }
@@ -90,6 +92,9 @@ foreach($conf['modules']['available'] as $i){
   $matching = $module->match($uri);
   if($matching != FALSE){
   	$module->execute($matching);
+  	if($conf['logfile'] != null){
+  	  fclose($conf['logfile']);
+  	}
   	exit(0);
   }
 }