如何使用logback-webhook-appender监视远程JVM应用程序。

Overview

This tutorial is for anyone who is actively using logback in their JVM applications. For those who are not familiar with the framework, I strongly recommend checking out its documentation.

Out-of-the-box logback can send WARNING and ERRØR logging events via email. Sometimes receiving those events is the best way to monitor and debug remote applications. However, email as notifications channel isn't that attractive. That's why the SIP3 team wrote a handy logback extension that uses webhooks and makes it possible to send the same events to unlimited amount of channels including the most popular - Slack and Telegram.

In this tutorial we will build a simple Spring Boot application and configure it to start sending ERROR messages via 一世ncoming Webhook for Slack.

1. Creating Spring Boot Application

Open Spring Initializr project to create a dummy Spring Boot application:

Spring Initializr

让我们选择Kotlin,然后按生成按钮。

在此展示中,我们将在DemoApplication类中添加一些代码:

@SpringBootApplication
class DemoApplication {

    private val logger: Logger = LoggerFactory.getLogger(DemoApplication::class.java)

    @PostConstruct
    fun init() {
        try {
            throw RuntimeException("Houston, we have a problem!")
        } catch (e: Exception) {
            logger.error("Got exception...", e)
        }       
    }
}

fun main(args: Array<String>) {
    runApplication<DemoApplication>(*args)
}

如您所见,每次启动应用程序时,以上代码都会引发RuntimeException。

2. Adding logback-webhook-appender

Let's pull logback-webhook-appender and install it to a local Maven repository using mvn clean install.

现在,我们可以将其添加为项目的依赖项pom.xml(在某些情况下,您必须通过明确指定其版本来解决与okhttp3库的冲突):

<properties>
    ...
    <okhttp3.version>4.5.0</okhttp3.version>
</properties>

<dependencies>
    ...     
    <dependency>
        <groupId>io.sip3</groupId>
        <artifactId>logback-webhook-appender</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </dependency>   
    ...
</dependencies>

3. Configuring logback-webhook-appender

Prior to the next step we need to set up an Incoming Webhook for our Slack.

完成后,我们可以创建和配置src / main / resources / logback-spring.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="webhook" class="io.sip3.logback.WebhookAppender">
        <url>https://hooks.slack.com/services/%YOUR_WEBHOOK_URI%</url>     // (1)
        <json>                                                             // (2)
            {
                "text": ":fire: Houston, we have a problem! :fire:",
                "attachments": [
                    {
                        "text": "{message}"                                // (3)
                    }
                ]
            }
        </json>
        <encoder>
            <pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="ERROR">
        <appender-ref ref="webhook"/>
    </root>
</configuration>
  • (1)-使用<url>设置一个Webhook URL(2)-使用<json>设置有效负载模板(3)-使用{信息}作为编码日志事件的占位符

4. Testing logback-webhook-appender

到现在为止还挺好。 让我们开始使用mvn spring-boot:运行命令。 然后打开配置的Slack频道以接收错误通知:

Slack `ERROR` Notification

这就是您如何在2-5分钟内设置一个非常简单但有用的监视的方法。

我希望您喜欢这种方法,并将在您的项目中开始使用它。 如果您有任何疑问,请给我留言:)

干杯...

from: https://dev.to//agafox/how-to-monitor-remote-jvm-applications-with-logback-webhook-appender-2oc5

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值