| 1 | /* |
| 2 | * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com> |
| 3 | * Copyright (C) 2014 - Christian Babeux <christian.babeux@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.log4j; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.util.concurrent.atomic.AtomicLong; |
| 23 | |
| 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; |
| 28 | |
| 29 | /** |
| 30 | * LTTng-UST Log4j 1.x log handler. |
| 31 | * |
| 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. |
| 35 | * |
| 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. |
| 38 | * |
| 39 | * @author Alexandre Montplaisir |
| 40 | * @author Christian Babeux |
| 41 | */ |
| 42 | public class LttngLogAppender extends AppenderSkeleton implements ILttngHandler { |
| 43 | |
| 44 | private static final String SHARED_OBJECT_NAME = "lttng-ust-log4j-jni"; |
| 45 | |
| 46 | private final AtomicLong eventCount = new AtomicLong(0); |
| 47 | |
| 48 | private final ILttngAgent<LttngLogAppender> agent; |
| 49 | |
| 50 | |
| 51 | /** |
| 52 | * Constructor |
| 53 | * |
| 54 | * @throws IOException |
| 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. |
| 61 | */ |
| 62 | public LttngLogAppender() throws IOException, SecurityException { |
| 63 | super(); |
| 64 | /* Initialize LTTng UST tracer. */ |
| 65 | try { |
| 66 | System.loadLibrary(SHARED_OBJECT_NAME); // $NON-NLS-1$ |
| 67 | } catch (UnsatisfiedLinkError e) { |
| 68 | throw new IOException(e); |
| 69 | } |
| 70 | |
| 71 | /** Register to the relevant agent */ |
| 72 | agent = LttngLog4jAgent.getInstance(); |
| 73 | agent.registerHandler(this); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public synchronized void close() { |
| 78 | agent.unregisterHandler(this); |
| 79 | } |
| 80 | |
| 81 | /** |
| 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. |
| 84 | * |
| 85 | * @return The number of events logged so far |
| 86 | */ |
| 87 | @Override |
| 88 | public long getEventCount() { |
| 89 | return eventCount.get(); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public boolean requiresLayout() { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | protected void append(LoggingEvent event) { |
| 99 | /* |
| 100 | * Check if the current message should be logged, according to the UST |
| 101 | * session settings. |
| 102 | */ |
| 103 | if (!agent.isEventEnabled(event.getLoggerName())) { |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * The line number returned from LocationInformation is a string. At |
| 109 | * least try to convert to a proper int. |
| 110 | */ |
| 111 | int line; |
| 112 | try { |
| 113 | String lineString = event.getLocationInformation().getLineNumber(); |
| 114 | line = Integer.parseInt(lineString); |
| 115 | } catch (NumberFormatException n) { |
| 116 | line = -1; |
| 117 | } |
| 118 | |
| 119 | eventCount.incrementAndGet(); |
| 120 | |
| 121 | LttngLog4jApi.tracepoint(event.getRenderedMessage(), |
| 122 | event.getLoggerName(), |
| 123 | event.getLocationInformation().getClassName(), |
| 124 | event.getLocationInformation().getMethodName(), |
| 125 | event.getLocationInformation().getFileName(), |
| 126 | line, |
| 127 | event.getTimeStamp(), |
| 128 | event.getLevel().toInt(), |
| 129 | event.getThreadName()); |
| 130 | } |
| 131 | |
| 132 | } |