Search in sources :

Example 1 with LoggerInfo

use of org.apache.solr.logging.LoggerInfo in project lucene-solr by apache.

the class Log4jWatcher method getAllLoggers.

@Override
public Collection<LoggerInfo> getAllLoggers() {
    org.apache.log4j.Logger root = org.apache.log4j.LogManager.getRootLogger();
    Map<String, LoggerInfo> map = new HashMap<>();
    Enumeration<?> loggers = org.apache.log4j.LogManager.getCurrentLoggers();
    while (loggers.hasMoreElements()) {
        org.apache.log4j.Logger logger = (org.apache.log4j.Logger) loggers.nextElement();
        String name = logger.getName();
        if (logger == root) {
            continue;
        }
        map.put(name, new Log4jInfo(name, logger));
        while (true) {
            int dot = name.lastIndexOf(".");
            if (dot < 0)
                break;
            name = name.substring(0, dot);
            if (!map.containsKey(name)) {
                map.put(name, new Log4jInfo(name, null));
            }
        }
    }
    map.put(LoggerInfo.ROOT_NAME, new Log4jInfo(LoggerInfo.ROOT_NAME, root));
    return map.values();
}
Also used : Logger(org.apache.log4j.Logger) HashMap(java.util.HashMap) LoggerInfo(org.apache.solr.logging.LoggerInfo) Logger(org.apache.log4j.Logger)

Example 2 with LoggerInfo

use of org.apache.solr.logging.LoggerInfo in project lucene-solr by apache.

the class JulWatcher method getAllLoggers.

@Override
public Collection<LoggerInfo> getAllLoggers() {
    LogManager manager = LogManager.getLogManager();
    Logger root = manager.getLogger("");
    Map<String, LoggerInfo> map = new HashMap<>();
    Enumeration<String> names = manager.getLoggerNames();
    while (names.hasMoreElements()) {
        String name = names.nextElement();
        Logger logger = Logger.getLogger(name);
        if (logger == root) {
            continue;
        }
        map.put(name, new JulInfo(name, logger));
        while (true) {
            int dot = name.lastIndexOf(".");
            if (dot < 0)
                break;
            name = name.substring(0, dot);
            if (!map.containsKey(name)) {
                map.put(name, new JulInfo(name, null));
            }
        }
    }
    map.put(LoggerInfo.ROOT_NAME, new JulInfo(LoggerInfo.ROOT_NAME, root));
    return map.values();
}
Also used : HashMap(java.util.HashMap) LoggerInfo(org.apache.solr.logging.LoggerInfo) Logger(java.util.logging.Logger) LogManager(java.util.logging.LogManager)

Example 3 with LoggerInfo

use of org.apache.solr.logging.LoggerInfo in project lucene-solr by apache.

the class LoggingHandler method handleRequestBody.

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
    // Don't do anything if the framework is unknown
    if (watcher == null) {
        rsp.add("error", "Logging Not Initialized");
        return;
    }
    rsp.add("watcher", watcher.getName());
    SolrParams params = req.getParams();
    if (params.get("threshold") != null) {
        watcher.setThreshold(params.get("threshold"));
    }
    // Write something at each level
    if (params.get("test") != null) {
        log.trace("trace message");
        log.debug("debug message");
        log.info("info (with exception)", new RuntimeException("test"));
        log.warn("warn (with exception)", new RuntimeException("test"));
        log.error("error (with exception)", new RuntimeException("test"));
    }
    String[] set = params.getParams("set");
    if (set != null) {
        for (String pair : set) {
            String[] split = pair.split(":");
            if (split.length != 2) {
                throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Invalid format, expected level:value, got " + pair);
            }
            String category = split[0];
            String level = split[1];
            watcher.setLogLevel(category, level);
        }
    }
    String since = req.getParams().get("since");
    if (since != null) {
        long time = -1;
        try {
            time = Long.parseLong(since);
        } catch (Exception ex) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "invalid timestamp: " + since);
        }
        AtomicBoolean found = new AtomicBoolean(false);
        SolrDocumentList docs = watcher.getHistory(time, found);
        if (docs == null) {
            rsp.add("error", "History not enabled");
            return;
        } else {
            SimpleOrderedMap<Object> info = new SimpleOrderedMap<>();
            if (time > 0) {
                info.add("since", time);
                info.add("found", found.get());
            } else {
                // show for the first request
                info.add("levels", watcher.getAllLevels());
            }
            info.add("last", watcher.getLastEvent());
            info.add("buffer", watcher.getHistorySize());
            info.add("threshold", watcher.getThreshold());
            rsp.add("info", info);
            rsp.add("history", docs);
        }
    } else {
        rsp.add("levels", watcher.getAllLevels());
        List<LoggerInfo> loggers = new ArrayList<>(watcher.getAllLoggers());
        Collections.sort(loggers);
        List<SimpleOrderedMap<?>> info = new ArrayList<>();
        for (LoggerInfo wrap : loggers) {
            info.add(wrap.getInfo());
        }
        rsp.add("loggers", info);
    }
    rsp.setHttpCaching(false);
}
Also used : ArrayList(java.util.ArrayList) LoggerInfo(org.apache.solr.logging.LoggerInfo) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) SolrException(org.apache.solr.common.SolrException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SolrParams(org.apache.solr.common.params.SolrParams) SolrException(org.apache.solr.common.SolrException)

Aggregations

LoggerInfo (org.apache.solr.logging.LoggerInfo)3 HashMap (java.util.HashMap)2 ArrayList (java.util.ArrayList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 LogManager (java.util.logging.LogManager)1 Logger (java.util.logging.Logger)1 Logger (org.apache.log4j.Logger)1 SolrDocumentList (org.apache.solr.common.SolrDocumentList)1 SolrException (org.apache.solr.common.SolrException)1 SolrParams (org.apache.solr.common.params.SolrParams)1 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)1