Use logger to log exceptions instead of printStackTrace(...)

This commit is contained in:
Ayman Abdel Ghany 2016-01-29 14:47:03 +02:00
parent 3f0214a8e3
commit 5ec0bd5bd6
6 changed files with 356 additions and 348 deletions

View File

@ -228,7 +228,7 @@ public class Dictionary {
try {
is = new FileInputStream(file.toFile());
} catch (FileNotFoundException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
}
try {
@ -393,7 +393,7 @@ public class Dictionary {
try {
is = new FileInputStream(file.toFile());
} catch (FileNotFoundException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
}
try {

View File

@ -7,9 +7,13 @@ import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
public class Monitor implements Runnable {
public static ESLogger logger= Loggers.getLogger("ik-analyzer");
private static CloseableHttpClient httpclient = HttpClients.createDefault();
/*
* 上次更改时间
@ -87,7 +91,7 @@ public class Monitor implements Runnable {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
}
}
}

View File

@ -24,11 +24,11 @@ public class Sleep {
Thread.sleep(num*60*60*1000);
return;
default:
logger.error("输入类型错误应为MSEC,SEC,MIN,HOUR之一");
System.err.println("输入类型错误应为MSEC,SEC,MIN,HOUR之一");
return;
}
} catch (InterruptedException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
}
}

View File

@ -34,6 +34,8 @@ import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.Version;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;
@ -45,6 +47,8 @@ import org.wltea.analyzer.core.Lexeme;
*/
public class SWMCQueryBuilder {
public static ESLogger logger= Loggers.getLogger("ik-analyzer");
/**
* 生成SWMCQuery
* @param fieldName
@ -78,7 +82,7 @@ public class SWMCQueryBuilder {
lexemes.add(l);
}
}catch(IOException e){
e.printStackTrace();
logger.error(e.getMessage(), e);
}
return lexemes;
}
@ -87,7 +91,7 @@ public class SWMCQueryBuilder {
/**
* 根据分词结果生成SWMC搜索
* @param fieldName
// * @param pathOption
// * @param pathOption
* @param quickMode
* @return
*/
@ -135,7 +139,7 @@ public class SWMCQueryBuilder {
Query q = qp.parse(keywordBuffer_Short.toString());
return q;
} catch (ParseException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
}
}else{
@ -145,7 +149,7 @@ public class SWMCQueryBuilder {
Query q = qp.parse(keywordBuffer.toString());
return q;
} catch (ParseException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
}
}
}

View File

@ -67,20 +67,20 @@ public class IKAnalzyerDemo {
ts.reset();
//迭代获取分词结果
while (ts.incrementToken()) {
logger.info(offset.startOffset() + " - " + offset.endOffset() + " : " + term.toString() + " | " + type.type());
System.out.println(offset.startOffset() + " - " + offset.endOffset() + " : " + term.toString() + " | " + type.type());
}
//关闭TokenStream关闭StringReader
ts.end(); // Perform end-of-stream operations, e.g. set the final offset.
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
} finally {
//释放TokenStream的所有资源
if(ts != null){
try {
ts.close();
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
}
}
}

View File

@ -110,39 +110,39 @@ public class LuceneIndexAndSearchDemo {
QueryParser qp = new QueryParser(fieldName, analyzer);
qp.setDefaultOperator(QueryParser.AND_OPERATOR);
Query query = qp.parse(keyword);
logger.info("Query = " + query);
System.out.println("Query = " + query);
//搜索相似度最高的5条记录
TopDocs topDocs = isearcher.search(query , 5);
logger.info("命中:" + topDocs.totalHits);
System.out.println("命中:" + topDocs.totalHits);
//输出结果
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (int i = 0; i < topDocs.totalHits; i++){
Document targetDoc = isearcher.doc(scoreDocs[i].doc);
logger.info("内容:" + targetDoc.toString());
System.out.println("内容:" + targetDoc.toString());
}
} catch (CorruptIndexException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
} catch (LockObtainFailedException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
} catch (ParseException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
} finally{
if(ireader != null){
try {
ireader.close();
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
}
}
if(directory != null){
try {
directory.close();
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
}
}
}