[Java] logback slf4j sample

Thing you want to do

I just want to easily try slf4j on the console ~~ However, all the information to be examined seems to be long and long. ~~

then, let's begin.

(1) Get logback

http://logback.qos.ch/download.html Get logback-1.2.3.tar.gz.

Find out, create ./lib/ and store it there

(2) Create Test.java

Test.java


   import org.slf4j.LoggerFactory;
   import org.slf4j.Logger;
   public class Test {
     public static void main(String[] args){
       Logger logger = LoggerFactory.getLogger("Test");
       logger.info("info: {}", "information");
       logger.warn("warn: {}", "warning");
       logger.error("error: {}", "error");
     }
   }

Current directory structure at this point

$ tree
.
|-- Test.java
`-- lib
    |-- logback-classic-1.2.3.jar
    |-- logback-core-1.2.3.jar
    `-- slf4j-api-1.7.25.jar

(3) Build

$ javac -cp .:./lib/logback-core-1.2.3.jar:./lib/logback-classic-1.2.3.jar:./lib/slf4j-api-1.7.25.jar Test.java

(4) Execution

$ java -cp .:./lib/logback-core-1.2.3.jar:./lib/logback-classic-1.2.3.jar:./lib/slf4j-api-1.7.25.jar Test
14:46:17.371 [main] INFO Test - info:information
14:46:17.375 [main] WARN Test - warn:warning
14:46:17.376 [main] ERROR Test - error:error

Output to file

Prepare logback.xml.

$ tree
.
|-- Test.java
|-- lib
|   |-- logback-classic-1.2.3.jar
|   |-- logback-core-1.2.3.jar
|   `-- slf4j-api-1.7.25.jar
`-- logback.xml ★ this

The contents of logback.xml. For the time being, this will come out.

<configuration>
  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>test.log</file>
    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="DEBUG">
    <appender-ref ref="FILE" />
  </root>
</configuration>

Perform steps (3) and (4)

Recommended Posts

[Java] logback slf4j sample
logback sample
Sample code for log output by Java + SLF4J + Logback
[Java] Generics sample
Java sample code 02
Java sample code 03
Selenium sample (Java)
Java GUI sample
Java sample code 04
Java sample code 01
[Java] Holiday judgment sample
Digital signature sample code (JAVA)
Java parallelization code sample collection
Java standard log output sample
Selenium Sample Reservation Form (Java)
Java 9 new features and sample code
A simple sample callback in Java
Java
BloomFilter description and implementation sample (JAVA)
[Java] Date period duplication check sample
EXCEL file update sample with JAVA
Java
Sample vending machine made in Java
Sample code using Minio from Java
A standalone Java app that sends logs to CloudWatch Logs with slf4j / logback
Added slf4J + logback to Eclipse Maven project
Sample code collection for Azure Java development
Sample vending machine made in Java (classification)
Sample to unzip gz file in Java