码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA使用Logback发送日志到控制台、文件、ELK的最简单用法

时间:2019-01-08 17:19:44      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:color   policy   bsp   field   debug   code   connect   htc   EAP   

一、简述

  本文讲JAVA使用Logback发送日志到控制台、文件、ELK的最简单用法。

 

二、教程

  1、新建pom.xml项目引入下列依赖:

    <dependencies>
        <!--Begin LogBack Log-->
        <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <!--Logback to elk-->
        <!-- https://mvnrepository.com/artifact/net.logstash.logback/logstash-logback-encoder -->
        <dependency>
            <groupId>net.logstash.logback</groupId>
            <artifactId>logstash-logback-encoder</artifactId>
            <version>5.2</version>
        </dependency>
        <!--End LogBack Log-->
    </dependencies>

 

  2、在resources目录下添加logback.xml文件,将地址换成Logstash的地址,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
    <appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="File" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>log/app.%d{yyyy-MM-dd}.log.tar.gz</fileNamePattern>
            <maxHistory>7</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="LogStash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
        <destination>172.16.4.141:5044</destination>
        <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder">
            <customFields>{"appName":"myapp-service"}</customFields>
        </encoder>
        <connectionStrategy>
            <roundRobin>
                <connectionTTL>5 minutes</connectionTTL>
            </roundRobin>
        </connectionStrategy>
    </appender>
    <root level="INFO">
        <appender-ref ref="File"/>
        <appender-ref ref="Console"/>
        <appender-ref ref="LogStash"/>
    </root>
</configuration>

 

  2、写程序发送日志

package com.yungoal.elktest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Application {
    private static final Logger log = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        log.info("welcome to song xing zhu blogs.");
    }
}

 

三、效果

技术分享图片

 

JAVA使用Logback发送日志到控制台、文件、ELK的最简单用法

标签:color   policy   bsp   field   debug   code   connect   htc   EAP   

原文地址:https://www.cnblogs.com/songxingzhu/p/10239881.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!