2 * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
3 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
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.
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
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
19 package org
.lttng
.ust
.agent
.jul
;
21 import java
.io
.IOException
;
22 import java
.util
.Collection
;
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
;
30 import org
.lttng
.ust
.agent
.ILttngAgent
;
31 import org
.lttng
.ust
.agent
.ILttngHandler
;
32 import org
.lttng
.ust
.agent
.context
.ContextInfoSerializer
;
35 * LTTng-UST JUL log handler.
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.
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.
44 * @author Alexandre Montplaisir
45 * @author David Goulet
47 public class LttngLogHandler
extends Handler
implements ILttngHandler
{
49 private static final String SHARED_OBJECT_NAME
= "lttng-ust-jul-jni";
52 * Dummy Formatter object, so we can use its
53 * {@link Formatter#formatMessage(LogRecord)} method.
55 private static final Formatter FORMATTER
= new Formatter() {
57 public String
format(LogRecord record
) {
58 throw new UnsupportedOperationException();
62 private final ILttngAgent
<LttngLogHandler
> agent
;
64 /** Number of events logged (really sent through JNI) by this handler */
65 private final AtomicLong eventCount
= new AtomicLong(0);
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.
78 public LttngLogHandler() throws IOException
, SecurityException
{
80 /* Initialize LTTng UST tracer. */
82 System
.loadLibrary(SHARED_OBJECT_NAME
); //$NON-NLS-1$
83 } catch (UnsatisfiedLinkError e
) {
84 throw new IOException(e
);
87 /** Register to the relevant agent */
88 agent
= LttngJulAgent
.getInstance();
89 agent
.registerHandler(this);
93 public synchronized void close() {
94 agent
.unregisterHandler(this);
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.
101 * @return The number of events logged so far
104 public long getEventCount() {
105 return eventCount
.get();
109 public void flush() {
113 public void publish(LogRecord record
) {
115 * Check if the current message should be logged, according to the UST
118 if (!agent
.isEventEnabled(record
.getLoggerName())) {
122 String formattedMessage
= FORMATTER
.formatMessage(record
);
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
);
128 eventCount
.incrementAndGet();
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.
135 LttngJulApi
.tracepointWithContext(formattedMessage
,
136 record
.getLoggerName(),
137 record
.getSourceClassName(),
138 record
.getSourceMethodName(),
140 record
.getLevel().intValue(),
141 record
.getThreadID(),
142 contextInfo
.getEntriesArray(),
143 contextInfo
.getStringsArray());