Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

enhanced logging.

  • Loading branch information...
commit 2b375b4cdc4c6a5096ccef27b6d237e73b5a7da4 1 parent c75bcfe
@rnewson authored
View
1  README.md
@@ -355,6 +355,7 @@ couchdb-lucene respects several system properties;
<dl>
<dt>couchdb.url</dt><dd>the url to contact CouchDB with (default is "http://localhost:5984")</dd>
<dt>couchdb.lucene.dir</dt><dd>specify the path to the lucene indexes (the default is to make a directory called 'lucene' relative to couchdb's current working directory.</dd>
+<dt>couchdb.log.dir</dt><dd>specify the directory of the log file (which is called couchdb-lucene.log), defaults to the platform-specific temp directory.</dd>
</dl>
You can override these properties like this;
View
4 src/main/java/com/github/rnewson/couchdb/lucene/Config.java
@@ -37,7 +37,7 @@
static final String AUTHOR = "_author";
static final String INDEX_DIR = System.getProperty("couchdb.lucene.dir", "lucene");
-
+
static final int RAM_BUF = Integer.getInteger("couchdb.lucene.ram", 256);
static final int BATCH_SIZE = Integer.getInteger("couchdb.lucene.batch", 1000);
@@ -53,5 +53,5 @@
static final int COMMIT_MIN = Integer.getInteger("couchdb.lucene.commit.min", 10 * 1000);
static final int COMMIT_MAX = Integer.getInteger("couchdb.lucene.commit.max", 5 * 60 * 1000);
-
+
}
View
2  src/main/java/com/github/rnewson/couchdb/lucene/Database.java
@@ -55,7 +55,7 @@
CLIENT.getParams().setAuthenticationPreemptive(true);
final Credentials creds = new UsernamePasswordCredentials(Config.DB_USER, Config.DB_PASSWORD);
CLIENT.getState().setCredentials(AuthScope.ANY, creds);
- Log.log("Authenticating to couchdb as '%s'.", Config.DB_USER);
+ Utils.LOG.debug("Authenticating to couchdb as " + Config.DB_USER);
}
}
View
22 src/main/java/com/github/rnewson/couchdb/lucene/Index.java
@@ -89,7 +89,7 @@ public void run() {
try {
updateIndex();
} catch (final IOException e) {
- Log.log(e);
+ Utils.LOG.warn("Exception while updating index.", e);
}
}
}
@@ -99,7 +99,7 @@ private void sleep() {
try {
Thread.sleep(Config.COMMIT_MIN);
} catch (final InterruptedException e) {
- Log.log("Interrupted while sleeping, indexer is exiting.");
+ Utils.LOG.fatal("Interrupted while sleeping, indexer is exiting.", e);
}
}
@@ -121,7 +121,7 @@ private IndexWriter newWriter() throws IOException {
private synchronized void updateIndex() throws IOException {
if (IndexWriter.isLocked(dir)) {
- Log.log("Forcibly unlocking locked index at startup.");
+ Utils.LOG.warn("Forcibly unlocking locked index at startup.");
IndexWriter.unlock(dir);
}
@@ -146,8 +146,8 @@ private synchronized void updateIndex() throws IOException {
if (term == null || Config.DB.equals(term.field()) == false)
break;
if (Arrays.binarySearch(dbnames, term.text()) < 0) {
- Log.log("Database '%s' has been deleted," + " removing all documents from index.",
- term.text());
+ Utils.LOG.info("Database '" + term.text()
+ + "' has been deleted, removing all documents from index.");
delete(term.text(), progress, writer);
commit = true;
expunge = true;
@@ -182,7 +182,7 @@ private synchronized void updateIndex() throws IOException {
}
}
} catch (final Exception e) {
- Log.log(e);
+ Utils.LOG.error("Error updating index.", e);
commit = false;
} finally {
if (commit) {
@@ -194,8 +194,8 @@ private synchronized void updateIndex() throws IOException {
final IndexReader reader = IndexReader.open(dir);
try {
- Log.log("Committed changes to index (%,d documents in index, %,d deletes).", reader
- .numDocs(), reader.numDeletedDocs());
+ Utils.LOG.info("Committed changes to index (" + reader.numDocs() + " documents in index, "
+ + reader.numDeletedDocs() + " deletes).");
} finally {
reader.close();
}
@@ -218,7 +218,7 @@ private boolean updateDatabase(final IndexWriter writer, final String dbname, fi
// Reindex the database if sequence is 0 or signature changed.
if (progress.getSeq(dbname) == 0 || cur_sig.equals(new_sig) == false) {
- Log.log("Indexing '%s' from scratch.", dbname);
+ Utils.LOG.info("Indexing " + dbname + " from scratch.");
delete(dbname, progress, writer);
progress.update(dbname, new_sig, 0);
result = true;
@@ -229,7 +229,7 @@ private boolean updateDatabase(final IndexWriter writer, final String dbname, fi
final JSONObject obj = DB.getAllDocsBySeq(dbname, update_seq, Config.BATCH_SIZE);
if (!obj.has("rows")) {
- Log.log("no rows found (%s).", obj);
+ Utils.LOG.warn("no rows found (" + obj + ").");
return false;
}
@@ -267,7 +267,7 @@ private boolean updateDatabase(final IndexWriter writer, final String dbname, fi
if (result) {
progress.update(dbname, new_sig, update_seq);
- Log.log("%s: index caught up to %,d.", dbname, update_seq);
+ Utils.LOG.info(dbname + ": index caught up to " + update_seq);
}
return result;
View
35 src/main/java/com/github/rnewson/couchdb/lucene/Log.java
@@ -1,35 +0,0 @@
-package com.github.rnewson.couchdb.lucene;
-
-import org.apache.log4j.Logger;
-
-/**
- * Copyright 2009 Robert Newson
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-
-public final class Log {
-
- private static final Logger LOG = Logger.getLogger("couchdb-lucene");
-
- public static void log(final String fmt, final Object... args) {
- if (LOG.isInfoEnabled()) {
- LOG.info(String.format(fmt, args));
- }
- }
-
- public static void log(final Exception e) {
- LOG.warn(e.getMessage(), e);
- }
-
-}
View
4 src/main/java/com/github/rnewson/couchdb/lucene/Main.java
@@ -25,6 +25,10 @@
public final class Main {
public static void main(final String[] args) throws Exception {
+ if (System.getProperty("couchdb.log.dir") == null) {
+ System.setProperty("couchdb.log.dir", System.getProperty("java.io.tmpdir"));
+ }
+ Utils.LOG.info("couchdb-lucene started.");
if (args.length >= 1 && args[0].equals("-index")) {
Index.main(args);
return;
View
6 src/main/java/com/github/rnewson/couchdb/lucene/RhinoDocument.java
@@ -37,7 +37,7 @@
public final class RhinoDocument extends ScriptableObject {
private static final long serialVersionUID = 1L;
-
+
public final Document doc;
private static final Database DB = new Database(Config.DB_URL);
@@ -116,12 +116,12 @@ public static void jsFunction_field(final Context cx, final Scriptable thisObj,
tv = Field.TermVector.NO;
if (args[0] == null) {
- Log.log("null key passed to field().");
+ Utils.LOG.warn("null key passed to field().");
return;
}
if (args[1] == null) {
- Log.log("null value passed to field().");
+ Utils.LOG.warn("null value passed to field().");
return;
}
View
2  src/main/java/com/github/rnewson/couchdb/lucene/Search.java
@@ -78,7 +78,7 @@ public static void main(final String[] args) {
if (reader != null && reopen) {
final IndexReader newReader = reader.reopen();
if (reader != newReader) {
- Log.log("Lucene index was updated, reopening searcher.");
+ Utils.LOG.info("Lucene index was updated, reopening searcher.");
final IndexReader oldReader = reader;
reader = newReader;
searcher = new IndexSearcher(reader);
View
8 src/main/java/com/github/rnewson/couchdb/lucene/Utils.java
@@ -23,6 +23,7 @@
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringEscapeUtils;
+import org.apache.log4j.Logger;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.Term;
@@ -33,11 +34,8 @@
class Utils {
- public static void log(final String fmt, final Object... args) {
- final String msg = String.format(fmt, args);
- System.out.printf("{\"log\":\"%s\"}\n", msg);
- }
-
+ public static final Logger LOG = Logger.getLogger("couchdb-lucene");
+
public static String throwableToJSON(final Throwable t) {
return error(t.getMessage() == null ? "Unknown error" : String.format("%s: %s", t.getClass(), t.getMessage()));
}
View
96 src/main/java/org/apache/nutch/analysis/lang/NGramProfile.java
@@ -34,11 +34,9 @@
import java.util.List;
import java.util.Map;
-import org.apache.commons.logging.LogFactory;
+import org.apache.log4j.Logger;
import org.apache.lucene.analysis.Token;
-import com.github.rnewson.couchdb.lucene.Log;
-
/**
* This class runs a ngram analysis over submitted text, results might be used
* for automatic language identifiaction.
@@ -52,6 +50,8 @@
*/
public class NGramProfile {
+ private static final Logger LOGGER = Logger.getLogger(NGramProfile.class);
+
/** The minimum length allowed for a ngram. */
final static int ABSOLUTE_MIN_NGRAM_LENGTH = 1;
@@ -314,8 +314,8 @@ public float getSimilarity(NGramProfile another) {
sum += other.frequency;
}
}
- } catch (Exception e) {
- Log.log(e);
+ } catch (final Exception e) {
+ LOGGER.warn(e);
}
return sum;
}
@@ -374,8 +374,8 @@ public static NGramProfile create(String name, InputStream is, String encoding)
while ((len = bis.read(buffer)) != -1) {
text.append(new String(buffer, 0, len, encoding));
}
- } catch (IOException e) {
- Log.log(e);
+ } catch (final IOException e) {
+ LOGGER.warn("Exception raised while creating profile.", e);
}
newProfile.analyze(text);
@@ -430,7 +430,7 @@ public void save(OutputStream os) throws IOException {
*
* @param args
*/
- public static void main(String args[]) {
+ public static void main(String args[]) throws Exception {
String usage = "Usage: NGramProfile " + "[-create profilename filename encoding] "
+ "[-similarity file1 file2] " + "[-score profile-name filename encoding]";
@@ -473,52 +473,46 @@ public static void main(String args[]) {
}
}
- try {
+ switch (command) {
- switch (command) {
-
- case CREATE:
-
- File f = new File(filename);
- FileInputStream fis = new FileInputStream(f);
- NGramProfile newProfile = NGramProfile.create(profilename, fis, encoding);
- fis.close();
- f = new File(profilename + "." + FILE_EXTENSION);
- FileOutputStream fos = new FileOutputStream(f);
- newProfile.save(fos);
- System.out.println("new profile " + profilename + "." + FILE_EXTENSION + " was created.");
- break;
-
- case SIMILARITY:
-
- f = new File(filename);
- fis = new FileInputStream(f);
- newProfile = NGramProfile.create(filename, fis, encoding);
- newProfile.normalize();
-
- f = new File(filename2);
- fis = new FileInputStream(f);
- NGramProfile newProfile2 = NGramProfile.create(filename2, fis, encoding);
- newProfile2.normalize();
- System.out.println("Similarity is " + newProfile.getSimilarity(newProfile2));
- break;
-
- case SCORE:
- f = new File(filename);
- fis = new FileInputStream(f);
- newProfile = NGramProfile.create(filename, fis, encoding);
-
- f = new File(profilename + "." + FILE_EXTENSION);
- fis = new FileInputStream(f);
- NGramProfile compare = new NGramProfile(profilename, DEFAULT_MIN_NGRAM_LENGTH, DEFAULT_MAX_NGRAM_LENGTH);
- compare.load(fis);
- System.out.println("Score is " + compare.getSimilarity(newProfile));
- break;
+ case CREATE:
- }
+ File f = new File(filename);
+ FileInputStream fis = new FileInputStream(f);
+ NGramProfile newProfile = NGramProfile.create(profilename, fis, encoding);
+ fis.close();
+ f = new File(profilename + "." + FILE_EXTENSION);
+ FileOutputStream fos = new FileOutputStream(f);
+ newProfile.save(fos);
+ System.out.println("new profile " + profilename + "." + FILE_EXTENSION + " was created.");
+ break;
+
+ case SIMILARITY:
+
+ f = new File(filename);
+ fis = new FileInputStream(f);
+ newProfile = NGramProfile.create(filename, fis, encoding);
+ newProfile.normalize();
+
+ f = new File(filename2);
+ fis = new FileInputStream(f);
+ NGramProfile newProfile2 = NGramProfile.create(filename2, fis, encoding);
+ newProfile2.normalize();
+ System.out.println("Similarity is " + newProfile.getSimilarity(newProfile2));
+ break;
+
+ case SCORE:
+ f = new File(filename);
+ fis = new FileInputStream(f);
+ newProfile = NGramProfile.create(filename, fis, encoding);
+
+ f = new File(profilename + "." + FILE_EXTENSION);
+ fis = new FileInputStream(f);
+ NGramProfile compare = new NGramProfile(profilename, DEFAULT_MIN_NGRAM_LENGTH, DEFAULT_MAX_NGRAM_LENGTH);
+ compare.load(fis);
+ System.out.println("Score is " + compare.getSimilarity(newProfile));
+ break;
- } catch (Exception e) {
- Log.log(e);
}
}
View
30 src/main/resources/log4j.xml
@@ -1,14 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
- <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
- <param name="Target" value="System.err"/>
- <layout class="org.apache.log4j.PatternLayout">
- <param name="ConversionPattern" value="%p %m%n"/>
- </layout>
- </appender>
- <root>
- <priority value="INFO"/>
- <appender-ref ref="ConsoleAppender"/>
- </root>
+ <appender name="file" class="org.apache.log4j.RollingFileAppender">
+ <param name="File" value="${couchdb.log.dir}/couchdb-lucene.log" />
+ <param name="MaxBackupIndex" value="3" />
+ <param name="MaxFileSize" value="5M" />
+ <param name="Append" value="true" />
+ <layout class="org.apache.log4j.PatternLayout">
+ <param name="ConversionPattern" value="[%c] %p %m%n" />
+ </layout>
+ </appender>
+ <appender name="console" class="org.apache.log4j.ConsoleAppender">
+ <param name="Target" value="System.err" />
+ <layout class="org.apache.log4j.PatternLayout">
+ <param name="ConversionPattern" value="[%c] %p %m%n" />
+ </layout>
+ </appender>
+ <root>
+ <priority value="INFO" />
+ <appender-ref ref="console" />
+ <appender-ref ref="file" />
+ </root>
</log4j:configuration>
View
14 src/test/resources/log4j.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
+ <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
+ <param name="Target" value="System.err"/>
+ <layout class="org.apache.log4j.PatternLayout">
+ <param name="ConversionPattern" value="[%c] %p %m%n"/>
+ </layout>
+ </appender>
+ <root>
+ <priority value="INFO"/>
+ <appender-ref ref="ConsoleAppender"/>
+ </root>
+</log4j:configuration>
Please sign in to comment.
Something went wrong with that request. Please try again.