logback.xml 并从 JetBrains IDEA IDE 运行应用程序
我在IDEA中开发应用程序时,logback.xml应该放在哪里才能对应用程序产生影响?
当您运行/调试 IDEA 时,似乎不会生成任何 jar,也不会调用 Maven 来构建某些内容。它是否直接从编译的 *.class 文件执行 main() ?如果是这样,我应该把logback.xml放在哪里才能生效?
When I develop application in IDEA, where should I place logback.xml for it to have an effect on the application?
It seems when you run/debug IDEA doesn't make any jars and doesn't invoke Maven to build something. Does it execute main() directly from compiled *.class file? If so, where can I put logback.xml so that it would have effect?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
logback.xml
应该位于 CLASSPATH 的根目录中。当您运行应用程序时,完整的 CLASSPATH 会在最开始打印。当我将logback.xml
放入/src/main/resources
(Maven 项目)时,它可以正常工作。还将其以logback-test.xml
名称放入/src/test/resources
中也存在。只需运行:
并查看它是否返回某些内容或
null
。如果您不使用 Maven 项目,请打开项目结构 (Ctrl + Alt + Shift + S)并在模块部分中添加选择包含
logback.xml
的文件夹,并将其标记为源(蓝色图标)。logback.xml
should be available in the root directory of your CLASSPATH. When you run your application, the full CLASSPATH is printed at the very beginning. When I putlogback.xml
in/src/main/resources
(Maven project) it works without any problem. Also putting it in/src/test/resources
withlogback-test.xml
name has presence.Simply run:
And see whether it returns something or
null
.If you are not working with Maven project, open Project structure (Ctrl + Alt + Shift + S) and add in Modules section select folder containing
logback.xml
and mark it as Sources (blue icon).我需要重新构建项目(不仅仅是重新运行或重新编译项目)。更改 src\main\resources\logback.xml 不会影响 logback,因为类加载器正在选取
target/
文件夹中的 logback.xml,而不是/src
文件夹。这是有道理的,因为目标是运行环境。因此,需要进行构建才能将新的 xml 文件传输到目标。I needed to re-build the project (not just re-run or re-compile the project). Changing the
src\main\resources\logback.xml
did not affect logback because the class loader is picking up the logback.xml that is in thetarget/
folder, not the/src
folder. This makes sense since target is the run environment. Therefore, a build is required to transfer the new xml file over to target.