com.github.maricn.logback.SlackAppender Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of logback-slack-appender Show documentation
Show all versions of logback-slack-appender Show documentation
Logback appender for sending messages to Slack chats.
The newest version!
package com.github.maricn.logback;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Layout;
import ch.qos.logback.core.LayoutBase;
import ch.qos.logback.core.UnsynchronizedAppenderBase;
public class SlackAppender extends UnsynchronizedAppenderBase {
private final static String API_URL = "https://slack.com/api/chat.postMessage";
private final static ObjectMapper objectMapper = new ObjectMapper();
private static Layout defaultLayout = new LayoutBase() {
public String doLayout(ILoggingEvent event) {
return "-- [" + event.getLevel() + "]" +
event.getLoggerName() + " - " +
event.getFormattedMessage().replaceAll("\n", "\n\t");
}
};
private String webhookUri;
private String token;
private String channel;
private String username;
private String iconEmoji;
private String iconUrl;
private Boolean colorCoding = false;
private Layout layout = defaultLayout;
private int timeout = 30_000;
@Override
protected void append(final ILoggingEvent evt) {
try {
if (webhookUri != null && !webhookUri.isEmpty()) {
sendMessageWithWebhookUri(evt);
} else if (token != null && !token.isEmpty()) {
sendMessageWithToken(evt);
}
} catch (Exception ex) {
ex.printStackTrace();
addError("Error posting log to Slack.com (" + channel + "): " + evt, ex);
}
}
private void sendMessageWithWebhookUri(final ILoggingEvent evt) throws IOException {
String[] parts = layout.doLayout(evt).split("\n", 2);
Map message = new HashMap<>();
message.put("channel", channel);
message.put("username", username);
message.put("icon_emoji", iconEmoji);
message.put("icon_url", iconUrl);
message.put("text", parts[0]);
// Send the lines below the first line as an attachment.
if (parts.length > 1 && parts[1].length() > 0) {
Map attachment = new HashMap<>();
attachment.put("text", parts[1]);
if (colorCoding) {
attachment.put("color", colorByEvent(evt));
}
message.put("attachments", Collections.singletonList(attachment));
}
final byte[] bytes = objectMapper.writeValueAsBytes(message);
postMessage(webhookUri, "application/json", bytes);
}
private void sendMessageWithToken(final ILoggingEvent evt) throws IOException {
final StringWriter requestParams = new StringWriter();
requestParams.append("token=").append(token).append("&");
String[] parts = layout.doLayout(evt).split("\n", 2);
requestParams.append("text=").append(URLEncoder.encode(parts[0], "UTF-8")).append('&');
// Send the lines below the first line as an attachment.
if (parts.length > 1 && parts[1].length() > 0) {
Map attachment = new HashMap<>();
attachment.put("text", parts[1]);
if (colorCoding) {
attachment.put("color", colorByEvent(evt));
}
List