Commit | Line | Data |
---|---|---|
d60dfbe4 AM |
1 | /* |
2 | * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com> | |
3 | * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com> | |
4 | * | |
5 | * This library is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU Lesser General Public License, version 2.1 only, | |
7 | * as published by the Free Software Foundation. | |
8 | * | |
9 | * This library is distributed in the hope that it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | |
12 | * for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU Lesser General Public License | |
15 | * along with this library; if not, write to the Free Software Foundation, | |
16 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 | */ | |
18 | ||
19 | package org.lttng.ust.agent.jul; | |
20 | ||
21 | import java.io.IOException; | |
8ab5c06b AM |
22 | import java.util.Collection; |
23 | import java.util.Map; | |
24 | import java.util.Map.Entry; | |
d60dfbe4 | 25 | import java.util.concurrent.atomic.AtomicLong; |
4721f9c7 | 26 | import java.util.logging.Formatter; |
d60dfbe4 AM |
27 | import java.util.logging.Handler; |
28 | import java.util.logging.LogRecord; | |
29 | ||
30 | import org.lttng.ust.agent.ILttngAgent; | |
31 | import org.lttng.ust.agent.ILttngHandler; | |
8ab5c06b | 32 | import org.lttng.ust.agent.context.ContextInfoSerializer; |
d60dfbe4 AM |
33 | |
34 | /** | |
35 | * LTTng-UST JUL log handler. | |
36 | * | |
37 | * Applications can attach this handler to their | |
38 | * {@link java.util.logging.Logger} to have it generate UST events from logging | |
39 | * events received through the logger. | |
40 | * | |
41 | * It sends its events to UST via the JNI library "liblttng-ust-jul-jni.so". | |
42 | * Make sure this library is available before using this handler. | |
43 | * | |
44 | * @author Alexandre Montplaisir | |
45 | * @author David Goulet | |
46 | */ | |
47 | public class LttngLogHandler extends Handler implements ILttngHandler { | |
48 | ||
49 | private static final String SHARED_OBJECT_NAME = "lttng-ust-jul-jni"; | |
50 | ||
4721f9c7 AM |
51 | /** |
52 | * Dummy Formatter object, so we can use its | |
53 | * {@link Formatter#formatMessage(LogRecord)} method. | |
54 | */ | |
55 | private static final Formatter FORMATTER = new Formatter() { | |
56 | @Override | |
57 | public String format(LogRecord record) { | |
58 | throw new UnsupportedOperationException(); | |
59 | } | |
60 | }; | |
61 | ||
d60dfbe4 AM |
62 | private final ILttngAgent<LttngLogHandler> agent; |
63 | ||
64 | /** Number of events logged (really sent through JNI) by this handler */ | |
65 | private final AtomicLong eventCount = new AtomicLong(0); | |
66 | ||
67 | /** | |
68 | * Constructor | |
69 | * | |
70 | * @throws IOException | |
71 | * This handler requires the lttng-ust-jul-jni.so native | |
72 | * library, through which it will send the trace events. This | |
73 | * exception is throw is this library cannot be found. | |
74 | * @throws SecurityException | |
75 | * We will forward any SecurityExcepion that may be thrown when | |
76 | * trying to load the JNI library. | |
77 | */ | |
78 | public LttngLogHandler() throws IOException, SecurityException { | |
79 | super(); | |
80 | /* Initialize LTTng UST tracer. */ | |
81 | try { | |
82 | System.loadLibrary(SHARED_OBJECT_NAME); //$NON-NLS-1$ | |
83 | } catch (UnsatisfiedLinkError e) { | |
84 | throw new IOException(e); | |
85 | } | |
86 | ||
87 | /** Register to the relevant agent */ | |
88 | agent = LttngJulAgent.getInstance(); | |
89 | agent.registerHandler(this); | |
90 | } | |
91 | ||
92 | @Override | |
93 | public synchronized void close() { | |
94 | agent.unregisterHandler(this); | |
95 | } | |
96 | ||
97 | /** | |
98 | * Get the number of events logged by this handler so far. This means the | |
99 | * number of events actually sent through JNI to UST. | |
100 | * | |
101 | * @return The number of events logged so far | |
102 | */ | |
103 | @Override | |
104 | public long getEventCount() { | |
105 | return eventCount.get(); | |
106 | } | |
107 | ||
108 | @Override | |
109 | public void flush() { | |
110 | } | |
111 | ||
112 | @Override | |
113 | public void publish(LogRecord record) { | |
114 | /* | |
115 | * Check if the current message should be logged, according to the UST | |
116 | * session settings. | |
117 | */ | |
118 | if (!agent.isEventEnabled(record.getLoggerName())) { | |
119 | return; | |
120 | } | |
121 | ||
4721f9c7 AM |
122 | String formattedMessage = FORMATTER.formatMessage(record); |
123 | ||
8ab5c06b AM |
124 | /* Retrieve all the requested context information we can find */ |
125 | Collection<Entry<String, Map<String, Integer>>> enabledContexts = agent.getEnabledAppContexts(); | |
b1ca4c5f | 126 | ContextInfoSerializer.SerializedContexts contextInfo = ContextInfoSerializer.queryAndSerializeRequestedContexts(enabledContexts); |
8ab5c06b | 127 | |
d60dfbe4 | 128 | eventCount.incrementAndGet(); |
8ab5c06b | 129 | |
d60dfbe4 AM |
130 | /* |
131 | * Specific tracepoint designed for JUL events. The source class of the | |
132 | * caller is used for the event name, the raw message is taken, the | |
133 | * loglevel of the record and the thread ID. | |
134 | */ | |
8ab5c06b | 135 | LttngJulApi.tracepointWithContext(formattedMessage, |
d60dfbe4 AM |
136 | record.getLoggerName(), |
137 | record.getSourceClassName(), | |
138 | record.getSourceMethodName(), | |
139 | record.getMillis(), | |
140 | record.getLevel().intValue(), | |
8ab5c06b | 141 | record.getThreadID(), |
b1ca4c5f AM |
142 | contextInfo.getEntriesArray(), |
143 | contextInfo.getStringsArray()); | |
d60dfbe4 AM |
144 | } |
145 | ||
d60dfbe4 | 146 | } |