About Java log output

Log façade library

The standard java.util.logging was created after log4j, but it wasn't completely popular because log4j was easy to use. Therefore, an interface that does not depend on the log library was required. If the log library is different for each library, it will be difficult to combine the logs into one file, so basically it is better to use SLF4J.

SLF4J(Simple Logging Facade For Java) It seems that there are many configurations of SLF4J + logback or SLF4J + Log4j2. It seems that it was created because Commons Logging is difficult to use, so SLF4J cannot be used If you don't have a reason, it seems good to use this.

Required JAR

API (required): slf4j-api-\ *. Jar Hinding to log implementation (one of them) Log4j:slf4j-log4j12-*.jar Log4j2:log4j-slf4j-impl-*.jar Logback:logback-classic-*.jar java.util.logging:slf4j-jdk14-*.jar

Adapter, bridge

jcl is Jakarta Commons Logging jul is java.util.logging Abbreviation for.

Reference site

https://www.nextdoorwith.info/wp/se/slf4j/

Commons Logging

JBoss Logging

Log output library

java.util.logging

Log4j Out of support. It is better to use log4j2.

Installation

<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

setting file

log4j.properties


### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %5p %c{1} - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=log/log4j.log
log4j.appender.file.Append=true
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %5p %c{1} - %m%n

log4j.rootLogger=debug, stdout, file

About Conversion Pattern -Class name: "% C". The package name can be omitted by using "% C {1}". -Method name% M -Process ID is not supported ・ Thread ID is not supported

Divide the log file into multiple files

log4j.properties



### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%C{1} %M %t %d %5p %c{1} - %m%n

### direct messages to file mylog.log ###
log4j.appender.file1=org.apache.log4j.FileAppender
log4j.appender.file1.File=log/log4j1.log
log4j.appender.file1.Append=true
log4j.appender.file1.layout=org.apache.log4j.PatternLayout
log4j.appender.file1.layout.ConversionPattern=%d %5p %c{1} - %m%n
### direct messages to file mylog.log ###
log4j.appender.file2=org.apache.log4j.FileAppender
log4j.appender.file2.File=log/log4j2.log
log4j.appender.file2.Append=true
log4j.appender.file2.layout=org.apache.log4j.PatternLayout
log4j.appender.file2.layout.ConversionPattern=%d %5p %c{1} - %m%n

#
log4j.logger.kero.domain=debug, file1
log4j.logger.kero.view=debug, file2
log4j.logger.kero.infrastructure=debug, file2
log4j.rootLogger=debug, stdout

Log4j2

Installation

		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-api</artifactId>
			<version>2.9.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>2.9.1</version>
		</dependency>

setting file

log4j2.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration>
<Configuration status="OFF">
	<Properties>
		<Property name="ptn">%d{yyyy-MM-dd HH:mm:ss.SSS} p=%-5p c=%c t=%t C=%C F=%F M=%M L=%L m=%m %n</Property>
	</Properties>
	<Appenders>
		<Console name="console" target="SYSTEM_OUT">
			<PatternLayout pattern="${ptn}" />
		</Console>
		<RollingFile name="file" fileName="log/log4j2.log"
			filePattern="log/%d{yyyyMM}/log4j2_%d{yyyyMMdd}_%i.log">
			<PatternLayout pattern="${ptn}" />
			<Policies>
				<OnStartupTriggeringPolicy />
				<SizeBasedTriggeringPolicy size="2 MB" />
				<TimeBasedTriggeringPolicy />
			</Policies>
			<DefaultRolloverStrategy max="10" />
		</RollingFile>
	</Appenders>
	<Loggers>
		<Root level="all">
			<AppenderRef ref="console" />
			<AppenderRef ref="file" />
		</Root>
	</Loggers>
</Configuration>

Source

package ml.kerotori.log4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Test1 {
	static Logger logger = LogManager.getLogger(Test1.class);

	public static void main(String[] args) {
		logger.trace("Test1 main method");
	}
}

Logback

Installation

	<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>

setting file

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/logback.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 HH:mm:ss.SSS} - %m%n</pattern>
        </encoder>
    </appender>

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

</configuration>

Source


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

public class Test2 {
	private static Logger log = LoggerFactory.getLogger("Test2");
	public static void main(String[] args) {
		 log.debug("Test2 main method");
	}

}

reference

A soft grasp of the history and current status of Java logging libraries (for beginners) https://qiita.com/nisshiee/items/c5388f1d472ec86295e0

Recommended Posts

About Java log output
Java standard log output sample
Log output to file in Java
[Java] output, variables
About Java interface
[Java] About Java 12 features
[Java] About arrays
Something about java
Where about java
About Java features
About Java threads
[Java] About interface
About Java class
About Java arrays
[Output] About each
About java inheritance
About interface, java interface
About List [Java]
About java var
About Java literals
About Java commands
Output about the method # 2
About Java functional interface
Java, about 2D arrays
About class division (Java)
Java formatted output [Notes]
About [Java] [StreamAPI] allMatch ()
About Java StringBuilder class
Try scraping about 30 lines in Java (CSV output)
[Java] About Singleton Class
About Java method binding
[Java] About anonymous classes
About method splitting (Java)
[Java] GlassFish 5 Troubleshooting Log
[Java Silver] About initialization
About Java Array List
About Java Polymorphism super ()
Request parameter log output sample Java & Spring MVC
About inheritance (Java Silver)
About Java String class
About Java access modifiers
About Java lambda expressions
About Java entry points
About Java 10 Docker support
Personal summary about Java
[Java] About enum type
All about Java programming
[Output] About the database
About java abstract class
Sample code for log output by Java + SLF4J + Logback
A note about Java GC
What I researched about Java 8
About an instance of java
What I researched about Java 6
Output about the method Part 1
[Gradle] About Java plug-in tasks
About Java variable declaration statements
What I researched about Java 9
[Java] About try-catch exception handling
About Java class loader types
[Java Silver] About equals method