| 1 | /* |
| 2 | * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com> |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU Lesser General Public License, version 2.1 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This library is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 11 | * for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public License |
| 14 | * along with this library; if not, write to the Free Software Foundation, |
| 15 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | package org.lttng.ust.agent.log4j; |
| 19 | |
| 20 | import java.util.Collection; |
| 21 | import java.util.Collections; |
| 22 | import java.util.List; |
| 23 | import java.util.Set; |
| 24 | import java.util.TreeSet; |
| 25 | |
| 26 | import org.apache.log4j.Appender; |
| 27 | import org.apache.log4j.Category; |
| 28 | import org.apache.log4j.LogManager; |
| 29 | import org.apache.log4j.Logger; |
| 30 | import org.lttng.ust.agent.AbstractLttngAgent; |
| 31 | |
| 32 | /** |
| 33 | * Agent implementation for using the Log4j logger, connecting to a root session |
| 34 | * daemon. |
| 35 | * |
| 36 | * @author Alexandre Montplaisir |
| 37 | */ |
| 38 | class LttngLog4jAgent extends AbstractLttngAgent<LttngLogAppender> { |
| 39 | |
| 40 | private static LttngLog4jAgent instance = null; |
| 41 | |
| 42 | private LttngLog4jAgent() { |
| 43 | super(Domain.LOG4J); |
| 44 | } |
| 45 | |
| 46 | public static synchronized LttngLog4jAgent getInstance() { |
| 47 | if (instance == null) { |
| 48 | instance = new LttngLog4jAgent(); |
| 49 | } |
| 50 | return instance; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public Collection<String> listAvailableEvents() { |
| 55 | Set<String> ret = new TreeSet<String>(); |
| 56 | |
| 57 | @SuppressWarnings("unchecked") |
| 58 | List<Logger> loggers = Collections.list(LogManager.getCurrentLoggers()); |
| 59 | for (Logger logger : loggers) { |
| 60 | if (logger == null) { |
| 61 | continue; |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * Check if that logger has at least one LTTng log4j appender |
| 66 | * attached. |
| 67 | */ |
| 68 | if (hasLttngAppenderAttached(logger)) { |
| 69 | ret.add(logger.getName()); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return ret; |
| 74 | } |
| 75 | |
| 76 | private static boolean hasLttngAppenderAttached(Category logger) { |
| 77 | @SuppressWarnings("unchecked") |
| 78 | List<Appender> appenders = Collections.list(logger.getAllAppenders()); |
| 79 | for (Appender appender : appenders) { |
| 80 | if (appender instanceof LttngLogAppender) { |
| 81 | return true; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * A parent logger, if any, may be connected to an LTTng handler. In |
| 87 | * this case, we will want to include this child logger in the output, |
| 88 | * since it will be accessible by LTTng. |
| 89 | */ |
| 90 | Category parent = logger.getParent(); |
| 91 | if (parent != null) { |
| 92 | return hasLttngAppenderAttached(parent); |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * We have reached the root logger and have not found any LTTng handler, |
| 97 | * this event will not be accessible. |
| 98 | */ |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | } |