Skip to content

Commit

Permalink
Added GelfSender
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbodart committed Jan 16, 2017
1 parent bf358b5 commit 681cc69
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build/runtime.dependencies
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mvn://repo.bodar.com/com.googlecode.totallylazy:totallylazy:pack|sources:2.249
mvn://repo.bodar.com/com.googlecode.totallylazy:totallylazy:pack|sources:2.258
mvn://repo.bodar.com/com.googlecode.yadic:yadic:pack|sources:2.48
50 changes: 50 additions & 0 deletions src/com/googlecode/utterlyidle/gelf/GelfSender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.googlecode.utterlyidle.gelf;

import com.googlecode.totallylazy.LazyException;
import com.googlecode.totallylazy.Maps;
import com.googlecode.totallylazy.Option;
import com.googlecode.totallylazy.time.Clock;
import com.googlecode.totallylazy.time.Seconds;

import java.io.IOException;
import java.net.SocketAddress;
import java.nio.channels.DatagramChannel;
import java.util.Date;
import java.util.Map;

import static com.googlecode.totallylazy.Bytes.bytes;
import static com.googlecode.totallylazy.json.Json.json;
import static com.googlecode.totallylazy.security.GZip.gzip;
import static java.nio.ByteBuffer.wrap;

public interface GelfSender {
void send(String host, String shortMessage, Option<Date> timestamp, Severity severity, Map<String, Object> data);

static GelfSender udpGelfSender(SocketAddress socketAddress, Clock clock) throws IOException {
DatagramChannel channel = DatagramChannel.open().connect(socketAddress);
return (host, shortMessage, timestamp, severity, data) -> {
Date time = timestamp.getOrElse(clock.now());
try {
channel.write(wrap(gzip(bytes(json(gelf(host, shortMessage, severity, time, data))))));
} catch (IOException e) {
throw LazyException.lazyException(e);
}
};
}

static Map<String, Object> gelf(String host, String shortMessage, Severity severity, Date time, Map<String, Object> data) {
Map<String, Object> gelf = Maps.map(
"version", "1.1",
"host", host,
"short_message", shortMessage,
"level", severity.value(),
"timestamp", Seconds.sinceEpoch(time));
gelf.put("_data", data);
return gelf;
}

static GelfSender noOp() {
return (String host, String shortMessage, Option<Date> timestamp, Severity severity, Map<String, Object> data) -> {};
}

}
42 changes: 42 additions & 0 deletions src/com/googlecode/utterlyidle/gelf/Severity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.googlecode.utterlyidle.gelf;

import com.googlecode.totallylazy.Value;

import static com.googlecode.totallylazy.Sequences.sequence;

public enum Severity implements Value<Integer> {
Emergency("emerg"),
Alert("alert"),
Critical("crit"),
Error("err"),
Warning("warning"),
Notice("notice"),
Informational("info"),
Debug("debug");

private final String keyword;

Severity(String keyword) {
this.keyword = keyword;
}

public String keyword() {
return keyword;
}

@Override
public String toString() {
return super.toString().toLowerCase();
}

@Override
public Integer value() {
return ordinal();
}

public static Severity parse(String name){
return sequence(Severity.values()).
find(value -> value.name().equalsIgnoreCase(name) || value.keyword().equalsIgnoreCase(name)).
get();
}
}

0 comments on commit 681cc69

Please sign in to comment.