2 * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
3 * Copyright (C) 2014 - Christian Babeux <christian.babeux@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
.log4j
;
21 import java
.io
.IOException
;
22 import java
.util
.concurrent
.atomic
.AtomicLong
;
24 import org
.apache
.log4j
.AppenderSkeleton
;
25 import org
.apache
.log4j
.spi
.LoggingEvent
;
26 import org
.lttng
.ust
.agent
.ILttngAgent
;
27 import org
.lttng
.ust
.agent
.ILttngHandler
;
30 * LTTng-UST Log4j 1.x log handler.
32 * Applications can attach this appender to their
33 * {@link org.apache.log4j.Logger} to have it generate UST events from logging
34 * events received through the logger.
36 * It sends its events to UST via the JNI library "liblttng-ust-log4j-jni.so".
37 * Make sure this library is available before using this appender.
39 * @author Alexandre Montplaisir
40 * @author Christian Babeux
42 public class LttngLogAppender
extends AppenderSkeleton
implements ILttngHandler
{
44 private static final String SHARED_OBJECT_NAME
= "lttng-ust-log4j-jni";
46 private final AtomicLong eventCount
= new AtomicLong(0);
48 private final ILttngAgent
<LttngLogAppender
> agent
;
55 * This handler requires the lttng-ust-log4j-jni.so native
56 * library, through which it will send the trace events. This
57 * exception is throw is this library cannot be found.
58 * @throws SecurityException
59 * We will forward any SecurityExcepion that may be thrown when
60 * trying to load the JNI library.
62 public LttngLogAppender() throws IOException
, SecurityException
{
64 /* Initialize LTTng UST tracer. */
66 System
.loadLibrary(SHARED_OBJECT_NAME
); // $NON-NLS-1$
67 } catch (UnsatisfiedLinkError e
) {
68 throw new IOException(e
);
71 /** Register to the relevant agent */
72 agent
= LttngLog4jAgent
.getInstance();
73 agent
.registerHandler(this);
77 public synchronized void close() {
78 agent
.unregisterHandler(this);
82 * Get the number of events logged by this handler so far. This means the
83 * number of events actually sent through JNI to UST.
85 * @return The number of events logged so far
88 public long getEventCount() {
89 return eventCount
.get();
93 public boolean requiresLayout() {
98 protected void append(LoggingEvent event
) {
100 * Check if the current message should be logged, according to the UST
103 if (!agent
.isEventEnabled(event
.getLoggerName())) {
108 * The line number returned from LocationInformation is a string. At
109 * least try to convert to a proper int.
113 String lineString
= event
.getLocationInformation().getLineNumber();
114 line
= Integer
.parseInt(lineString
);
115 } catch (NumberFormatException n
) {
119 eventCount
.incrementAndGet();
121 LttngLog4jApi
.tracepoint(event
.getRenderedMessage(),
122 event
.getLoggerName(),
123 event
.getLocationInformation().getClassName(),
124 event
.getLocationInformation().getMethodName(),
125 event
.getLocationInformation().getFileName(),
127 event
.getTimeStamp(),
128 event
.getLevel().toInt(),
129 event
.getThreadName());