blob: 30f4f6e2755e6f719288bb7f98fb8d2091e329be [file] [log] [blame]
/*
* 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.solr.logging;
import java.lang.invoke.MethodHandles;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.util.CollectionUtil;
import org.apache.solr.core.SolrResourceLoader;
import org.apache.solr.logging.jul.JulWatcher;
import org.apache.solr.logging.log4j2.Log4j2Watcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A Class to monitor Logging events and hold N events in memory
*
* <p>This is abstract so we can support both JUL and Log4j2 (and other logging platforms)
*/
public abstract class LogWatcher<E> {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
protected CircularList<SolrDocument> history;
protected long last = -1;
/**
* @return The implementation name
*/
public abstract String getName();
/**
* @return The valid level names for this framework
*/
public abstract List<String> getAllLevels();
/** Sets the log level within this framework */
public abstract void setLogLevel(String category, String level);
/**
* @return all registered loggers
*/
public abstract Collection<LoggerInfo> getAllLoggers();
public abstract void setThreshold(String level);
public abstract String getThreshold();
public void add(E event, long timstamp) {
history.add(unmodifiable(toSolrDocument(event)));
last = timstamp;
}
/**
* Since we store the derivative {@link SolrDocument} instances and potentially return the same
* instance to multiple callers, we should guard against modification.
*/
private static SolrDocument unmodifiable(SolrDocument doc) {
LinkedHashMap<String, Object> map = CollectionUtil.newLinkedHashMap(doc.size());
for (Map.Entry<String, Object> e : doc) {
Object v = e.getValue();
if (v instanceof Collection) {
map.put(e.getKey(), Collections.unmodifiableCollection((Collection<?>) v));
} else {
map.put(e.getKey(), v);
}
}
return new SolrDocument(Collections.unmodifiableMap(map));
}
public long getLastEvent() {
return last;
}
public int getHistorySize() {
return (history == null) ? -1 : history.getBufferSize();
}
public SolrDocumentList getHistory(long since, AtomicBoolean found) {
if (history == null) {
return null;
}
SolrDocumentList docs = new SolrDocumentList();
Iterator<SolrDocument> iter = history.iterator();
while (iter.hasNext()) {
SolrDocument logEventEntry = iter.next();
long ts = ((Date) logEventEntry.getFirstValue("time")).getTime();
if (ts == since) {
if (found != null) {
found.set(true);
}
}
if (ts > since) {
docs.add(logEventEntry);
}
}
docs.setNumFound(docs.size()); // make it not look too funny
return docs;
}
public abstract SolrDocument toSolrDocument(E event);
public abstract void registerListener(ListenerConfig cfg);
public void reset() {
history.clear();
last = -1;
}
/**
* Create and register a LogWatcher.
*
* <p>JUL and Log4j watchers are supported out-of-the-box. You can register your own LogWatcher
* implementation via the plugins architecture
*
* @param config a LogWatcherConfig object, containing the configuration for this LogWatcher.
* @param loader a SolrResourceLoader, to be used to load plugin LogWatcher implementations. Can
* be null if
* @return a LogWatcher configured for the container's logging framework
*/
public static LogWatcher<?> newRegisteredLogWatcher(
LogWatcherConfig config, SolrResourceLoader loader) {
if (!config.isEnabled()) {
log.debug("A LogWatcher is not enabled");
return null;
}
LogWatcher<?> logWatcher = createWatcher(config, loader);
if (logWatcher != null) {
if (config.getWatcherSize() > 0) {
if (log.isDebugEnabled()) {
log.debug("Registering Log Listener [{}]", logWatcher.getName());
}
logWatcher.registerListener(config.asListenerConfig());
}
}
return logWatcher;
}
private static LogWatcher<?> createWatcher(LogWatcherConfig config, SolrResourceLoader loader) {
String fname = config.getLoggingClass();
String slf4jImpl;
try {
slf4jImpl = LoggerFactory.getILoggerFactory().getClass().getName();
log.debug("SLF4J impl is {}", slf4jImpl);
if (fname == null) {
if ("org.apache.logging.slf4j.Log4jLoggerFactory".equals(slf4jImpl)) {
fname = "Log4j2";
} else if (slf4jImpl.indexOf("JDK") > 0) {
fname = "JUL";
}
}
} catch (Throwable e) {
log.warn("Unable to read SLF4J version. LogWatcher will be disabled: ", e);
if (e instanceof OutOfMemoryError) {
throw (OutOfMemoryError) e;
}
return null;
}
if (fname == null) {
log.debug("No LogWatcher configured");
return null;
}
if ("JUL".equalsIgnoreCase(fname)) {
return new JulWatcher(slf4jImpl);
}
if ("Log4j2".equals(fname)) {
return new Log4j2Watcher();
}
try {
return loader != null ? loader.newInstance(fname, LogWatcher.class) : null;
} catch (Throwable e) {
log.warn("Unable to load LogWatcher {}: {}", fname, e);
if (e instanceof OutOfMemoryError) {
throw (OutOfMemoryError) e;
}
}
return null;
}
}