Blame liblttng-ust-java-agent/java/lttng-ust-agent-log4j/org/lttng/ust/agent/log4j/LttngLog4jAgent.java

Packit c04fcb
/*
Packit c04fcb
 * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
Packit c04fcb
 *
Packit c04fcb
 * This library is free software; you can redistribute it and/or modify it
Packit c04fcb
 * under the terms of the GNU Lesser General Public License, version 2.1 only,
Packit c04fcb
 * as published by the Free Software Foundation.
Packit c04fcb
 *
Packit c04fcb
 * This library is distributed in the hope that it will be useful, but WITHOUT
Packit c04fcb
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
Packit c04fcb
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
Packit c04fcb
 * for more details.
Packit c04fcb
 *
Packit c04fcb
 * You should have received a copy of the GNU Lesser General Public License
Packit c04fcb
 * along with this library; if not, write to the Free Software Foundation,
Packit c04fcb
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Packit c04fcb
 */
Packit c04fcb
Packit c04fcb
package org.lttng.ust.agent.log4j;
Packit c04fcb
Packit c04fcb
import java.util.Collection;
Packit c04fcb
import java.util.Collections;
Packit c04fcb
import java.util.Enumeration;
Packit c04fcb
import java.util.List;
Packit c04fcb
import java.util.Set;
Packit c04fcb
import java.util.TreeSet;
Packit c04fcb
Packit c04fcb
import org.apache.log4j.Appender;
Packit c04fcb
import org.apache.log4j.Category;
Packit c04fcb
import org.apache.log4j.LogManager;
Packit c04fcb
import org.apache.log4j.Logger;
Packit c04fcb
import org.lttng.ust.agent.AbstractLttngAgent;
Packit c04fcb
Packit c04fcb
/**
Packit c04fcb
 * Agent implementation for using the Log4j logger, connecting to a root session
Packit c04fcb
 * daemon.
Packit c04fcb
 *
Packit c04fcb
 * @author Alexandre Montplaisir
Packit c04fcb
 */
Packit c04fcb
class LttngLog4jAgent extends AbstractLttngAgent<LttngLogAppender> {
Packit c04fcb
Packit c04fcb
	private static LttngLog4jAgent instance = null;
Packit c04fcb
Packit c04fcb
	private LttngLog4jAgent() {
Packit c04fcb
		super(Domain.LOG4J);
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	public static synchronized LttngLog4jAgent getInstance() {
Packit c04fcb
		if (instance == null) {
Packit c04fcb
			instance = new LttngLog4jAgent();
Packit c04fcb
		}
Packit c04fcb
		return instance;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	@Override
Packit c04fcb
	public Collection<String> listAvailableEvents() {
Packit c04fcb
		Set<String> ret = new TreeSet<String>();
Packit c04fcb
Packit c04fcb
		@SuppressWarnings("unchecked")
Packit c04fcb
		List<Logger> loggers = Collections.list(LogManager.getCurrentLoggers());
Packit c04fcb
		for (Logger logger : loggers) {
Packit c04fcb
			if (logger == null) {
Packit c04fcb
				continue;
Packit c04fcb
			}
Packit c04fcb
Packit c04fcb
			/*
Packit c04fcb
			 * Check if that logger has at least one LTTng log4j appender
Packit c04fcb
			 * attached.
Packit c04fcb
			 */
Packit c04fcb
			if (hasLttngAppenderAttached(logger)) {
Packit c04fcb
				ret.add(logger.getName());
Packit c04fcb
			}
Packit c04fcb
		}
Packit c04fcb
Packit c04fcb
		return ret;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	private static boolean hasLttngAppenderAttached(Category logger) {
Packit c04fcb
		@SuppressWarnings("unchecked")
Packit c04fcb
		Enumeration<Appender> appenders = logger.getAllAppenders();
Packit c04fcb
		if (appenders != null) {
Packit c04fcb
			for (Appender appender : Collections.list(appenders)) {
Packit c04fcb
				if (appender instanceof LttngLogAppender) {
Packit c04fcb
					return true;
Packit c04fcb
				}
Packit c04fcb
			}
Packit c04fcb
		}
Packit c04fcb
Packit c04fcb
		/*
Packit c04fcb
		 * A parent logger, if any, may be connected to an LTTng handler. In
Packit c04fcb
		 * this case, we will want to include this child logger in the output,
Packit c04fcb
		 * since it will be accessible by LTTng.
Packit c04fcb
		 */
Packit c04fcb
		Category parent = logger.getParent();
Packit c04fcb
		if (parent != null) {
Packit c04fcb
			return hasLttngAppenderAttached(parent);
Packit c04fcb
		}
Packit c04fcb
Packit c04fcb
		/*
Packit c04fcb
		 * We have reached the root logger and have not found any LTTng handler,
Packit c04fcb
		 * this event will not be accessible.
Packit c04fcb
		 */
Packit c04fcb
		return false;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
}