One 、 sketch

This article talks about JAVA Use Logback Send logs to the console 、 file 、ELK The simplest use of .

Two 、 course

1、 newly build pom.xml The project introduces the following dependencies :

 <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、 stay resources Add under directory logback.xml file , Change the address to Logstash The address of , The contents are as follows :

<?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>127.0.0.1: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、 Write program to send log

package com.test.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.");
}
}

3、 ... and 、 effect

 

JAVA Use Logback Send logs to the console 、 file 、ELK More related articles on the simplest use of

  1. Java Use logback Log and save files

    explain : In general logback You can specify what level the class uses to display the output log , And the same class can not specify the level , Then output the log to the corresponding level . The first configuration : <?xml version="1.0&quo ...

  2. logback Write logs to different folders

    Reprint :logback Different business logs are printed to different files One .logback.xml The file configuration is as follows : <?xml version="1.0" encoding="UTF-8& ...

  3. spring-boot-route( sixteen ) Use logback Production log file

    Logging is a very important part of a system , We often need to look at the log to locate the problem , Let's study together today Spring Boot The log system of . There are many students used to use in the production code System.out To output logs , This is not recommended ...

  4. logback Set by day , File cut size , Total log file size .

    Set by day , File cut size , Total log file size . <?xml version="1.0" encoding="UTF-8"?> <configura ...

  5. 【spring boot logback】 journal logback Generate log file in this project , Not in the specified log file directory / After the specified log file reaches the maximum value, it will not be cut according to the configuration

    The original log file configuration is as follows : <?xml version="1.0" encoding="UTF-8"?> <configuration scan ...

  6. 【spring boot logback】 Log using custom logback-spring.xml After the document ,application.properties Will the configuration of logs still work

    This article Will target [ Log using custom logback-spring.xml After the document ,application.properties Will the configuration of logs still work ] This is a theme to explore . This test project is based on [spr ...

  7. Spring Boot Logback Application log

    e Spring Boot Logback Application log 2015-09-08 19:57 7673 Human reading Comment on (0) Collection report . classification : Spring Boot(51) . Catalog (?)[+] Log for ...

  8. springboot( Two ).springboot Integrate logback For log output

    springboot Integrate logback For log output The basic framework of our project has been completed ,http The request is already accessible , Now add the logging function to our framework and record the daily records to the file ad locum , We use logbac ...

  9. IDEA Project building 10 —— Use slf4j and logback Logging

    . brief introduction java The journal is divided into two parts, a facade . An implementation , We're familiar with SLF4j.Log4j.Log4j2.Logback Log component of slf4j It's a unified entrance provided by the facade , Specific implementation by log4j.log4j2.l ...

Random recommendation

  1. iis7 Compress js File and enable gzip Compress

    Compress js file open IIS 7 Configuration file for :c:\windows\system32\inetsrv\config\applicationhost.config stay <staticContent loc ...

  2. Outdoor physical penetration : Terminal , Client's web Test ideas

    Now the client interface is getting better and better , A lot of them use web technology , Light . Dazzling interface . Quick update , But this way web And then there are the shortcomings of , It's just instability , Easily influenced by users and other factors . Because a lot of clients web It's embedded , Internal communication , So a lot of safety tests ...

  3. eclipse Middle case conversion shortcut key

    ctrl+shift+x   Capitalize ctrl+shift+y   Change to lowercase

  4. python Basics —— Regular expressions

    Regular expressions Regular expressions for advanced text pattern matching . extract . And / Or text form of search and replace function provides the basis . To put it simply , Regular expressions are strings of characters and special symbols , They describe the repetition of patterns or the representation of multiple characters , So regular expressions ...

  5. TP frame Add delete check

    TP Framework adds data to database 1. Use array method to add modeling objects 2. Use AR The way The way strongly typed languages exist 3. Use the auto collection form to add : Only use POST The way , One operation method for submitting data implements two logics :A Display page B Get data ...

  6. C# Method extension

    using System.Collections.Generic; namespace Extra_Function { public static class Function { public s ...

  7. Jmeter( Nine )JDBC Connection pool

    JDBC by java Access to the database provides common API, It can provide unified access for a variety of relational databases . because SQL Is the standard language of relational database management system , As long as we follow SQL standard , So the code we write can access MySQL You can also visit SQL ...

  8. CentOS 7 Installation configuration Samba The server

    CentOS 7 Installation configuration Samba The server CentOS 7 Next Samba Notes on server installation and configuration process . Let's say we have a scenario Share name route jurisdiction SHAREDOC /smb/docs All personnel including guests can ...

  9. win7 Upgrade to win10, There is an arithmetic overflow problem

    The front desk winform, backstage java The code is : OutputStream ou=(OutputStream)response.getOutputStream(); ou.write(rightSet.get ...

  10. Linux File compression / pack / decompression

    stay Linux In daily maintenance , You often need to back up and synchronize some important files , In the process of transmission, if the file is large, it will be very slow , And it takes up a lot of space , At this time, we need to use compression tools to compress and package large files , Now let's introduce the commonly used pressure ...