[JAVA] Output log to external file with slf4j + logback with Maven

This is the first post. I think there are many points that cannot be reached, but thank you.

I want to output the log to a file

I haven't introduced slf4j properly, so I took this opportunity to try it. goal --Output the log to the Eclipse console and log file. --The APIs used are slf4j and logback

Development environment

Required API --slf4j-api: Output log when jar is executed --logback-classic: Output log to file --logback-core: Output log to file

Other necessary items

definition of logback.xml

For the definition of logback.xml, I referred to the site that is summarized in quite detail. I hope you can refer to it. Logback usage memo

By the way, I defined it as follows.

logback.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE logback>
<configuration>
	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
	    <encoder>
	        <pattern>%date{yyyy-MM-dd HH:mm:ss} [%thread] %level %logger{0} - %msg \(%file:%line\)%n</pattern>
	    </encoder>
	    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
	        <level>DEBUG</level>
	    </filter>
	</appender>

	<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
	    <file>logs/app.log</file>
	    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
	        <fileNamePattern>logs/app.%d{yyyy-MM-dd}.log.tar.gz</fileNamePattern>
	        <maxHistory>7</maxHistory>
	    </rollingPolicy>
	    <encoder>
	        <pattern>%d{yyyy-MM-dd'T'HH:mm:ss'Z'} - %m%n</pattern>
	    </encoder>
	</appender>

	<root level="DEBUG" additivity="false">
	    <appender-ref ref="STDOUT" />
	    <appender-ref ref="FILE" />
	</root>

</configuration>

definition of pom.xml

I tried to define the required API in maven dependency (this is wrong).

pom.xml


<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency>

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.1.3</version>
</dependency>

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.1.3</version>
</dependency>

So, write the following source and execute it.

main.java


public class App
{
    private static Logger log = LoggerFactory.getLogger("test");
    public static void main( String[] args )
    {
    	log.debug("Log output test");
    	log.info("Log output test");
    	log.warn("Log output test");
    	log.error("Log output test");
    }
}

Output to console log!

console.log



2019-03-03 14:56:28testtest [main] DEBUG test -Log output test(App.java:19)
2019-03-03 14:56:28testtest [main] INFO test -Log output test(App.java:20)
2019-03-03 14:56:28testtest [main] WARN test -Log output test(App.java:21)
2019-03-03 14:56:28testtest [main] ERROR test -Log output test(App.java:22)

Output to log file!

app.log


2019-03-03T14:56:28Z -Log output test
2019-03-03T14:56:28Z -Log output test
2019-03-03T14:56:28Z -Log output test
2019-03-03T14:56:28Z -Log output test

It was output safely.

By the way, if the following error occurs, the API may be booked.

SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#noProviders for further details.

It happened when I wrote this article, but initially I thought that logback-classic and logback-core were included in slf4j-api. However, what was actually included was spring-boot-starter, and logback-classic and logback-core were really required.

Reference site

Logback usage memo

Recommended Posts

Output log to external file with slf4j + logback with Maven
Log output to file in Java
Output system time to MANIFEST.MF with Maven
Added slf4J + logback to Eclipse Maven project
Sample code for log output by Java + SLF4J + Logback
csv file output with opencsv
Output javadoc to word file
Control log output with Doma2
Output XML tree to file
Output embedded Tomcat access log to standard output with Spring Boot
I want to hook log file generation / open with log4j # FileAppender
How to make a jar file with no dependencies in Maven
Put the date (e.g. yyyy-MM-dd) in the log file output by logback
Enable log output to both file and console using log4j in Eclipse.
How to output Jetty log to any directory
Output HTTP header of google-http-client to log
How to achieve file download with Feign
How to set JAVA_HOME with Maven appassembler-maven-plugin
How to achieve file upload with Feign
Let's write Java file input / output with NIO
[Apache Camel] Easy output of throughput to log
Add jar file obtained from Maven to IntelliJ
Mask confidential information in log messages with Logback
Replaced Tomcat 8.5 log output with Log4j2.8 or later
[Spring Batch] Output table data to CSV file