00c5c48cc77cb0ac352f57e1f5e7e0dffe6770fc
[lttng-ust.git] / liblttng-ust-java-agent / java / lttng-ust-agent-jul / org / lttng / ust / agent / jul / LttngLogHandler.java
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;
22 import java.util.Collection;
23 import java.util.Map;
24 import java.util.Map.Entry;
25 import java.util.concurrent.atomic.AtomicLong;
26 import java.util.logging.Formatter;
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;
32 import org.lttng.ust.agent.context.ContextInfoSerializer;
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
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
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
122 String formattedMessage = FORMATTER.formatMessage(record);
123
124 /* Retrieve all the requested context information we can find */
125 Collection<Entry<String, Map<String, Integer>>> enabledContexts = agent.getEnabledAppContexts();
126 ContextInfoSerializer.SerializedContexts contextInfo = ContextInfoSerializer.queryAndSerializeRequestedContexts(enabledContexts);
127
128 eventCount.incrementAndGet();
129
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 */
135 LttngJulApi.tracepointWithContext(formattedMessage,
136 record.getLoggerName(),
137 record.getSourceClassName(),
138 record.getSourceMethodName(),
139 record.getMillis(),
140 record.getLevel().intValue(),
141 record.getThreadID(),
142 contextInfo.getEntriesArray(),
143 contextInfo.getStringsArray());
144 }
145
146 }
This page took 0.031117 seconds and 3 git commands to generate.