Skip to content

Instantly share code, notes, and snippets.

@sndyuk
Created January 14, 2012 19:34
Show Gist options
  • Save sndyuk/1612622 to your computer and use it in GitHub Desktop.
Save sndyuk/1612622 to your computer and use it in GitHub Desktop.
Logback appender with fluent-logger-java
package com.example;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import org.fluentd.logger.FluentLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.core.UnsynchronizedAppenderBase;
public class FluentLogbackAppender<E> extends UnsynchronizedAppenderBase<E> {
private static final Logger LOG = LoggerFactory.getLogger(FluentLogbackAppender.class);
private static final ExecutorService THREAD_POOL = Executors.newFixedThreadPool(1);
private static final class DaemonAppender<E> implements Runnable {
private final FluentLogger fluentLogger;
private final BlockingQueue<E> queue;
private final String label;
DaemonAppender(String tag, String label, String remoteHost, int port, int maxQueueSize) {
this.fluentLogger = FluentLogger.getLogger(tag, remoteHost, port);
this.queue = new LinkedBlockingQueue<E>(maxQueueSize);
this.label = label;
}
void log(E eventObject) {
if (!queue.offer(eventObject)) {
LOG.debug("Message queue is full. Ignore the message.");
}
}
@Override
public void run() {
try {
for (;;) {
Map<String, Object> data = new HashMap<String, Object>();
data.put("msg", queue.take());
fluentLogger.log(label, data);
}
} catch (InterruptedException e) {
LOG.error("Unknown error.", e);
close();
}
}
void close() {
FluentLogger.close();
queue.clear();
}
}
private DaemonAppender<E> appender;
private String tag;
private String label;
private String remoteHost;
private int port;
// リモートホストに接続できないときに何件までログを保持するか(件数制限に達している時にきたログは破棄する)
private int maxQueueSize;
@Override
public void start() {
super.start();
appender = new DaemonAppender<E>(tag, label, remoteHost, port, maxQueueSize);
THREAD_POOL.execute(appender);
}
@Override
protected void append(E eventObject) {
appender.log(eventObject);
}
@Override
public void stop() {
super.stop();
appender.close();
THREAD_POOL.shutdownNow();
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public int getMaxQueueSize() {
return maxQueueSize;
}
public void setMaxQueueSize(int maxQueueSize) {
this.maxQueueSize = maxQueueSize;
}
public String getRemoteHost() {
return remoteHost;
}
public void setRemoteHost(String remoteHost) {
this.remoteHost = remoteHost;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE logback>
<included>
<appender name="FLUENT" class="com.example.FluentLogbackAppender">
<tag>app</tag>
<label>nomal</label>
<remoteHost>example.com</remoteHost>
<port>24224</port>
<maxQueueSize>20</maxQueueSize>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{HH:mm:ss.SSS} [%thread] %-5level %logger{15}#%line --- %msg %n</pattern>
</encoder>
</appender>
</included>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE logback>
<configuration>
<include resource="logback-appenders.xml" />
<root>
<level value="info" />
<appender-ref ref="STDOUT" />
<appender-ref ref="FLUENT" />
</root>
</configuration>
@sndyuk
Copy link
Author

sndyuk commented Jul 20, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment