Logstash Client for Java Virtual Machine
You should convert your server certificate into PKCS12 format.
$ openssl pkcs12 -export -inkey your_server.key -in your_server.crt -out server.p12
<dependency>
<groupId>com.wuriyanto</groupId>
<artifactId>jvm-stash</artifactId>
<version>1.0</version>
</dependency>
Stash stash = new Stash.Builder()
.setHost("localhost")
.setPort(5000)
.build();
try {
stash.connect();
} catch (StashException e) {
System.out.println(e.getMessage());
System.exit(1);
}
stash.write("hello logstash".getBytes())
try {
stash.close();
} catch (IOException e) {
System.out.println(e.getMessage());
System.exit(1);
}
Assumed you already enable ssl
config inside logstash.conf
input {
tcp {
port => 5000
ssl_enable => true
ssl_cert => "/etc/server.crt"
ssl_key => "/etc/server.key"
ssl_verify => false
}
}
Let's write some code again
InputStream keyStore = null;
try {
keyStore = new FileInputStream("/path/to/your/server.p12");
} catch (Exception e) {
System.out.println(e.getMessage());
System.exit(1);
}
Stash stash = new Stash.Builder()
.setHost("localhost")
.setPort(5000)
// makesure set to true
.setSecure(true)
.setKeyStoreIs(keyStore)
.setKeyStorePassword("damn12345")
.build();
try {
stash.connect();
} catch (StashException e) {
System.out.println(e.getMessage());
System.exit(1);
}
stash.write("hello logstash".getBytes())
try {
stash.close();
} catch (IOException e) {
System.out.println(e.getMessage());
System.exit(1);
}
private static final Logger LOGGER = Logger.getLogger(AppLogger.class.getName());
public static void main(String[] args) throws StashException {
InputStream keyStore = null;
try {
keyStore = new FileInputStream("/path/to/your/server.p12");
} catch (Exception e) {
System.out.println(e.getMessage());
System.exit(1);
}
Stash stash = new Stash.Builder()
.setHost("localhost")
.setPort(5000)
// makesure set to true
.setSecure(true)
.setKeyStoreIs(keyStore)
.setKeyStorePassword("damn12345")
.build();
try {
stash.connect();
} catch (StashException e) {
System.out.println(e.getMessage());
System.exit(1);
}
// Set Handler with StashLogHandler
LOGGER.addHandler(new StashLogHandler(stash));
// Use standar log that uses StashLogHandler
LOGGER.log(Level.INFO, "hello");
try {
stash.close();
} catch (IOException e) {
System.out.println(e.getMessage());
System.exit(1);
}