예전에 혼자 만들던 프로젝트에 붙였던 로그.
그냥 나도 볼겸, 다른 사람도 쓸 사람 있으면 쓰라고 올림.
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.11</version>
</dependency>
<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="5 seconds">
<property name="DATE" value="%d{yyyy-MM-dd, Asia/Seoul}"/>
<property name="DATETIME" value="%d{yyyy-MM-dd HH:mm:ss z, Asia/Seoul}"/>
<property name="LOG_PATH" value="/var/log/project" />
<property name="CONSOLE_PATTERN" value="[${DATETIME}] [%-5level] : %msg%n" />
<property name="ACCESS_LOG_FILE_PATTERN" value="${CONSOLE_PATTERN}" />
<property name="DETAIL_LOG_FILE_PATTERN" value="${CONSOLE_PATTERN}" />
<appender name="detail-file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>info</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>${LOG_PATH}/detail.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}detail-${DATE}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${DETAIL_LOG_FILE_PATTERN}</pattern>
</encoder>
</appender>
<appender name="error-file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>error</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>${LOG_PATH}/error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/error-${DATE}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${DETAIL_LOG_FILE_PATTERN}</pattern>
</encoder>
</appender>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_PATTERN}</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="console" />
<appender-ref ref="error-file" />
<appender-ref ref="detail-file" />
</root>
</configuration>
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Log {
private static final Logger log = LoggerFactory.getLogger(Log.class);
public enum LogLevel {
Debug(0),
Info(1),
Warn(2),
Error(3),
Trace(4);
private int value;
LogLevel(int value) {
this.value = value;
}
}
public static void debug(String pattern, Object... args) {
log.debug(getErrorPattern(pattern), args);
}
public static void info(String pattern, Object... args){
log.info(getErrorPattern(pattern), args);
}
public static void warn(String pattern, Object... args) {
log.warn(getErrorPattern(pattern), args);
}
public static void error(String pattern, Object... args){
log.error(getErrorPattern(pattern), args);
}
public static void trace(String pattern, Object... args){
log.trace(getErrorPattern(pattern), args);
}
public static void write(LogLevel level, String pattern, Object... args){
switch (level){
case Debug: debug(pattern, args); break;
case Info: info(pattern, args); break;
case Warn: warn(pattern, args); break;
case Error: error(pattern, args); break;
case Trace:
default: trace(pattern, args); break;
}
}
private static String getClassName(){
return Thread.currentThread().getStackTrace()[2].getClassName();
}
private static String getMethodName() {
Thread.currentThread().getStackTrace()[2].getLineNumber();
return Thread.currentThread().getStackTrace()[2].getMethodName();
}
private static String getErrorPattern(String pattern) {
StackTraceElement[] stackList = new Throwable().getStackTrace();
if (stackList.length > 3) {
String head = stackList[2].getClassName() + "." + stackList[2].getMethodName() + "():" + stackList[2].getLineNumber();
pattern = head + ", " + pattern;
}
return pattern;
}
}
사용법
Log.info("County={} Result={}", finalCountyNameUpper, resultArray[1]);
'Java' 카테고리의 다른 글
자바 랜덤키 (0) | 2021.05.13 |
---|---|
자바 Json Parser (0) | 2021.02.22 |
자바 Aspect, Annotation으로 Log 만들기 (0) | 2021.02.22 |
자바 외부 dll 연동하기 opencv (0) | 2021.01.20 |
자바 Async3 (0) | 2020.11.08 |