Skip to content

rage28/log4j2-slack

Repository files navigation

Log4j2 Slack Appender

This is a highly customizable appender for Apache SLF4J2 to push the logs to Slack using webhooks

Dependencies

Gradle

    dependencies {
        compile 'com.github.rage28:log4j2-slack:1.0'
    }

Maven

    <dependency>
        <groupId>com.github.rage28</groupId>
        <artifactId>log4j2-slack</artifactId>
        <version>1.0</version>
    </dependency>

Configuration

Example

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" packages="com.rage28.log4j2">
    <Appenders>
        <Slack name="ExampleSlacker"
                       channel="#updates"
                       webhookUrl="https://hooks.slack.com/services/X/Y/Z"
                       username="botter"
                       iconEmoji="robot_face">
            <SlackLogLayout />
        </Slack>
    </Appenders>

    <Loggers>
        <Root level="info">
            <AppenderRef ref="ExampleSlacker"/>
        </Root>
    </Loggers>
</Configuration>
import com.github.rage28.log4j2.slack.message.SlackLogMessage;
import com.github.rage28.log4j2.slack.model.SlackLog.SlackLogBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class MyClass {
	private static Logger logger = LogManager.get(MyClass.class);
	
	public static void main(String[] args) {
		logger.error(new SlackLogMessage(SlackLogBuilder.builder()
                     			.withTitle("This is a title")
                     			.withColor("red")
                     			.withText("Something bad happened! I can feel it")
                     			.build()));
	}
}