This is the first post. I think there are many points that cannot be reached, but thank you.
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
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>
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.
Recommended Posts