Skip to content

Commit 69563db

Browse files
committed
feat: implement dynamic log directory resolution for portable and standard modes
1 parent 7a6b43a commit 69563db

6 files changed

Lines changed: 52 additions & 7 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.laker.postman.logging;
2+
3+
import ch.qos.logback.core.PropertyDefinerBase;
4+
import com.laker.postman.util.AppRuntimeLayout;
5+
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
9+
/**
10+
* 根据运行形态解析日志目录,确保便携版日志写入程序所在目录下的 logs。
11+
*/
12+
public class LogHomePropertyDefiner extends PropertyDefinerBase {
13+
14+
@Override
15+
public String getPropertyValue() {
16+
Path logDir = AppRuntimeLayout.logRootDirectory(LogHomePropertyDefiner.class);
17+
try {
18+
Files.createDirectories(logDir);
19+
} catch (Exception ignored) {
20+
// 目录创建失败时交由 logback 在后续初始化阶段报告具体错误。
21+
}
22+
return logDir.toString();
23+
}
24+
}

easy-postman-app/src/main/resources/logback.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<configuration>
3-
<!-- 定义日志文件存储路径变量,使用用户主目录下的 EasyPostman/logs 文件夹 -->
4-
<!-- 应用程序标识和环境配置 -->
5-
<property name="APP_NAME" value="EasyPostman"/>
6-
<property name="LOG_HOME" value="${user.home}/${APP_NAME}/logs"/>
3+
<!-- 便携版写入应用根目录/logs,安装版写入用户主目录/EasyPostman/logs -->
4+
<define name="LOG_HOME" class="com.laker.postman.logging.LogHomePropertyDefiner"/>
75
<!-- 支持通过系统属性覆盖日志级别,默认为 INFO -->
86
<property name="LOG_LEVEL" value="${LOG_LEVEL:-INFO}"/>
97

easy-postman-app/src/test/java/com/laker/postman/service/js/PostmanBuiltinLibrariesTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public void restoreSettings() {
7272
@BeforeMethod
7373
public void setUp() {
7474
log.info("初始化 JavaScript 执行环境...");
75+
resetScriptSettingsToDefaults();
7576
context = Context.newBuilder("js")
7677
.allowHostAccess(HostAccess.ALL)
7778
.allowHostClassLookup(className -> false)
@@ -653,6 +654,15 @@ private void restoreScriptSettings() {
653654
SettingManager.setRemoteJsRequireMaxBytes(originalRemoteMaxBytes);
654655
}
655656

657+
private void resetScriptSettingsToDefaults() {
658+
SettingManager.setRemoteJsRequireEnabled(false);
659+
SettingManager.setInsecureRemoteJsRequireEnabled(false);
660+
SettingManager.setRemoteJsRequireAllowedHosts("");
661+
SettingManager.setRemoteJsRequireConnectTimeoutMs(3000);
662+
SettingManager.setRemoteJsRequireReadTimeoutMs(5000);
663+
SettingManager.setRemoteJsRequireMaxBytes(512 * 1024);
664+
}
665+
656666
private String toJsString(String raw) {
657667
return raw.replace("\\", "\\\\").replace("'", "\\'");
658668
}

easy-postman-app/src/test/java/com/laker/postman/service/update/asset/RuntimeModeConsistencyTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.laker.postman.service.update.asset;
22

33
import com.laker.postman.plugin.runtime.PluginRuntime;
4+
import com.laker.postman.util.AppRuntimeLayout;
45
import com.laker.postman.util.SystemUtil;
56
import org.testng.annotations.AfterMethod;
67
import org.testng.annotations.BeforeMethod;
@@ -43,6 +44,7 @@ public void shouldResolvePortableModeConsistentlyAcrossModules() throws Exceptio
4344

4445
assertTrue(WindowsVersionDetector.isPortableVersion());
4546
assertEquals(Paths.get(SystemUtil.getEasyPostmanPath()).normalize(), appDir.resolve("data"));
47+
assertEquals(AppRuntimeLayout.logRootDirectory(RuntimeModeConsistencyTest.class), appDir.resolve("logs"));
4648
assertEquals(PluginRuntime.getManagedPluginDir(), appDir.resolve("plugins"));
4749
assertEquals(PluginRuntime.getPluginPackageDir(), appDir.resolve("plugins").resolve("packages"));
4850
}
@@ -52,9 +54,11 @@ public void shouldTreatJarStyleLaunchAsInstalledModeByDefault() {
5254
System.setProperty("easyPostman.app.dir", appDir.toString());
5355

5456
Path expectedDataRoot = Paths.get(System.getProperty("user.home"), "EasyPostman").toAbsolutePath().normalize();
57+
Path expectedLogRoot = expectedDataRoot.resolve("logs");
5558

5659
assertFalse(WindowsVersionDetector.isPortableVersion());
5760
assertEquals(Paths.get(SystemUtil.getEasyPostmanPath()).normalize(), expectedDataRoot);
61+
assertEquals(AppRuntimeLayout.logRootDirectory(RuntimeModeConsistencyTest.class), expectedLogRoot);
5862
assertEquals(PluginRuntime.getManagedPluginDir(), expectedDataRoot.resolve("plugins").resolve("installed"));
5963
assertEquals(PluginRuntime.getPluginPackageDir(), expectedDataRoot.resolve("plugins").resolve("packages"));
6064
}

easy-postman-plugin-bridge/src/main/java/com/laker/postman/common/constants/ConfigPathConstants.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.laker.postman.common.constants;
22

33
import com.laker.postman.model.Workspace;
4+
import com.laker.postman.util.AppRuntimeLayout;
45
import com.laker.postman.util.SystemUtil;
56
import lombok.experimental.UtilityClass;
67

@@ -96,10 +97,11 @@ private static String getDataRootPath() {
9697

9798
/**
9899
* 日志目录
99-
* 使用用户主目录,与 logback.xml 配置保持一致
100+
* Portable 模式:程序所在目录/logs/
101+
* 普通模式:用户主目录/EasyPostman/logs/
100102
*/
101-
public static final String LOG_DIR = System.getProperty("user.home") + File.separator +
102-
AppConstants.APP_NAME + File.separator + "logs" + File.separator;
103+
public static final String LOG_DIR = AppRuntimeLayout.logRootDirectory(ConfigPathConstants.class).toString()
104+
+ File.separator;
103105

104106
// ==================== 工作区相关路径方法 ====================
105107

easy-postman-plugin-bridge/src/main/java/com/laker/postman/util/AppRuntimeLayout.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ public static Path dataRootDirectory(Class<?> anchorClass) {
7575
return Paths.get(System.getProperty("user.home"), "EasyPostman").toAbsolutePath().normalize();
7676
}
7777

78+
public static Path logRootDirectory(Class<?> anchorClass) {
79+
if (isPortableMode(anchorClass)) {
80+
return applicationRootDirectory(anchorClass).resolve("logs").toAbsolutePath().normalize();
81+
}
82+
return Paths.get(System.getProperty("user.home"), "EasyPostman", "logs").toAbsolutePath().normalize();
83+
}
84+
7885
private static boolean hasPortableMarker(Path directory) {
7986
if (directory == null) {
8087
return false;

0 commit comments

Comments
 (0)