You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@logging.apache.org by liyuj <gi...@git.apache.org> on 2017/04/24 05:45:24 UTC

[GitHub] logging-log4j2 pull request #74: Add a new LuceneAppender which writes loggi...

GitHub user liyuj opened a pull request:

    https://github.com/apache/logging-log4j2/pull/74

    Add a new LuceneAppender which writes logging events to a lucene index library.

    Add a new LuceneAppender which writes logging events to a lucene index library.The log4j2.xml configuration is as follows:
    &lt;Lucene name="lucene" ignoreExceptions="true" target="/target/lucene/index" expiryTime=\u201c1296000\u201d&gt;
    &lt;PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/&gt;
    &lt;IndexField name="time" pattern="%d{UNIX_MILLIS}" type="LongField"/&gt;
    &lt;IndexField name="level" pattern="%-5level" /&gt;
    &lt;IndexField name="content" pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/&gt;
    &lt;/Lucene&gt;
    
    this appender relies on the Lucene 5.5.4 version.
    this patch adds the corresponding test cases.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/liyuj/logging-log4j2 master

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/logging-log4j2/pull/74.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #74
    
----
commit ab4fbe5875c6f5aebdfe1bc528c0bb8eaed57fe1
Author: liyuj <18...@163.com>
Date:   2017-04-24T05:38:52Z

    new LuceneAppender

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] logging-log4j2 pull request #74: Add a new LuceneAppender which writes loggi...

Posted by jvz <gi...@git.apache.org>.
Github user jvz commented on a diff in the pull request:

    https://github.com/apache/logging-log4j2/pull/74#discussion_r113044838
  
    --- Diff: log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/lucene/LuceneAppender.java ---
    @@ -0,0 +1,301 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements. See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You 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.
    + */
    +package org.apache.logging.log4j.nosql.appender.lucene;
    +
    +import java.io.IOException;
    +import java.io.Serializable;
    +import java.nio.file.Paths;
    +import java.util.Calendar;
    +import java.util.concurrent.ConcurrentHashMap;
    +import java.util.concurrent.Executors;
    +import java.util.concurrent.ScheduledExecutorService;
    +import java.util.concurrent.TimeUnit;
    +
    +import org.apache.logging.log4j.core.Appender;
    +import org.apache.logging.log4j.core.Filter;
    +import org.apache.logging.log4j.core.Layout;
    +import org.apache.logging.log4j.core.LogEvent;
    +import org.apache.logging.log4j.core.appender.AbstractAppender;
    +import org.apache.logging.log4j.core.appender.AppenderLoggingException;
    +import org.apache.logging.log4j.core.config.Node;
    +import org.apache.logging.log4j.core.config.plugins.Plugin;
    +import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
    +import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
    +import org.apache.logging.log4j.core.config.plugins.PluginElement;
    +import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
    +import org.apache.logging.log4j.util.Strings;
    +import org.apache.lucene.document.Document;
    +import org.apache.lucene.document.Field;
    +import org.apache.lucene.document.LongField;
    +import org.apache.lucene.document.StringField;
    +import org.apache.lucene.document.TextField;
    +import org.apache.lucene.index.IndexWriter;
    +import org.apache.lucene.index.IndexWriterConfig;
    +import org.apache.lucene.search.NumericRangeQuery;
    +import org.apache.lucene.store.FSDirectory;
    +
    +/**
    + * This Appender writes logging events to a lucene index library. It takes a list of
    + * {@link IndexField} with which determines which fields are written to the index library.
    + * <Lucene name="lucene" ignoreExceptions="true" target="/target/lucene/index">
    --- End diff --
    
    Also, these sorts of examples work better in the manual page. The javadocs are more useful for users programmatically configuring things, so they don't necessarily need XML examples here.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] logging-log4j2 issue #74: Add a new LuceneAppender which writes logging even...

Posted by jvz <gi...@git.apache.org>.
Github user jvz commented on the issue:

    https://github.com/apache/logging-log4j2/pull/74
  
    If Lucene's API is anything like Elasticsearch, then they don't bother changing the package name between major versions and there are hacky ways to support multiple versions.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] logging-log4j2 issue #74: Add a new LuceneAppender which writes logging even...

Posted by liyuj <gi...@git.apache.org>.
Github user liyuj commented on the issue:

    https://github.com/apache/logging-log4j2/pull/74
  
    We found a bug that is being corrected and then resubmitted in the form of a new module.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] logging-log4j2 pull request #74: Add a new LuceneAppender which writes loggi...

Posted by jvz <gi...@git.apache.org>.
Github user jvz commented on a diff in the pull request:

    https://github.com/apache/logging-log4j2/pull/74#discussion_r113046338
  
    --- Diff: log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/lucene/LuceneAppender.java ---
    @@ -0,0 +1,301 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements. See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You 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.
    + */
    +package org.apache.logging.log4j.nosql.appender.lucene;
    +
    +import java.io.IOException;
    +import java.io.Serializable;
    +import java.nio.file.Paths;
    +import java.util.Calendar;
    +import java.util.concurrent.ConcurrentHashMap;
    +import java.util.concurrent.Executors;
    +import java.util.concurrent.ScheduledExecutorService;
    +import java.util.concurrent.TimeUnit;
    +
    +import org.apache.logging.log4j.core.Appender;
    +import org.apache.logging.log4j.core.Filter;
    +import org.apache.logging.log4j.core.Layout;
    +import org.apache.logging.log4j.core.LogEvent;
    +import org.apache.logging.log4j.core.appender.AbstractAppender;
    +import org.apache.logging.log4j.core.appender.AppenderLoggingException;
    +import org.apache.logging.log4j.core.config.Node;
    +import org.apache.logging.log4j.core.config.plugins.Plugin;
    +import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
    +import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
    +import org.apache.logging.log4j.core.config.plugins.PluginElement;
    +import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
    +import org.apache.logging.log4j.util.Strings;
    +import org.apache.lucene.document.Document;
    +import org.apache.lucene.document.Field;
    +import org.apache.lucene.document.LongField;
    +import org.apache.lucene.document.StringField;
    +import org.apache.lucene.document.TextField;
    +import org.apache.lucene.index.IndexWriter;
    +import org.apache.lucene.index.IndexWriterConfig;
    +import org.apache.lucene.search.NumericRangeQuery;
    +import org.apache.lucene.store.FSDirectory;
    +
    +/**
    + * This Appender writes logging events to a lucene index library. It takes a list of
    + * {@link IndexField} with which determines which fields are written to the index library.
    + * <Lucene name="lucene" ignoreExceptions="true" target="/target/lucene/index">
    + *    <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
    + *    
    + *	  <IndexField name="time" pattern="%d{UNIX_MILLIS}" type="LongField"/>
    + *	  <IndexField name="level" pattern="%-5level" />
    + *	  <IndexField name="content" pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
    + * </Lucene>
    + */
    +@Plugin(name = "Lucene", category = Node.CATEGORY, elementType = Appender.ELEMENT_TYPE, printObject = true)
    +public class LuceneAppender extends AbstractAppender {
    +	/**
    +	 * index directory
    +	 */
    +	private final String target;
    +	/**
    +	 * Index expiration time (seconds)
    +	 */
    +	private final Integer expiryTime;
    +	/**
    +	 * IndexField array.
    +	 */
    +	private final LuceneIndexField[] indexFields;
    +	/**
    +	 * Periodically clear the index and submit the IndexWriter thread pool
    +	 */
    +	private final ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(2);
    +	/**
    +	 * IndexWriter corresponding to each index directory.
    +	 */
    +	private static final ConcurrentHashMap<String, IndexWriter> writerMap = new ConcurrentHashMap<String, IndexWriter>();
    +	
    +	static{
    +		Runtime.getRuntime().addShutdownHook(new Thread() {
    +			@Override
    +			public void run() {
    +				for(IndexWriter writer: writerMap.values()){
    +					if(null!=writer && writer.isOpen()){
    +						try {
    +							writer.commit();
    +							writer.close();
    +						} catch (IOException e) {
    +							LOGGER.error(e.getMessage(), e);
    +						}
    +					}
    +				}
    +				writerMap.clear();
    +			}
    +		});
    +		
    +	}
    +
    +	protected LuceneAppender(String name, boolean ignoreExceptions, Filter filter,
    +			Layout<? extends Serializable> layout, String target, Integer expiryTime, LuceneIndexField[] indexFields) {
    +		super(name, filter, layout, ignoreExceptions);
    +		this.target = target;
    +		this.expiryTime = expiryTime;
    +		this.indexFields = indexFields;
    +		
    +		initIndexWriter();
    +		registerCommitTimer();
    +		if (this.expiryTime != null){
    +			registerClearTimer();
    +		}
    +	}
    +
    +	/**
    +	 * IndexWriter initialization.
    +	 */
    +	private IndexWriter initIndexWriter() {
    +		if (null == writerMap.get(target)) {
    +			try {
    +				FSDirectory fsDir = FSDirectory.open(Paths.get(this.target));
    +				IndexWriterConfig writerConfig = new IndexWriterConfig(new LuceneAnalyzer());
    +				writerMap.putIfAbsent(target, new IndexWriter(fsDir, writerConfig));
    +			} catch (IOException e) {
    +				LOGGER.error("IndexWriter initialization failed!" + e.getMessage(), e);
    +			}
    +		}
    +		return writerMap.get(target);
    +	}
    +
    +	/**
    +	 * Register IndexWriter commit timertask.
    +	 */
    +	private void registerCommitTimer() {
    +		scheduledExecutor.scheduleAtFixedRate(new Runnable() {
    +			@Override
    +			public void run() {
    +				IndexWriter indexWriter = initIndexWriter();
    +				if (null!=indexWriter && indexWriter.numRamDocs()>0) {
    +					try {
    +						indexWriter.commit();
    +					} catch (IOException e) {
    +						LOGGER.error("IndexWriter commit failed!"+e.getMessage(),e);
    +					}
    +				}
    +			}
    +		}, 1, 1, TimeUnit.MINUTES);
    +	}
    +	
    +	/**
    +	 * Register IndexWriter clean timertask.
    +	 * Delete the index before {@link LuceneAppender#expiryTime} second every day at 0.
    +	 * 
    +	 * @see LuceneAppender#expiryTime
    +	 */
    +	private void registerClearTimer() {
    +		Calendar calendar = Calendar.getInstance();
    +		long curMillis = calendar.getTimeInMillis();
    +		calendar.add(Calendar.DAY_OF_MONTH, 1);
    +		calendar.set(Calendar.HOUR_OF_DAY, 0);
    +		calendar.set(Calendar.MINUTE, 0);
    +		calendar.set(Calendar.SECOND, 0);
    +		calendar.set(Calendar.MILLISECOND, 0);
    +		long difMinutes = (calendar.getTimeInMillis() - curMillis) / (1000 * 60);
    +
    +		scheduledExecutor.scheduleAtFixedRate(new Runnable() {
    +			@Override
    +			public void run() {
    +				LOGGER.info("delete index start!",target,expiryTime);
    +				IndexWriter indexWriter = initIndexWriter();
    +				if (null != indexWriter) {
    +					Long start = 0L;
    +					Long end = System.currentTimeMillis() - expiryTime * 1000;
    +					NumericRangeQuery<Long> rangeQuery = NumericRangeQuery.newLongRange("timestamp", start, end, true, true);
    +					try {
    +						indexWriter.deleteDocuments(rangeQuery);
    +						indexWriter.commit();
    +						LOGGER.info("delete index end! ",target,expiryTime);
    +					}catch (IOException e) {
    +						LOGGER.error("delete index failed! "+e.getMessage(),e);
    +					}
    +				}
    +			}
    +		}, difMinutes, 1440, TimeUnit.MINUTES);
    +	}
    +
    +	/**
    +	 * create lucene index.
    +	 * 
    +	 * @param event
    +	 */
    +	@Override
    +	public void append(LogEvent event) {
    +		if (null != indexFields && indexFields.length > 0) {
    +			IndexWriter indexWriter = initIndexWriter();
    +			if(null != indexWriter){
    +				Document doc = new Document();
    +				doc.add(new LongField("timestamp", event.getTimeMillis(), Field.Store.YES));
    +				try {
    +					for (LuceneIndexField field : indexFields) {
    +						String value = field.getLayout().toSerializable(event);
    +						if (Strings.isEmpty(value) || value.matches("[$]\\{.+\\}")) {
    --- End diff --
    
    Generally like to precompile regular expressions into Pattern objects. That's actually one case where a static field makes sense come to think of it. ;)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] logging-log4j2 issue #74: Add a new LuceneAppender which writes logging even...

Posted by garydgregory <gi...@git.apache.org>.
Github user garydgregory commented on the issue:

    https://github.com/apache/logging-log4j2/pull/74
  
    Why not use the current version of Lucene? Is the version you are using and the latest (6.5.0) compatible?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] logging-log4j2 issue #74: Add a new LuceneAppender which writes logging even...

Posted by liyuj <gi...@git.apache.org>.
Github user liyuj commented on the issue:

    https://github.com/apache/logging-log4j2/pull/74
  
    Lucene 6 and later versions require Java 8 or later.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] logging-log4j2 issue #74: Add a new LuceneAppender which writes logging even...

Posted by garydgregory <gi...@git.apache.org>.
Github user garydgregory commented on the issue:

    https://github.com/apache/logging-log4j2/pull/74
  
    Good to know! Thank you. I wonder if we should then create this under a "lucene5" package so that we can have a "lucene6" package in a different module if needed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] logging-log4j2 pull request #74: Add a new LuceneAppender which writes loggi...

Posted by jvz <gi...@git.apache.org>.
Github user jvz commented on a diff in the pull request:

    https://github.com/apache/logging-log4j2/pull/74#discussion_r113046093
  
    --- Diff: log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/lucene/LuceneAppender.java ---
    @@ -0,0 +1,301 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements. See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You 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.
    + */
    +package org.apache.logging.log4j.nosql.appender.lucene;
    +
    +import java.io.IOException;
    +import java.io.Serializable;
    +import java.nio.file.Paths;
    +import java.util.Calendar;
    +import java.util.concurrent.ConcurrentHashMap;
    +import java.util.concurrent.Executors;
    +import java.util.concurrent.ScheduledExecutorService;
    +import java.util.concurrent.TimeUnit;
    +
    +import org.apache.logging.log4j.core.Appender;
    +import org.apache.logging.log4j.core.Filter;
    +import org.apache.logging.log4j.core.Layout;
    +import org.apache.logging.log4j.core.LogEvent;
    +import org.apache.logging.log4j.core.appender.AbstractAppender;
    +import org.apache.logging.log4j.core.appender.AppenderLoggingException;
    +import org.apache.logging.log4j.core.config.Node;
    +import org.apache.logging.log4j.core.config.plugins.Plugin;
    +import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
    +import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
    +import org.apache.logging.log4j.core.config.plugins.PluginElement;
    +import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
    +import org.apache.logging.log4j.util.Strings;
    +import org.apache.lucene.document.Document;
    +import org.apache.lucene.document.Field;
    +import org.apache.lucene.document.LongField;
    +import org.apache.lucene.document.StringField;
    +import org.apache.lucene.document.TextField;
    +import org.apache.lucene.index.IndexWriter;
    +import org.apache.lucene.index.IndexWriterConfig;
    +import org.apache.lucene.search.NumericRangeQuery;
    +import org.apache.lucene.store.FSDirectory;
    +
    +/**
    + * This Appender writes logging events to a lucene index library. It takes a list of
    + * {@link IndexField} with which determines which fields are written to the index library.
    + * <Lucene name="lucene" ignoreExceptions="true" target="/target/lucene/index">
    + *    <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
    + *    
    + *	  <IndexField name="time" pattern="%d{UNIX_MILLIS}" type="LongField"/>
    + *	  <IndexField name="level" pattern="%-5level" />
    + *	  <IndexField name="content" pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
    + * </Lucene>
    + */
    +@Plugin(name = "Lucene", category = Node.CATEGORY, elementType = Appender.ELEMENT_TYPE, printObject = true)
    +public class LuceneAppender extends AbstractAppender {
    +	/**
    +	 * index directory
    +	 */
    +	private final String target;
    +	/**
    +	 * Index expiration time (seconds)
    +	 */
    +	private final Integer expiryTime;
    +	/**
    +	 * IndexField array.
    +	 */
    +	private final LuceneIndexField[] indexFields;
    +	/**
    +	 * Periodically clear the index and submit the IndexWriter thread pool
    +	 */
    +	private final ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(2);
    +	/**
    +	 * IndexWriter corresponding to each index directory.
    +	 */
    +	private static final ConcurrentHashMap<String, IndexWriter> writerMap = new ConcurrentHashMap<String, IndexWriter>();
    +	
    +	static{
    +		Runtime.getRuntime().addShutdownHook(new Thread() {
    +			@Override
    +			public void run() {
    +				for(IndexWriter writer: writerMap.values()){
    +					if(null!=writer && writer.isOpen()){
    +						try {
    +							writer.commit();
    +							writer.close();
    +						} catch (IOException e) {
    +							LOGGER.error(e.getMessage(), e);
    +						}
    +					}
    +				}
    +				writerMap.clear();
    +			}
    +		});
    +		
    +	}
    +
    +	protected LuceneAppender(String name, boolean ignoreExceptions, Filter filter,
    +			Layout<? extends Serializable> layout, String target, Integer expiryTime, LuceneIndexField[] indexFields) {
    +		super(name, filter, layout, ignoreExceptions);
    +		this.target = target;
    +		this.expiryTime = expiryTime;
    +		this.indexFields = indexFields;
    +		
    +		initIndexWriter();
    +		registerCommitTimer();
    +		if (this.expiryTime != null){
    +			registerClearTimer();
    +		}
    +	}
    +
    +	/**
    +	 * IndexWriter initialization.
    +	 */
    +	private IndexWriter initIndexWriter() {
    +		if (null == writerMap.get(target)) {
    +			try {
    +				FSDirectory fsDir = FSDirectory.open(Paths.get(this.target));
    +				IndexWriterConfig writerConfig = new IndexWriterConfig(new LuceneAnalyzer());
    +				writerMap.putIfAbsent(target, new IndexWriter(fsDir, writerConfig));
    +			} catch (IOException e) {
    +				LOGGER.error("IndexWriter initialization failed!" + e.getMessage(), e);
    +			}
    +		}
    +		return writerMap.get(target);
    +	}
    +
    +	/**
    +	 * Register IndexWriter commit timertask.
    +	 */
    +	private void registerCommitTimer() {
    +		scheduledExecutor.scheduleAtFixedRate(new Runnable() {
    +			@Override
    +			public void run() {
    +				IndexWriter indexWriter = initIndexWriter();
    +				if (null!=indexWriter && indexWriter.numRamDocs()>0) {
    +					try {
    +						indexWriter.commit();
    +					} catch (IOException e) {
    +						LOGGER.error("IndexWriter commit failed!"+e.getMessage(),e);
    +					}
    +				}
    +			}
    +		}, 1, 1, TimeUnit.MINUTES);
    +	}
    +	
    +	/**
    +	 * Register IndexWriter clean timertask.
    +	 * Delete the index before {@link LuceneAppender#expiryTime} second every day at 0.
    +	 * 
    +	 * @see LuceneAppender#expiryTime
    +	 */
    +	private void registerClearTimer() {
    +		Calendar calendar = Calendar.getInstance();
    +		long curMillis = calendar.getTimeInMillis();
    +		calendar.add(Calendar.DAY_OF_MONTH, 1);
    +		calendar.set(Calendar.HOUR_OF_DAY, 0);
    +		calendar.set(Calendar.MINUTE, 0);
    +		calendar.set(Calendar.SECOND, 0);
    +		calendar.set(Calendar.MILLISECOND, 0);
    +		long difMinutes = (calendar.getTimeInMillis() - curMillis) / (1000 * 60);
    --- End diff --
    
    You could use `TimeUnit.MILLIS.toMinutes(x)` here to be more expressive.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] logging-log4j2 pull request #74: Add a new LuceneAppender which writes loggi...

Posted by jvz <gi...@git.apache.org>.
Github user jvz commented on a diff in the pull request:

    https://github.com/apache/logging-log4j2/pull/74#discussion_r113046710
  
    --- Diff: log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/lucene/LuceneAppender.java ---
    @@ -0,0 +1,301 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements. See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You 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.
    + */
    +package org.apache.logging.log4j.nosql.appender.lucene;
    +
    +import java.io.IOException;
    +import java.io.Serializable;
    +import java.nio.file.Paths;
    +import java.util.Calendar;
    +import java.util.concurrent.ConcurrentHashMap;
    +import java.util.concurrent.Executors;
    +import java.util.concurrent.ScheduledExecutorService;
    +import java.util.concurrent.TimeUnit;
    +
    +import org.apache.logging.log4j.core.Appender;
    +import org.apache.logging.log4j.core.Filter;
    +import org.apache.logging.log4j.core.Layout;
    +import org.apache.logging.log4j.core.LogEvent;
    +import org.apache.logging.log4j.core.appender.AbstractAppender;
    +import org.apache.logging.log4j.core.appender.AppenderLoggingException;
    +import org.apache.logging.log4j.core.config.Node;
    +import org.apache.logging.log4j.core.config.plugins.Plugin;
    +import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
    +import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
    +import org.apache.logging.log4j.core.config.plugins.PluginElement;
    +import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
    +import org.apache.logging.log4j.util.Strings;
    +import org.apache.lucene.document.Document;
    +import org.apache.lucene.document.Field;
    +import org.apache.lucene.document.LongField;
    +import org.apache.lucene.document.StringField;
    +import org.apache.lucene.document.TextField;
    +import org.apache.lucene.index.IndexWriter;
    +import org.apache.lucene.index.IndexWriterConfig;
    +import org.apache.lucene.search.NumericRangeQuery;
    +import org.apache.lucene.store.FSDirectory;
    +
    +/**
    + * This Appender writes logging events to a lucene index library. It takes a list of
    + * {@link IndexField} with which determines which fields are written to the index library.
    + * <Lucene name="lucene" ignoreExceptions="true" target="/target/lucene/index">
    + *    <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
    + *    
    + *	  <IndexField name="time" pattern="%d{UNIX_MILLIS}" type="LongField"/>
    + *	  <IndexField name="level" pattern="%-5level" />
    + *	  <IndexField name="content" pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
    + * </Lucene>
    + */
    +@Plugin(name = "Lucene", category = Node.CATEGORY, elementType = Appender.ELEMENT_TYPE, printObject = true)
    +public class LuceneAppender extends AbstractAppender {
    +	/**
    +	 * index directory
    +	 */
    +	private final String target;
    +	/**
    +	 * Index expiration time (seconds)
    +	 */
    +	private final Integer expiryTime;
    +	/**
    +	 * IndexField array.
    +	 */
    +	private final LuceneIndexField[] indexFields;
    +	/**
    +	 * Periodically clear the index and submit the IndexWriter thread pool
    +	 */
    +	private final ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(2);
    +	/**
    +	 * IndexWriter corresponding to each index directory.
    +	 */
    +	private static final ConcurrentHashMap<String, IndexWriter> writerMap = new ConcurrentHashMap<String, IndexWriter>();
    +	
    +	static{
    +		Runtime.getRuntime().addShutdownHook(new Thread() {
    +			@Override
    +			public void run() {
    +				for(IndexWriter writer: writerMap.values()){
    +					if(null!=writer && writer.isOpen()){
    +						try {
    +							writer.commit();
    +							writer.close();
    +						} catch (IOException e) {
    +							LOGGER.error(e.getMessage(), e);
    +						}
    +					}
    +				}
    +				writerMap.clear();
    +			}
    +		});
    +		
    +	}
    +
    +	protected LuceneAppender(String name, boolean ignoreExceptions, Filter filter,
    +			Layout<? extends Serializable> layout, String target, Integer expiryTime, LuceneIndexField[] indexFields) {
    +		super(name, filter, layout, ignoreExceptions);
    +		this.target = target;
    +		this.expiryTime = expiryTime;
    +		this.indexFields = indexFields;
    +		
    +		initIndexWriter();
    +		registerCommitTimer();
    +		if (this.expiryTime != null){
    +			registerClearTimer();
    +		}
    +	}
    +
    +	/**
    +	 * IndexWriter initialization.
    +	 */
    +	private IndexWriter initIndexWriter() {
    +		if (null == writerMap.get(target)) {
    +			try {
    +				FSDirectory fsDir = FSDirectory.open(Paths.get(this.target));
    +				IndexWriterConfig writerConfig = new IndexWriterConfig(new LuceneAnalyzer());
    +				writerMap.putIfAbsent(target, new IndexWriter(fsDir, writerConfig));
    +			} catch (IOException e) {
    +				LOGGER.error("IndexWriter initialization failed!" + e.getMessage(), e);
    +			}
    +		}
    +		return writerMap.get(target);
    +	}
    +
    +	/**
    +	 * Register IndexWriter commit timertask.
    +	 */
    +	private void registerCommitTimer() {
    +		scheduledExecutor.scheduleAtFixedRate(new Runnable() {
    +			@Override
    +			public void run() {
    +				IndexWriter indexWriter = initIndexWriter();
    +				if (null!=indexWriter && indexWriter.numRamDocs()>0) {
    +					try {
    +						indexWriter.commit();
    +					} catch (IOException e) {
    +						LOGGER.error("IndexWriter commit failed!"+e.getMessage(),e);
    +					}
    +				}
    +			}
    +		}, 1, 1, TimeUnit.MINUTES);
    +	}
    +	
    +	/**
    +	 * Register IndexWriter clean timertask.
    +	 * Delete the index before {@link LuceneAppender#expiryTime} second every day at 0.
    +	 * 
    +	 * @see LuceneAppender#expiryTime
    +	 */
    +	private void registerClearTimer() {
    +		Calendar calendar = Calendar.getInstance();
    +		long curMillis = calendar.getTimeInMillis();
    +		calendar.add(Calendar.DAY_OF_MONTH, 1);
    +		calendar.set(Calendar.HOUR_OF_DAY, 0);
    +		calendar.set(Calendar.MINUTE, 0);
    +		calendar.set(Calendar.SECOND, 0);
    +		calendar.set(Calendar.MILLISECOND, 0);
    +		long difMinutes = (calendar.getTimeInMillis() - curMillis) / (1000 * 60);
    +
    +		scheduledExecutor.scheduleAtFixedRate(new Runnable() {
    +			@Override
    +			public void run() {
    +				LOGGER.info("delete index start!",target,expiryTime);
    +				IndexWriter indexWriter = initIndexWriter();
    +				if (null != indexWriter) {
    +					Long start = 0L;
    +					Long end = System.currentTimeMillis() - expiryTime * 1000;
    +					NumericRangeQuery<Long> rangeQuery = NumericRangeQuery.newLongRange("timestamp", start, end, true, true);
    +					try {
    +						indexWriter.deleteDocuments(rangeQuery);
    +						indexWriter.commit();
    +						LOGGER.info("delete index end! ",target,expiryTime);
    +					}catch (IOException e) {
    +						LOGGER.error("delete index failed! "+e.getMessage(),e);
    +					}
    +				}
    +			}
    +		}, difMinutes, 1440, TimeUnit.MINUTES);
    +	}
    +
    +	/**
    +	 * create lucene index.
    +	 * 
    +	 * @param event
    +	 */
    +	@Override
    +	public void append(LogEvent event) {
    +		if (null != indexFields && indexFields.length > 0) {
    +			IndexWriter indexWriter = initIndexWriter();
    +			if(null != indexWriter){
    +				Document doc = new Document();
    +				doc.add(new LongField("timestamp", event.getTimeMillis(), Field.Store.YES));
    +				try {
    +					for (LuceneIndexField field : indexFields) {
    +						String value = field.getLayout().toSerializable(event);
    +						if (Strings.isEmpty(value) || value.matches("[$]\\{.+\\}")) {
    +							return;
    +						}
    +						value = value.trim();
    +						String type = field.getType();
    +						if (Strings.isNotEmpty(type)) {
    +							Class<?> clazz = Class.forName("org.apache.lucene.document." + type);
    +							if (clazz == LongField.class) {
    +								doc.add(new LongField(field.getName(), Long.valueOf(value), Field.Store.YES));
    +							} else if (clazz == StringField.class) {
    +								doc.add(new StringField(field.getName(), value, Field.Store.YES));
    +							} else if (clazz == TextField.class) {
    +								doc.add(new TextField(field.getName(), value, Field.Store.YES));
    +							} else {
    +								throw new UnsupportedOperationException(type + " type currently not supported.");
    +							}
    +						} else {
    +							doc.add(new TextField(field.getName(), value, Field.Store.YES));
    +						}
    +					}
    +					indexWriter.addDocument(doc);
    +				} catch (Exception e) {
    +					LOGGER.error(e.getMessage(), e);
    +					if (!ignoreExceptions()) {
    +						throw new AppenderLoggingException(e);
    --- End diff --
    
    This is automatically handled by a wrapper appender that all appenders are constructed within. You can simplify this.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] logging-log4j2 pull request #74: Add a new LuceneAppender which writes loggi...

Posted by jvz <gi...@git.apache.org>.
Github user jvz commented on a diff in the pull request:

    https://github.com/apache/logging-log4j2/pull/74#discussion_r113045520
  
    --- Diff: log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/lucene/LuceneAppender.java ---
    @@ -0,0 +1,301 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements. See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You 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.
    + */
    +package org.apache.logging.log4j.nosql.appender.lucene;
    +
    +import java.io.IOException;
    +import java.io.Serializable;
    +import java.nio.file.Paths;
    +import java.util.Calendar;
    +import java.util.concurrent.ConcurrentHashMap;
    +import java.util.concurrent.Executors;
    +import java.util.concurrent.ScheduledExecutorService;
    +import java.util.concurrent.TimeUnit;
    +
    +import org.apache.logging.log4j.core.Appender;
    +import org.apache.logging.log4j.core.Filter;
    +import org.apache.logging.log4j.core.Layout;
    +import org.apache.logging.log4j.core.LogEvent;
    +import org.apache.logging.log4j.core.appender.AbstractAppender;
    +import org.apache.logging.log4j.core.appender.AppenderLoggingException;
    +import org.apache.logging.log4j.core.config.Node;
    +import org.apache.logging.log4j.core.config.plugins.Plugin;
    +import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
    +import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
    +import org.apache.logging.log4j.core.config.plugins.PluginElement;
    +import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
    +import org.apache.logging.log4j.util.Strings;
    +import org.apache.lucene.document.Document;
    +import org.apache.lucene.document.Field;
    +import org.apache.lucene.document.LongField;
    +import org.apache.lucene.document.StringField;
    +import org.apache.lucene.document.TextField;
    +import org.apache.lucene.index.IndexWriter;
    +import org.apache.lucene.index.IndexWriterConfig;
    +import org.apache.lucene.search.NumericRangeQuery;
    +import org.apache.lucene.store.FSDirectory;
    +
    +/**
    + * This Appender writes logging events to a lucene index library. It takes a list of
    + * {@link IndexField} with which determines which fields are written to the index library.
    + * <Lucene name="lucene" ignoreExceptions="true" target="/target/lucene/index">
    + *    <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
    + *    
    + *	  <IndexField name="time" pattern="%d{UNIX_MILLIS}" type="LongField"/>
    + *	  <IndexField name="level" pattern="%-5level" />
    + *	  <IndexField name="content" pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
    + * </Lucene>
    + */
    +@Plugin(name = "Lucene", category = Node.CATEGORY, elementType = Appender.ELEMENT_TYPE, printObject = true)
    +public class LuceneAppender extends AbstractAppender {
    +	/**
    +	 * index directory
    +	 */
    +	private final String target;
    +	/**
    +	 * Index expiration time (seconds)
    +	 */
    +	private final Integer expiryTime;
    +	/**
    +	 * IndexField array.
    +	 */
    +	private final LuceneIndexField[] indexFields;
    +	/**
    +	 * Periodically clear the index and submit the IndexWriter thread pool
    +	 */
    +	private final ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(2);
    --- End diff --
    
    I think we have some built in log4j mechanisms for scheduling things (see for example the rolling file appender plugins). Might be more robust here to make that all configurable, but I'm open to suggestions.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] logging-log4j2 pull request #74: Add a new LuceneAppender which writes loggi...

Posted by jvz <gi...@git.apache.org>.
Github user jvz commented on a diff in the pull request:

    https://github.com/apache/logging-log4j2/pull/74#discussion_r113045904
  
    --- Diff: log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/lucene/LuceneAppender.java ---
    @@ -0,0 +1,301 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements. See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You 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.
    + */
    +package org.apache.logging.log4j.nosql.appender.lucene;
    +
    +import java.io.IOException;
    +import java.io.Serializable;
    +import java.nio.file.Paths;
    +import java.util.Calendar;
    +import java.util.concurrent.ConcurrentHashMap;
    +import java.util.concurrent.Executors;
    +import java.util.concurrent.ScheduledExecutorService;
    +import java.util.concurrent.TimeUnit;
    +
    +import org.apache.logging.log4j.core.Appender;
    +import org.apache.logging.log4j.core.Filter;
    +import org.apache.logging.log4j.core.Layout;
    +import org.apache.logging.log4j.core.LogEvent;
    +import org.apache.logging.log4j.core.appender.AbstractAppender;
    +import org.apache.logging.log4j.core.appender.AppenderLoggingException;
    +import org.apache.logging.log4j.core.config.Node;
    +import org.apache.logging.log4j.core.config.plugins.Plugin;
    +import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
    +import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
    +import org.apache.logging.log4j.core.config.plugins.PluginElement;
    +import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
    +import org.apache.logging.log4j.util.Strings;
    +import org.apache.lucene.document.Document;
    +import org.apache.lucene.document.Field;
    +import org.apache.lucene.document.LongField;
    +import org.apache.lucene.document.StringField;
    +import org.apache.lucene.document.TextField;
    +import org.apache.lucene.index.IndexWriter;
    +import org.apache.lucene.index.IndexWriterConfig;
    +import org.apache.lucene.search.NumericRangeQuery;
    +import org.apache.lucene.store.FSDirectory;
    +
    +/**
    + * This Appender writes logging events to a lucene index library. It takes a list of
    + * {@link IndexField} with which determines which fields are written to the index library.
    + * <Lucene name="lucene" ignoreExceptions="true" target="/target/lucene/index">
    + *    <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
    + *    
    + *	  <IndexField name="time" pattern="%d{UNIX_MILLIS}" type="LongField"/>
    + *	  <IndexField name="level" pattern="%-5level" />
    + *	  <IndexField name="content" pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
    + * </Lucene>
    + */
    +@Plugin(name = "Lucene", category = Node.CATEGORY, elementType = Appender.ELEMENT_TYPE, printObject = true)
    +public class LuceneAppender extends AbstractAppender {
    +	/**
    +	 * index directory
    +	 */
    +	private final String target;
    +	/**
    +	 * Index expiration time (seconds)
    +	 */
    +	private final Integer expiryTime;
    +	/**
    +	 * IndexField array.
    +	 */
    +	private final LuceneIndexField[] indexFields;
    +	/**
    +	 * Periodically clear the index and submit the IndexWriter thread pool
    +	 */
    +	private final ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(2);
    +	/**
    +	 * IndexWriter corresponding to each index directory.
    +	 */
    +	private static final ConcurrentHashMap<String, IndexWriter> writerMap = new ConcurrentHashMap<String, IndexWriter>();
    --- End diff --
    
    I can't say that this is enforced everywhere, but the general pattern we like to follow is that shared state for appenders are handled by a manager class instead of static fields. Helps with certain deployment scenarios like WARs, reconfigurations, and multiple appenders using the same config.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] logging-log4j2 pull request #74: Add a new LuceneAppender which writes loggi...

Posted by jvz <gi...@git.apache.org>.
Github user jvz commented on a diff in the pull request:

    https://github.com/apache/logging-log4j2/pull/74#discussion_r113046499
  
    --- Diff: log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/lucene/LuceneAppender.java ---
    @@ -0,0 +1,301 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements. See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You 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.
    + */
    +package org.apache.logging.log4j.nosql.appender.lucene;
    +
    +import java.io.IOException;
    +import java.io.Serializable;
    +import java.nio.file.Paths;
    +import java.util.Calendar;
    +import java.util.concurrent.ConcurrentHashMap;
    +import java.util.concurrent.Executors;
    +import java.util.concurrent.ScheduledExecutorService;
    +import java.util.concurrent.TimeUnit;
    +
    +import org.apache.logging.log4j.core.Appender;
    +import org.apache.logging.log4j.core.Filter;
    +import org.apache.logging.log4j.core.Layout;
    +import org.apache.logging.log4j.core.LogEvent;
    +import org.apache.logging.log4j.core.appender.AbstractAppender;
    +import org.apache.logging.log4j.core.appender.AppenderLoggingException;
    +import org.apache.logging.log4j.core.config.Node;
    +import org.apache.logging.log4j.core.config.plugins.Plugin;
    +import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
    +import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
    +import org.apache.logging.log4j.core.config.plugins.PluginElement;
    +import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
    +import org.apache.logging.log4j.util.Strings;
    +import org.apache.lucene.document.Document;
    +import org.apache.lucene.document.Field;
    +import org.apache.lucene.document.LongField;
    +import org.apache.lucene.document.StringField;
    +import org.apache.lucene.document.TextField;
    +import org.apache.lucene.index.IndexWriter;
    +import org.apache.lucene.index.IndexWriterConfig;
    +import org.apache.lucene.search.NumericRangeQuery;
    +import org.apache.lucene.store.FSDirectory;
    +
    +/**
    + * This Appender writes logging events to a lucene index library. It takes a list of
    + * {@link IndexField} with which determines which fields are written to the index library.
    + * <Lucene name="lucene" ignoreExceptions="true" target="/target/lucene/index">
    + *    <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
    + *    
    + *	  <IndexField name="time" pattern="%d{UNIX_MILLIS}" type="LongField"/>
    + *	  <IndexField name="level" pattern="%-5level" />
    + *	  <IndexField name="content" pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
    + * </Lucene>
    + */
    +@Plugin(name = "Lucene", category = Node.CATEGORY, elementType = Appender.ELEMENT_TYPE, printObject = true)
    +public class LuceneAppender extends AbstractAppender {
    +	/**
    +	 * index directory
    +	 */
    +	private final String target;
    +	/**
    +	 * Index expiration time (seconds)
    +	 */
    +	private final Integer expiryTime;
    +	/**
    +	 * IndexField array.
    +	 */
    +	private final LuceneIndexField[] indexFields;
    +	/**
    +	 * Periodically clear the index and submit the IndexWriter thread pool
    +	 */
    +	private final ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(2);
    +	/**
    +	 * IndexWriter corresponding to each index directory.
    +	 */
    +	private static final ConcurrentHashMap<String, IndexWriter> writerMap = new ConcurrentHashMap<String, IndexWriter>();
    +	
    +	static{
    +		Runtime.getRuntime().addShutdownHook(new Thread() {
    +			@Override
    +			public void run() {
    +				for(IndexWriter writer: writerMap.values()){
    +					if(null!=writer && writer.isOpen()){
    +						try {
    +							writer.commit();
    +							writer.close();
    +						} catch (IOException e) {
    +							LOGGER.error(e.getMessage(), e);
    +						}
    +					}
    +				}
    +				writerMap.clear();
    +			}
    +		});
    +		
    +	}
    +
    +	protected LuceneAppender(String name, boolean ignoreExceptions, Filter filter,
    +			Layout<? extends Serializable> layout, String target, Integer expiryTime, LuceneIndexField[] indexFields) {
    +		super(name, filter, layout, ignoreExceptions);
    +		this.target = target;
    +		this.expiryTime = expiryTime;
    +		this.indexFields = indexFields;
    +		
    +		initIndexWriter();
    +		registerCommitTimer();
    +		if (this.expiryTime != null){
    +			registerClearTimer();
    +		}
    +	}
    +
    +	/**
    +	 * IndexWriter initialization.
    +	 */
    +	private IndexWriter initIndexWriter() {
    +		if (null == writerMap.get(target)) {
    +			try {
    +				FSDirectory fsDir = FSDirectory.open(Paths.get(this.target));
    +				IndexWriterConfig writerConfig = new IndexWriterConfig(new LuceneAnalyzer());
    +				writerMap.putIfAbsent(target, new IndexWriter(fsDir, writerConfig));
    +			} catch (IOException e) {
    +				LOGGER.error("IndexWriter initialization failed!" + e.getMessage(), e);
    +			}
    +		}
    +		return writerMap.get(target);
    +	}
    +
    +	/**
    +	 * Register IndexWriter commit timertask.
    +	 */
    +	private void registerCommitTimer() {
    +		scheduledExecutor.scheduleAtFixedRate(new Runnable() {
    +			@Override
    +			public void run() {
    +				IndexWriter indexWriter = initIndexWriter();
    +				if (null!=indexWriter && indexWriter.numRamDocs()>0) {
    +					try {
    +						indexWriter.commit();
    +					} catch (IOException e) {
    +						LOGGER.error("IndexWriter commit failed!"+e.getMessage(),e);
    +					}
    +				}
    +			}
    +		}, 1, 1, TimeUnit.MINUTES);
    +	}
    +	
    +	/**
    +	 * Register IndexWriter clean timertask.
    +	 * Delete the index before {@link LuceneAppender#expiryTime} second every day at 0.
    +	 * 
    +	 * @see LuceneAppender#expiryTime
    +	 */
    +	private void registerClearTimer() {
    +		Calendar calendar = Calendar.getInstance();
    +		long curMillis = calendar.getTimeInMillis();
    +		calendar.add(Calendar.DAY_OF_MONTH, 1);
    +		calendar.set(Calendar.HOUR_OF_DAY, 0);
    +		calendar.set(Calendar.MINUTE, 0);
    +		calendar.set(Calendar.SECOND, 0);
    +		calendar.set(Calendar.MILLISECOND, 0);
    +		long difMinutes = (calendar.getTimeInMillis() - curMillis) / (1000 * 60);
    +
    +		scheduledExecutor.scheduleAtFixedRate(new Runnable() {
    +			@Override
    +			public void run() {
    +				LOGGER.info("delete index start!",target,expiryTime);
    +				IndexWriter indexWriter = initIndexWriter();
    +				if (null != indexWriter) {
    +					Long start = 0L;
    +					Long end = System.currentTimeMillis() - expiryTime * 1000;
    +					NumericRangeQuery<Long> rangeQuery = NumericRangeQuery.newLongRange("timestamp", start, end, true, true);
    +					try {
    +						indexWriter.deleteDocuments(rangeQuery);
    +						indexWriter.commit();
    +						LOGGER.info("delete index end! ",target,expiryTime);
    +					}catch (IOException e) {
    +						LOGGER.error("delete index failed! "+e.getMessage(),e);
    +					}
    +				}
    +			}
    +		}, difMinutes, 1440, TimeUnit.MINUTES);
    +	}
    +
    +	/**
    +	 * create lucene index.
    +	 * 
    +	 * @param event
    +	 */
    +	@Override
    +	public void append(LogEvent event) {
    +		if (null != indexFields && indexFields.length > 0) {
    +			IndexWriter indexWriter = initIndexWriter();
    +			if(null != indexWriter){
    +				Document doc = new Document();
    +				doc.add(new LongField("timestamp", event.getTimeMillis(), Field.Store.YES));
    +				try {
    +					for (LuceneIndexField field : indexFields) {
    +						String value = field.getLayout().toSerializable(event);
    +						if (Strings.isEmpty(value) || value.matches("[$]\\{.+\\}")) {
    +							return;
    +						}
    +						value = value.trim();
    +						String type = field.getType();
    +						if (Strings.isNotEmpty(type)) {
    +							Class<?> clazz = Class.forName("org.apache.lucene.document." + type);
    --- End diff --
    
    Try to stick to the `LoaderUtil` methods for loading classes unless you have a ClassLoader already (in which case there might be another method in LoaderUtil or Loader for that)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] logging-log4j2 pull request #74: Add a new LuceneAppender which writes loggi...

Posted by jvz <gi...@git.apache.org>.
Github user jvz commented on a diff in the pull request:

    https://github.com/apache/logging-log4j2/pull/74#discussion_r113044694
  
    --- Diff: log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/lucene/LuceneAppender.java ---
    @@ -0,0 +1,301 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements. See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You 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.
    + */
    +package org.apache.logging.log4j.nosql.appender.lucene;
    +
    +import java.io.IOException;
    +import java.io.Serializable;
    +import java.nio.file.Paths;
    +import java.util.Calendar;
    +import java.util.concurrent.ConcurrentHashMap;
    +import java.util.concurrent.Executors;
    +import java.util.concurrent.ScheduledExecutorService;
    +import java.util.concurrent.TimeUnit;
    +
    +import org.apache.logging.log4j.core.Appender;
    +import org.apache.logging.log4j.core.Filter;
    +import org.apache.logging.log4j.core.Layout;
    +import org.apache.logging.log4j.core.LogEvent;
    +import org.apache.logging.log4j.core.appender.AbstractAppender;
    +import org.apache.logging.log4j.core.appender.AppenderLoggingException;
    +import org.apache.logging.log4j.core.config.Node;
    +import org.apache.logging.log4j.core.config.plugins.Plugin;
    +import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
    +import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
    +import org.apache.logging.log4j.core.config.plugins.PluginElement;
    +import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
    +import org.apache.logging.log4j.util.Strings;
    +import org.apache.lucene.document.Document;
    +import org.apache.lucene.document.Field;
    +import org.apache.lucene.document.LongField;
    +import org.apache.lucene.document.StringField;
    +import org.apache.lucene.document.TextField;
    +import org.apache.lucene.index.IndexWriter;
    +import org.apache.lucene.index.IndexWriterConfig;
    +import org.apache.lucene.search.NumericRangeQuery;
    +import org.apache.lucene.store.FSDirectory;
    +
    +/**
    + * This Appender writes logging events to a lucene index library. It takes a list of
    + * {@link IndexField} with which determines which fields are written to the index library.
    + * <Lucene name="lucene" ignoreExceptions="true" target="/target/lucene/index">
    --- End diff --
    
    Might want to surround this with something like `<pre>{@code ... }</pre>` or however it'd be best formatted. XML doesn't work well in javadocs ;)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] logging-log4j2 pull request #74: Add a new LuceneAppender which writes loggi...

Posted by liyuj <gi...@git.apache.org>.
Github user liyuj closed the pull request at:

    https://github.com/apache/logging-log4j2/pull/74


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] logging-log4j2 pull request #74: Add a new LuceneAppender which writes loggi...

Posted by jvz <gi...@git.apache.org>.
Github user jvz commented on a diff in the pull request:

    https://github.com/apache/logging-log4j2/pull/74#discussion_r113045083
  
    --- Diff: log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/lucene/LuceneAppender.java ---
    @@ -0,0 +1,301 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements. See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You 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.
    + */
    +package org.apache.logging.log4j.nosql.appender.lucene;
    +
    +import java.io.IOException;
    +import java.io.Serializable;
    +import java.nio.file.Paths;
    +import java.util.Calendar;
    +import java.util.concurrent.ConcurrentHashMap;
    +import java.util.concurrent.Executors;
    +import java.util.concurrent.ScheduledExecutorService;
    +import java.util.concurrent.TimeUnit;
    +
    +import org.apache.logging.log4j.core.Appender;
    +import org.apache.logging.log4j.core.Filter;
    +import org.apache.logging.log4j.core.Layout;
    +import org.apache.logging.log4j.core.LogEvent;
    +import org.apache.logging.log4j.core.appender.AbstractAppender;
    +import org.apache.logging.log4j.core.appender.AppenderLoggingException;
    +import org.apache.logging.log4j.core.config.Node;
    +import org.apache.logging.log4j.core.config.plugins.Plugin;
    +import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
    +import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
    +import org.apache.logging.log4j.core.config.plugins.PluginElement;
    +import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
    +import org.apache.logging.log4j.util.Strings;
    +import org.apache.lucene.document.Document;
    +import org.apache.lucene.document.Field;
    +import org.apache.lucene.document.LongField;
    +import org.apache.lucene.document.StringField;
    +import org.apache.lucene.document.TextField;
    +import org.apache.lucene.index.IndexWriter;
    +import org.apache.lucene.index.IndexWriterConfig;
    +import org.apache.lucene.search.NumericRangeQuery;
    +import org.apache.lucene.store.FSDirectory;
    +
    +/**
    + * This Appender writes logging events to a lucene index library. It takes a list of
    + * {@link IndexField} with which determines which fields are written to the index library.
    + * <Lucene name="lucene" ignoreExceptions="true" target="/target/lucene/index">
    + *    <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
    + *    
    + *	  <IndexField name="time" pattern="%d{UNIX_MILLIS}" type="LongField"/>
    + *	  <IndexField name="level" pattern="%-5level" />
    + *	  <IndexField name="content" pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
    + * </Lucene>
    + */
    +@Plugin(name = "Lucene", category = Node.CATEGORY, elementType = Appender.ELEMENT_TYPE, printObject = true)
    +public class LuceneAppender extends AbstractAppender {
    +	/**
    +	 * index directory
    +	 */
    +	private final String target;
    +	/**
    +	 * Index expiration time (seconds)
    +	 */
    +	private final Integer expiryTime;
    +	/**
    +	 * IndexField array.
    +	 */
    +	private final LuceneIndexField[] indexFields;
    +	/**
    +	 * Periodically clear the index and submit the IndexWriter thread pool
    +	 */
    +	private final ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(2);
    +	/**
    +	 * IndexWriter corresponding to each index directory.
    +	 */
    +	private static final ConcurrentHashMap<String, IndexWriter> writerMap = new ConcurrentHashMap<String, IndexWriter>();
    +	
    +	static{
    +		Runtime.getRuntime().addShutdownHook(new Thread() {
    --- End diff --
    
    Not a fan of adding a shutdown hook here when it's already managed by the stop() method.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] logging-log4j2 issue #74: Add a new LuceneAppender which writes logging even...

Posted by jvz <gi...@git.apache.org>.
Github user jvz commented on the issue:

    https://github.com/apache/logging-log4j2/pull/74
  
    That makes sense. In the case of Elasticsearch (1.x, 2.x, and 5.x), you can support all versions using the same code in certain scenarios (usually simple things). If Lucene follows the usual Apache idea of renaming their packages between major releases, then it'd be necessary to have both modules anyways.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] logging-log4j2 pull request #74: Add a new LuceneAppender which writes loggi...

Posted by jvz <gi...@git.apache.org>.
Github user jvz commented on a diff in the pull request:

    https://github.com/apache/logging-log4j2/pull/74#discussion_r113046801
  
    --- Diff: log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/lucene/LuceneAppender.java ---
    @@ -0,0 +1,301 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements. See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You 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.
    + */
    +package org.apache.logging.log4j.nosql.appender.lucene;
    +
    +import java.io.IOException;
    +import java.io.Serializable;
    +import java.nio.file.Paths;
    +import java.util.Calendar;
    +import java.util.concurrent.ConcurrentHashMap;
    +import java.util.concurrent.Executors;
    +import java.util.concurrent.ScheduledExecutorService;
    +import java.util.concurrent.TimeUnit;
    +
    +import org.apache.logging.log4j.core.Appender;
    +import org.apache.logging.log4j.core.Filter;
    +import org.apache.logging.log4j.core.Layout;
    +import org.apache.logging.log4j.core.LogEvent;
    +import org.apache.logging.log4j.core.appender.AbstractAppender;
    +import org.apache.logging.log4j.core.appender.AppenderLoggingException;
    +import org.apache.logging.log4j.core.config.Node;
    +import org.apache.logging.log4j.core.config.plugins.Plugin;
    +import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
    +import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
    +import org.apache.logging.log4j.core.config.plugins.PluginElement;
    +import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
    +import org.apache.logging.log4j.util.Strings;
    +import org.apache.lucene.document.Document;
    +import org.apache.lucene.document.Field;
    +import org.apache.lucene.document.LongField;
    +import org.apache.lucene.document.StringField;
    +import org.apache.lucene.document.TextField;
    +import org.apache.lucene.index.IndexWriter;
    +import org.apache.lucene.index.IndexWriterConfig;
    +import org.apache.lucene.search.NumericRangeQuery;
    +import org.apache.lucene.store.FSDirectory;
    +
    +/**
    + * This Appender writes logging events to a lucene index library. It takes a list of
    + * {@link IndexField} with which determines which fields are written to the index library.
    + * <Lucene name="lucene" ignoreExceptions="true" target="/target/lucene/index">
    + *    <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
    + *    
    + *	  <IndexField name="time" pattern="%d{UNIX_MILLIS}" type="LongField"/>
    + *	  <IndexField name="level" pattern="%-5level" />
    + *	  <IndexField name="content" pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
    + * </Lucene>
    + */
    +@Plugin(name = "Lucene", category = Node.CATEGORY, elementType = Appender.ELEMENT_TYPE, printObject = true)
    +public class LuceneAppender extends AbstractAppender {
    +	/**
    +	 * index directory
    +	 */
    +	private final String target;
    +	/**
    +	 * Index expiration time (seconds)
    +	 */
    +	private final Integer expiryTime;
    +	/**
    +	 * IndexField array.
    +	 */
    +	private final LuceneIndexField[] indexFields;
    +	/**
    +	 * Periodically clear the index and submit the IndexWriter thread pool
    +	 */
    +	private final ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(2);
    +	/**
    +	 * IndexWriter corresponding to each index directory.
    +	 */
    +	private static final ConcurrentHashMap<String, IndexWriter> writerMap = new ConcurrentHashMap<String, IndexWriter>();
    +	
    +	static{
    +		Runtime.getRuntime().addShutdownHook(new Thread() {
    +			@Override
    +			public void run() {
    +				for(IndexWriter writer: writerMap.values()){
    +					if(null!=writer && writer.isOpen()){
    +						try {
    +							writer.commit();
    +							writer.close();
    +						} catch (IOException e) {
    +							LOGGER.error(e.getMessage(), e);
    +						}
    +					}
    +				}
    +				writerMap.clear();
    +			}
    +		});
    +		
    +	}
    +
    +	protected LuceneAppender(String name, boolean ignoreExceptions, Filter filter,
    +			Layout<? extends Serializable> layout, String target, Integer expiryTime, LuceneIndexField[] indexFields) {
    +		super(name, filter, layout, ignoreExceptions);
    +		this.target = target;
    +		this.expiryTime = expiryTime;
    +		this.indexFields = indexFields;
    +		
    +		initIndexWriter();
    +		registerCommitTimer();
    +		if (this.expiryTime != null){
    +			registerClearTimer();
    +		}
    +	}
    +
    +	/**
    +	 * IndexWriter initialization.
    +	 */
    +	private IndexWriter initIndexWriter() {
    +		if (null == writerMap.get(target)) {
    +			try {
    +				FSDirectory fsDir = FSDirectory.open(Paths.get(this.target));
    +				IndexWriterConfig writerConfig = new IndexWriterConfig(new LuceneAnalyzer());
    +				writerMap.putIfAbsent(target, new IndexWriter(fsDir, writerConfig));
    +			} catch (IOException e) {
    +				LOGGER.error("IndexWriter initialization failed!" + e.getMessage(), e);
    +			}
    +		}
    +		return writerMap.get(target);
    +	}
    +
    +	/**
    +	 * Register IndexWriter commit timertask.
    +	 */
    +	private void registerCommitTimer() {
    +		scheduledExecutor.scheduleAtFixedRate(new Runnable() {
    +			@Override
    +			public void run() {
    +				IndexWriter indexWriter = initIndexWriter();
    +				if (null!=indexWriter && indexWriter.numRamDocs()>0) {
    +					try {
    +						indexWriter.commit();
    +					} catch (IOException e) {
    +						LOGGER.error("IndexWriter commit failed!"+e.getMessage(),e);
    +					}
    +				}
    +			}
    +		}, 1, 1, TimeUnit.MINUTES);
    +	}
    +	
    +	/**
    +	 * Register IndexWriter clean timertask.
    +	 * Delete the index before {@link LuceneAppender#expiryTime} second every day at 0.
    +	 * 
    +	 * @see LuceneAppender#expiryTime
    +	 */
    +	private void registerClearTimer() {
    +		Calendar calendar = Calendar.getInstance();
    +		long curMillis = calendar.getTimeInMillis();
    +		calendar.add(Calendar.DAY_OF_MONTH, 1);
    +		calendar.set(Calendar.HOUR_OF_DAY, 0);
    +		calendar.set(Calendar.MINUTE, 0);
    +		calendar.set(Calendar.SECOND, 0);
    +		calendar.set(Calendar.MILLISECOND, 0);
    +		long difMinutes = (calendar.getTimeInMillis() - curMillis) / (1000 * 60);
    +
    +		scheduledExecutor.scheduleAtFixedRate(new Runnable() {
    +			@Override
    +			public void run() {
    +				LOGGER.info("delete index start!",target,expiryTime);
    +				IndexWriter indexWriter = initIndexWriter();
    +				if (null != indexWriter) {
    +					Long start = 0L;
    +					Long end = System.currentTimeMillis() - expiryTime * 1000;
    +					NumericRangeQuery<Long> rangeQuery = NumericRangeQuery.newLongRange("timestamp", start, end, true, true);
    +					try {
    +						indexWriter.deleteDocuments(rangeQuery);
    +						indexWriter.commit();
    +						LOGGER.info("delete index end! ",target,expiryTime);
    +					}catch (IOException e) {
    +						LOGGER.error("delete index failed! "+e.getMessage(),e);
    +					}
    +				}
    +			}
    +		}, difMinutes, 1440, TimeUnit.MINUTES);
    +	}
    +
    +	/**
    +	 * create lucene index.
    +	 * 
    +	 * @param event
    +	 */
    +	@Override
    +	public void append(LogEvent event) {
    +		if (null != indexFields && indexFields.length > 0) {
    +			IndexWriter indexWriter = initIndexWriter();
    +			if(null != indexWriter){
    +				Document doc = new Document();
    +				doc.add(new LongField("timestamp", event.getTimeMillis(), Field.Store.YES));
    +				try {
    +					for (LuceneIndexField field : indexFields) {
    +						String value = field.getLayout().toSerializable(event);
    +						if (Strings.isEmpty(value) || value.matches("[$]\\{.+\\}")) {
    +							return;
    +						}
    +						value = value.trim();
    +						String type = field.getType();
    +						if (Strings.isNotEmpty(type)) {
    +							Class<?> clazz = Class.forName("org.apache.lucene.document." + type);
    +							if (clazz == LongField.class) {
    +								doc.add(new LongField(field.getName(), Long.valueOf(value), Field.Store.YES));
    +							} else if (clazz == StringField.class) {
    +								doc.add(new StringField(field.getName(), value, Field.Store.YES));
    +							} else if (clazz == TextField.class) {
    +								doc.add(new TextField(field.getName(), value, Field.Store.YES));
    +							} else {
    +								throw new UnsupportedOperationException(type + " type currently not supported.");
    +							}
    +						} else {
    +							doc.add(new TextField(field.getName(), value, Field.Store.YES));
    +						}
    +					}
    +					indexWriter.addDocument(doc);
    +				} catch (Exception e) {
    +					LOGGER.error(e.getMessage(), e);
    +					if (!ignoreExceptions()) {
    +						throw new AppenderLoggingException(e);
    +					}
    +				}
    +			}
    +		}
    +	}
    +	
    +	@PluginBuilderFactory
    +    public static <B extends Builder<B>> B newBuilder() {
    +        return new Builder<B>().asBuilder();
    +    }
    +
    +    public static class Builder<B extends Builder<B>> extends AbstractAppender.Builder<B>
    +        implements org.apache.logging.log4j.core.util.Builder<LuceneAppender> {
    +
    +        @PluginElement("IndexField")
    +        @Required(message = "No IndexField provided")
    +        private LuceneIndexField[] indexField;        
    +
    +        @PluginBuilderAttribute        
    +        private Integer expiryTime;
    --- End diff --
    
    Is the default supposed to be null?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] logging-log4j2 issue #74: Add a new LuceneAppender which writes logging even...

Posted by mikaelstaldal <gi...@git.apache.org>.
Github user mikaelstaldal commented on the issue:

    https://github.com/apache/logging-log4j2/pull/74
  
    I suggest we place this in a new module "log4j-lucene" instead of in log4j-nosql. We probably want to get rid of log4j-nosql, so we shouldn't put new stuff in it.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] logging-log4j2 issue #74: Add a new LuceneAppender which writes logging even...

Posted by liyuj <gi...@git.apache.org>.
Github user liyuj commented on the issue:

    https://github.com/apache/logging-log4j2/pull/74
  
    Lucene5 API and lucene6 are incompatible.
    I think the future is divided into multiple modules is more reasonable, such as log4j-lucene5 and log4j-lucene6.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] logging-log4j2 issue #74: Add a new LuceneAppender which writes logging even...

Posted by garydgregory <gi...@git.apache.org>.
Github user garydgregory commented on the issue:

    https://github.com/apache/logging-log4j2/pull/74
  
    I was thinking that if you can use Java 8 then you depend on log4j-lucene6 and if you can't then log4j-lucene5.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] logging-log4j2 issue #74: Add a new LuceneAppender which writes logging even...

Posted by garydgregory <gi...@git.apache.org>.
Github user garydgregory commented on the issue:

    https://github.com/apache/logging-log4j2/pull/74
  
    And maybe even log4j-lucene5...


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] logging-log4j2 issue #74: Add a new LuceneAppender which writes logging even...

Posted by garydgregory <gi...@git.apache.org>.
Github user garydgregory commented on the issue:

    https://github.com/apache/logging-log4j2/pull/74
  
    How about updating the patch to provide a new module log4j-lucene5?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---