启动嵌入式码头时指定系统 属性
Specifying system property when starting embedded jetty
我有一个具有 log4j 配置的 Web 应用程序。此配置在指定要放置日志文件的路径时使用系统 属性 catalina.home。
现在我正在准备集成 junit 测试套件,它加载一个 嵌入式码头 并加载这个网络应用程序(然后我使用休息电话等)。我的问题是,由于此 Web 应用程序现在是从码头加载的 catalina.home 系统 属性 未预先指定,因此日志文件将存储在根目录中windows C:/logs/...
我需要一种方法来指定要从码头使用的 catalina.home。最初我尝试使用:
System.setProperty("catalina.home", "target/jetty/tests/");
但这似乎被忽略了。
我无法弄清楚为什么上面的方法不起作用以及如何让它起作用。有人可以在这里提出建议吗?有没有人遇到同样的问题?
日志记录是一种特殊的野兽,它很早就初始化,通常是 JVM 在您的应用程序中最先初始化的东西之一。
您对 System.setProperty("catalina.home", "target/jetty/tests/");
的使用可能在此过程中为时已晚,因为 log4j 可能在您设置 属性 之前就已经初始化了。
为了取得最大成功,您需要检查以下内容...
- 您的测试用例不应声明静态 Logger 实例
- 您的
System.setProperty
应该出现在测试用例的 static { }
块中
- 出于测试原因,也许您可以
src/test/resources
替换(也称为覆盖)您的 log4j.properties
(或 log4j.xml
)文件,并在其中指定替代的日志记录目录路径.
Bonus: You should look for, and use ${project.basedir}
and ${basedir}
System Properties from the Gradle or Maven environment, and use them to make a fully qualified absolute path for your System.setProperty()
call, especially if you have a multi-module build, as the relative path will always be from ${user.dir}
(aka PWD) and that doesn't change with a normal build setup.
我有一个具有 log4j 配置的 Web 应用程序。此配置在指定要放置日志文件的路径时使用系统 属性 catalina.home。
现在我正在准备集成 junit 测试套件,它加载一个 嵌入式码头 并加载这个网络应用程序(然后我使用休息电话等)。我的问题是,由于此 Web 应用程序现在是从码头加载的 catalina.home 系统 属性 未预先指定,因此日志文件将存储在根目录中windows C:/logs/...
我需要一种方法来指定要从码头使用的 catalina.home。最初我尝试使用:
System.setProperty("catalina.home", "target/jetty/tests/");
但这似乎被忽略了。
我无法弄清楚为什么上面的方法不起作用以及如何让它起作用。有人可以在这里提出建议吗?有没有人遇到同样的问题?
日志记录是一种特殊的野兽,它很早就初始化,通常是 JVM 在您的应用程序中最先初始化的东西之一。
您对 System.setProperty("catalina.home", "target/jetty/tests/");
的使用可能在此过程中为时已晚,因为 log4j 可能在您设置 属性 之前就已经初始化了。
为了取得最大成功,您需要检查以下内容...
- 您的测试用例不应声明静态 Logger 实例
- 您的
System.setProperty
应该出现在测试用例的static { }
块中 - 出于测试原因,也许您可以
src/test/resources
替换(也称为覆盖)您的log4j.properties
(或log4j.xml
)文件,并在其中指定替代的日志记录目录路径.
Bonus: You should look for, and use
${project.basedir}
and${basedir}
System Properties from the Gradle or Maven environment, and use them to make a fully qualified absolute path for yourSystem.setProperty()
call, especially if you have a multi-module build, as the relative path will always be from${user.dir}
(aka PWD) and that doesn't change with a normal build setup.