92b76de2594446e4afc67a559c30f931b487cb8e
[lttng-ust.git] / liblttng-ust-java-agent / java / lttng-ust-agent-log4j / org / lttng / ust / agent / log4j / LttngLogAppender.java
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.Collection;
23 import java.util.Map;
24 import java.util.Map.Entry;
25 import java.util.concurrent.atomic.AtomicLong;
26
27 import org.apache.log4j.AppenderSkeleton;
28 import org.apache.log4j.spi.LoggingEvent;
29 import org.lttng.ust.agent.ILttngAgent;
30 import org.lttng.ust.agent.ILttngHandler;
31 import org.lttng.ust.agent.context.ContextInfoSerializer;
32
33 /**
34 * LTTng-UST Log4j 1.x log handler.
35 *
36 * Applications can attach this appender to their
37 * {@link org.apache.log4j.Logger} to have it generate UST events from logging
38 * events received through the logger.
39 *
40 * It sends its events to UST via the JNI library "liblttng-ust-log4j-jni.so".
41 * Make sure this library is available before using this appender.
42 *
43 * @author Alexandre Montplaisir
44 * @author Christian Babeux
45 */
46 public class LttngLogAppender extends AppenderSkeleton implements ILttngHandler {
47
48 private static final String SHARED_OBJECT_NAME = "lttng-ust-log4j-jni";
49
50 private final AtomicLong eventCount = new AtomicLong(0);
51
52 private final ILttngAgent<LttngLogAppender> agent;
53
54
55 /**
56 * Constructor
57 *
58 * @throws IOException
59 * This handler requires the lttng-ust-log4j-jni.so native
60 * library, through which it will send the trace events. This
61 * exception is throw is this library cannot be found.
62 * @throws SecurityException
63 * We will forward any SecurityExcepion that may be thrown when
64 * trying to load the JNI library.
65 */
66 public LttngLogAppender() throws IOException, SecurityException {
67 super();
68 /* Initialize LTTng UST tracer. */
69 try {
70 System.loadLibrary(SHARED_OBJECT_NAME); // $NON-NLS-1$
71 } catch (UnsatisfiedLinkError e) {
72 throw new IOException(e);
73 }
74
75 /** Register to the relevant agent */
76 agent = LttngLog4jAgent.getInstance();
77 agent.registerHandler(this);
78 }
79
80 @Override
81 public synchronized void close() {
82 agent.unregisterHandler(this);
83 }
84
85 /**
86 * Get the number of events logged by this handler so far. This means the
87 * number of events actually sent through JNI to UST.
88 *
89 * @return The number of events logged so far
90 */
91 @Override
92 public long getEventCount() {
93 return eventCount.get();
94 }
95
96 @Override
97 public boolean requiresLayout() {
98 return false;
99 }
100
101 @Override
102 protected void append(LoggingEvent event) {
103 /*
104 * Check if the current message should be logged, according to the UST
105 * session settings.
106 */
107 if (!agent.isEventEnabled(event.getLoggerName())) {
108 return;
109 }
110
111 /*
112 * The line number returned from LocationInformation is a string. At
113 * least try to convert to a proper int.
114 */
115 int line;
116 try {
117 String lineString = event.getLocationInformation().getLineNumber();
118 line = Integer.parseInt(lineString);
119 } catch (NumberFormatException n) {
120 line = -1;
121 }
122
123 /* Retrieve all the requested context information we can find */
124 Collection<Entry<String, Map<String, Integer>>> enabledContexts = agent.getEnabledAppContexts();
125 ContextInfoSerializer.SerializedContexts contextInfo = ContextInfoSerializer.queryAndSerializeRequestedContexts(enabledContexts);
126
127 eventCount.incrementAndGet();
128
129 LttngLog4jApi.tracepointWithContext(event.getRenderedMessage(),
130 event.getLoggerName(),
131 event.getLocationInformation().getClassName(),
132 event.getLocationInformation().getMethodName(),
133 event.getLocationInformation().getFileName(),
134 line,
135 event.getTimeStamp(),
136 event.getLevel().toInt(),
137 event.getThreadName(),
138 contextInfo.getEntriesArray(),
139 contextInfo.getStringsArray());
140 }
141
142 }
This page took 0.0311 seconds and 3 git commands to generate.