Move to kernel style SPDX license identifiers
[lttng-ust.git] / liblttng-ust-java-agent / java / lttng-ust-agent-log4j / org / lttng / ust / agent / log4j / LttngLog4jAgent.java
CommitLineData
5b5ffa03 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-only
5b5ffa03 3 *
c0c0989a
MJ
4 * Copyright (C) 2015 EfficiOS Inc.
5 * Copyright (C) 2015 Alexandre Montplaisir <alexmonthy@efficios.com>
5b5ffa03
DG
6 */
7
d60dfbe4
AM
8package org.lttng.ust.agent.log4j;
9
68a1ef73
AM
10import java.util.Collection;
11import java.util.Collections;
a548b3c1 12import java.util.Enumeration;
68a1ef73 13import java.util.List;
46070114
AM
14import java.util.Set;
15import java.util.TreeSet;
68a1ef73
AM
16
17import org.apache.log4j.Appender;
46070114 18import org.apache.log4j.Category;
68a1ef73
AM
19import org.apache.log4j.LogManager;
20import org.apache.log4j.Logger;
d60dfbe4
AM
21import org.lttng.ust.agent.AbstractLttngAgent;
22
23/**
24 * Agent implementation for using the Log4j logger, connecting to a root session
25 * daemon.
26 *
27 * @author Alexandre Montplaisir
28 */
29class LttngLog4jAgent extends AbstractLttngAgent<LttngLogAppender> {
30
31 private static LttngLog4jAgent instance = null;
32
33 private LttngLog4jAgent() {
34 super(Domain.LOG4J);
35 }
5b5ffa03 36
d60dfbe4
AM
37 public static synchronized LttngLog4jAgent getInstance() {
38 if (instance == null) {
39 instance = new LttngLog4jAgent();
40 }
41 return instance;
42 }
5b5ffa03 43
68a1ef73
AM
44 @Override
45 public Collection<String> listAvailableEvents() {
46070114 46 Set<String> ret = new TreeSet<String>();
68a1ef73
AM
47
48 @SuppressWarnings("unchecked")
49 List<Logger> loggers = Collections.list(LogManager.getCurrentLoggers());
50 for (Logger logger : loggers) {
2b218c9a
AM
51 if (logger == null) {
52 continue;
53 }
54
68a1ef73
AM
55 /*
56 * Check if that logger has at least one LTTng log4j appender
57 * attached.
58 */
59 if (hasLttngAppenderAttached(logger)) {
60 ret.add(logger.getName());
61 }
62 }
63
64 return ret;
65 }
66
46070114 67 private static boolean hasLttngAppenderAttached(Category logger) {
68a1ef73 68 @SuppressWarnings("unchecked")
a548b3c1
AM
69 Enumeration<Appender> appenders = logger.getAllAppenders();
70 if (appenders != null) {
71 for (Appender appender : Collections.list(appenders)) {
72 if (appender instanceof LttngLogAppender) {
73 return true;
74 }
68a1ef73
AM
75 }
76 }
46070114
AM
77
78 /*
79 * A parent logger, if any, may be connected to an LTTng handler. In
80 * this case, we will want to include this child logger in the output,
81 * since it will be accessible by LTTng.
82 */
83 Category parent = logger.getParent();
84 if (parent != null) {
85 return hasLttngAppenderAttached(parent);
86 }
87
88 /*
89 * We have reached the root logger and have not found any LTTng handler,
90 * this event will not be accessible.
91 */
68a1ef73
AM
92 return false;
93 }
46070114 94
5b5ffa03 95}
This page took 0.031022 seconds and 4 git commands to generate.