logback config FileName 이 기본 파일 logback.xml이 아닌 경우, logback config 사용하는 방법.
1) LoggerContext 를 reset하고,
2) System.setProperty에 logback.configurationFile 값을 설정 후,
3) ContextInitializer.autoConfig() 를 이용해서, logback config 를 다시 설정하도록 한다.
1. Resoruce (class load) 에서 재설정.
package com.tistory.lunadaddy.test.logbackconfigloader;
import ch.qos.logback.classic.util.ContextInitializer;
import ch.qos.logback.classic.LoggerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import static java.util.concurrent.TimeUnit.SECONDS;
public class Main {
private static final Logger logger = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) throws InterruptedException {
String profile = System.getProperty("app.profile", "dev"); // 기본값은 "dev"로 설정
loadLogbackConfig(profile);
// 로그 테스트
logger.info("Application started with profile: {}", profile);
String checkProcess = "sample.jar";
while (true) {
boolean canExecute = checkRunningJarProcesses(checkProcess); // 해당 jar가 수행중일 때만 로직을 처리하도록 함.
if(canExecute) {
// do something...
logger.info("do something...");
} else {
logger.info("skip");
}
SECONDS.sleep(2);
}
}
private static boolean checkRunningJarProcesses(String checkProcess) {
boolean jarProcessFound = false;
if(checkProcess == null) {
return jarProcessFound;
}
checkProcess = checkProcess.trim();
// 환경설정. 1) JAVA_HOME 정의하고, 2) path에 %JAVA_HOME%\bin 추가할 것.
String command = "jps";
String line;
Process process = null;
try {
process = new ProcessBuilder(command).start();
logger.debug("Checking for running JAR processes...");
try(BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
while ((line = reader.readLine()) != null) {
// "java -jar"가 포함된 프로세스를 찾음
String[] columns = line.split(" "); // 1: pid, 2: process
if (columns.length > 1 && checkProcess.equalsIgnoreCase(columns[1])) {
logger.debug("Found JAR process: " + line);
jarProcessFound = true;
}
}
}
// int returnCode = process.waitFor();
// logger.debug("returnCode: {}", returnCode);
} catch (IOException e) {
logger.error(e.toString());
} finally {
if(process != null) {
process.destroy();
}
}
return jarProcessFound;
}
private static void loadLogbackConfig(String profile) {
String logbackConfigFile = String.format("logback-%s.xml", profile);
try {
System.setProperty(ContextInitializer.CONFIG_FILE_PROPERTY, logbackConfigFile);
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
context.reset();
ContextInitializer ci = new ContextInitializer(context);
ci.autoConfig();
} catch (Exception e) {
e.printStackTrace();
System.err.println("Failed to load logback configuration file: " + logbackConfigFile);
}
}
}
2. 외부파일에서 재설정
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.util.StatusPrinter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
public class LogbackConfigLoader {
private static final Logger logger = LoggerFactory.getLogger(LogbackConfigLoader.class);
public static void main(String[] args) {
String profile = System.getProperty("app.profile", "dev"); // 기본값은 "dev"로 설정
loadLogbackConfig(profile);
// 로그 테스트
logger.info("Application started with profile: {}", profile);
}
private static void loadLogbackConfig(String profile) {
String logbackConfigFile = String.format("logback-%s.xml", profile);
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
try {
File file = new File(logbackConfigFile);
if (file.exists()) {
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(context);
context.reset();
configurator.doConfigure(file);
StatusPrinter.printInCaseOfErrorsOrWarnings(context);
} else {
logger.error("Logback configuration file {} does not exist.", logbackConfigFile);
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("Failed to load logback configuration file: " + logbackConfigFile);
}
}
}
3. pom.xml
shade에서 finalName에서 version 제외한 artifactId.jar 로 파일명을 재설정한다.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tistory.lunadaddy.test</groupId>
<artifactId>logback-config-loader</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<description>Simple standalone Java Applications</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- SLF4J, logback -->
<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.2.3</version>
</dependency>
</dependencies>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<!-- Compiler plugin enforces Java 1.7 compatibility and activates annotation processors -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${project.artifactId}</finalName>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.tistory.lunadaddy.test.logbackconfigloader.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
4. logback-dev.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %-5level %logger{36} - %msg%n</Pattern>
</encoder>
</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/logback-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<Pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %-5level %logger{36} - %msg%n</Pattern>
</encoder>
</appender>
<logger name="org.springframework" level="info"/>
<logger name="kr.or.connect" level="debug"/>
<root level="debug">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
</configuration>
end.
728x90
'Programming Language > Java' 카테고리의 다른 글
cmd script - jar application 이 미실행 중일 때만, start 수행 (0) | 2024.11.11 |
---|---|
java application - start cmd / stop cmd (1) | 2024.11.09 |
G1 GC에서는 Full GC는 잘 안일어난다 (0) | 2023.08.05 |
csv 파일 생성 예제. (추가 라이브러리 없이) (0) | 2023.07.27 |
Spring Webflux timout 설정 (0) | 2023.07.19 |