-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf358b5
commit 681cc69
Showing
3 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) -> {}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |