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
.concurrent
.atomic
.AtomicLong
;
23 import java
.util
.logging
.Formatter
;
24 import java
.util
.logging
.Handler
;
25 import java
.util
.logging
.LogRecord
;
27 import org
.lttng
.ust
.agent
.ILttngAgent
;
28 import org
.lttng
.ust
.agent
.ILttngHandler
;
31 * LTTng-UST JUL log handler.
33 * Applications can attach this handler to their
34 * {@link java.util.logging.Logger} to have it generate UST events from logging
35 * events received through the logger.
37 * It sends its events to UST via the JNI library "liblttng-ust-jul-jni.so".
38 * Make sure this library is available before using this handler.
40 * @author Alexandre Montplaisir
41 * @author David Goulet
43 public class LttngLogHandler
extends Handler
implements ILttngHandler
{
45 private static final String SHARED_OBJECT_NAME
= "lttng-ust-jul-jni";
48 * Dummy Formatter object, so we can use its
49 * {@link Formatter#formatMessage(LogRecord)} method.
51 private static final Formatter FORMATTER
= new Formatter() {
53 public String
format(LogRecord record
) {
54 throw new UnsupportedOperationException();
58 private final ILttngAgent
<LttngLogHandler
> agent
;
60 /** Number of events logged (really sent through JNI) by this handler */
61 private final AtomicLong eventCount
= new AtomicLong(0);
67 * This handler requires the lttng-ust-jul-jni.so native
68 * library, through which it will send the trace events. This
69 * exception is throw is this library cannot be found.
70 * @throws SecurityException
71 * We will forward any SecurityExcepion that may be thrown when
72 * trying to load the JNI library.
74 public LttngLogHandler() throws IOException
, SecurityException
{
76 /* Initialize LTTng UST tracer. */
78 System
.loadLibrary(SHARED_OBJECT_NAME
); //$NON-NLS-1$
79 } catch (UnsatisfiedLinkError e
) {
80 throw new IOException(e
);
83 /** Register to the relevant agent */
84 agent
= LttngJulAgent
.getInstance();
85 agent
.registerHandler(this);
89 public synchronized void close() {
90 agent
.unregisterHandler(this);
94 * Get the number of events logged by this handler so far. This means the
95 * number of events actually sent through JNI to UST.
97 * @return The number of events logged so far
100 public long getEventCount() {
101 return eventCount
.get();
105 public void flush() {
109 public void publish(LogRecord record
) {
111 * Check if the current message should be logged, according to the UST
114 if (!agent
.isEventEnabled(record
.getLoggerName())) {
118 String formattedMessage
= FORMATTER
.formatMessage(record
);
120 eventCount
.incrementAndGet();
122 * Specific tracepoint designed for JUL events. The source class of the
123 * caller is used for the event name, the raw message is taken, the
124 * loglevel of the record and the thread ID.
126 LttngJulApi
.tracepoint(formattedMessage
,
127 record
.getLoggerName(),
128 record
.getSourceClassName(),
129 record
.getSourceMethodName(),
131 record
.getLevel().intValue(),
132 record
.getThreadID());