Skip to content
This repository was archived by the owner on Feb 5, 2026. It is now read-only.

Commit 2f180aa

Browse files
Ricardo Illgenjcolgan-r7
authored andcommitted
Add logback encoder support and use UTF-8 encoding explicitly (#11)
* Add logback encoder support and use UTF-8 encoding explicitly * Reformat code using spaces instead of tabs * Bump patch version * Using Charset constructor in order to avoid encoding check * Fix generics styling Co-Authored-By: James Colgan <30832432+jcolgan-r7@users.noreply.github.com>
1 parent cf13c29 commit 2f180aa

File tree

5 files changed

+33
-30
lines changed

5 files changed

+33
-30
lines changed

pom.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
<jackson.version>2.10.1</jackson.version>
99
<log4j.version>2.7</log4j.version>
1010
<disruptor.version>3.3.6</disruptor.version>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1112
</properties>
1213

1314
<modelVersion>4.0.0</modelVersion>
1415
<groupId>com.rapid7</groupId>
1516
<artifactId>r7insight_java</artifactId>
1617
<packaging>jar</packaging>
1718
<name>r7insight_java</name>
18-
<version>1.1.40</version>
19+
<version>1.1.41</version>
1920
<description>Contains logback, log4j, and log4j2 appenders that will send log data to Logentries</description>
2021
<url>https://github.com/rapid7/r7insight_java</url>
2122
<licenses>
@@ -45,6 +46,7 @@
4546
<configuration>
4647
<source>1.8</source>
4748
<target>1.8</target>
49+
<encoding>${project.build.sourceEncoding}</encoding>
4850
</configuration>
4951
</plugin>
5052
<plugin>
@@ -163,7 +165,7 @@
163165
<dependency>
164166
<groupId>ch.qos.logback</groupId>
165167
<artifactId>logback-classic</artifactId>
166-
<version>1.0.11</version>
168+
<version>1.2.3</version>
167169
<optional>true</optional>
168170
</dependency>
169171
<dependency>

src/main/java/com/rapid7/logback/LogentriesAppender.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package com.rapid7.logback;
22

33
import ch.qos.logback.classic.PatternLayout;
4-
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
54
import ch.qos.logback.classic.pattern.SyslogStartConverter;
65
import ch.qos.logback.classic.spi.ILoggingEvent;
76
import ch.qos.logback.core.AppenderBase;
87
import ch.qos.logback.core.Layout;
8+
import ch.qos.logback.core.encoder.Encoder;
9+
import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
910
import ch.qos.logback.core.net.SyslogConstants;
1011
import com.rapid7.net.AsyncLogger;
1112
import com.rapid7.net.LoggerConfiguration;
1213

14+
import static java.nio.charset.StandardCharsets.UTF_8;
15+
1316
/**
1417
* Logentries appender for logback.
1518
*
@@ -33,6 +36,8 @@ public class LogentriesAppender extends AppenderBase<ILoggingEvent> {
3336
* Layout
3437
*/
3538
private Layout<ILoggingEvent> layout;
39+
40+
private Encoder<ILoggingEvent> encoder;
3641
/**
3742
* Facility String
3843
*/
@@ -171,18 +176,22 @@ public void setLogID(String logID) {
171176
}
172177

173178
/**
174-
* Sets the suffixPattern to be the pattern field in the .xml configuration file
179+
* Sets the encoder for this appender
175180
*
176-
* @param encoder Logback Pattern Layout Encoder
181+
* @param encoder Logback Encoder
177182
*/
178-
public void setEncoder(PatternLayoutEncoder encoder) {
179-
this.suffixPattern = encoder.getPattern();
183+
public void setEncoder(Encoder<ILoggingEvent> encoder) {
184+
this.encoder = encoder;
180185
}
181186

182187
@Override
183188
public void start() {
184-
if (layout == null) {
185-
layout = buildLayout();
189+
if (encoder == null) {
190+
layout = layout == null ? buildLayout() : layout;
191+
LayoutWrappingEncoder<ILoggingEvent> lwe = new LayoutWrappingEncoder<>();
192+
lwe.setLayout(buildLayout());
193+
lwe.setContext(getContext());
194+
encoder = lwe;
186195
}
187196
this.iopsAsync = new AsyncLogger(configurationBuilder.build());
188197
super.start();
@@ -240,7 +249,10 @@ public void setLayout(Layout<ILoggingEvent> layout) {
240249
@Override
241250
protected void append(ILoggingEvent event) {
242251
// Render the event according to layout
243-
String formattedEvent = layout.doLayout(event);
252+
byte[] encodedEvent = encoder.encode(event);
253+
String formattedEvent;
254+
formattedEvent = new String(encodedEvent, UTF_8);
255+
244256
// Prepare to be queued
245257
this.iopsAsync.addLineToQueue(formattedEvent);
246258
}

src/test/java/com/rapid7/logback/LogentriesAppenderTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.rapid7.logback;
22

3+
import ch.qos.logback.classic.LoggerContext;
4+
import ch.qos.logback.core.Context;
35
import com.rapid7.util.SocketChannelReceiver;
46
import org.junit.Test;
57
import org.slf4j.Logger;
@@ -18,6 +20,8 @@ public class LogentriesAppenderTest {
1820
private static final boolean debug = true;
1921
private static final String hostname = "server1";
2022
private static final String logId = "LogId";
23+
private static final String facility = "kern";
24+
private static final Context context = new LoggerContext();
2125
private static final int port = 1000;
2226

2327
private static final String EMPTY_PREFIX = "";
@@ -37,6 +41,8 @@ public void setterTests() {
3741
le.setHostName(hostname);
3842
le.setLogHostName(true);
3943
le.setLogID(logId);
44+
le.setContext(context);
45+
le.setFacility(facility);
4046
le.start();
4147
assertEquals(le.iopsAsync.getToken(), token);
4248
assertEquals(le.iopsAsync.getRegion(), region);
@@ -70,5 +76,4 @@ public void testCreateAppenderAndLogMessage() throws Exception {
7076
}
7177
}
7278

73-
7479
}

src/test/java/com/rapid7/logback/LogentriesAppenderTest.java~

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/test/java/com/rapid7/util/SocketChannelReceiver.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import java.util.concurrent.BlockingQueue;
1616
import java.util.concurrent.TimeUnit;
1717

18+
import static java.nio.charset.StandardCharsets.UTF_8;
19+
1820
/**
1921
* Stub of InsightOPS server
2022
*/
@@ -105,7 +107,6 @@ private SSLContext initSSlContext() throws Exception {
105107
return sslContext;
106108
}
107109

108-
109110
private synchronized void waitUntilReady() throws InterruptedException {
110111
if (!isReady) {
111112
wait(5000);
@@ -119,7 +120,7 @@ private void listenForMessages(Socket socket) throws IOException {
119120
if (messageLength == -1) {
120121
continue;
121122
}
122-
String[] messages = new String(Arrays.copyOf(buffer, messageLength)).split("\\r?\\n");
123+
String[] messages = new String(Arrays.copyOf(buffer, messageLength), UTF_8).split("\\r?\\n");
123124
Collections.addAll(messagesReceived, messages);
124125
}
125126
}

0 commit comments

Comments
 (0)