Intellij Idea 如何在调试模式下启动应用程序时将日志级别设置为“debug”(IntelliJ)

kokeuurv  于 2023-11-16  发布在  其他
关注(0)|答案(1)|浏览(294)

当启动应用程序时,通常只在控制台中打印log.info(...)。当在调试模式下启动应用程序时,打印log.info(...)和log.debug(...)。
我可以在main方法中设置调试级别而不阅读系统属性吗?

  1. @Slf4j
  2. public class Logging {
  3. public static void main(String[] args) {
  4. log.info("This message should be logged when running the app normally");
  5. log.debug("This message should be logged when running the app in debug mode");
  6. }
  7. }

字符串
配置文件(log4j2.xml):

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- 'status' defines the log level of the internal log4j events -->
  3. <Configuration status="warn">
  4. <Properties>
  5. <Property name="LOG_PATTERN">%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1} - %m%n</Property>
  6. </Properties>
  7. <Appenders>
  8. <Console name="console" target="SYSTEM_OUT" follow="true">
  9. <PatternLayout pattern="${LOG_PATTERN}"/>
  10. </Console>
  11. </Appenders>
  12. <Loggers>
  13. <Root level="info">
  14. <AppenderRef ref="console"/>
  15. </Root>
  16. </Loggers>
  17. </Configuration>

5jvtdoz2

5jvtdoz21#

应用程序不应该知道它们是否在调试中运行。相反,使用不同的命令行参数(对于Intellij:How do you input command line arguments in IntelliJ IDEA?),然后在代码中以编程方式设置日志级别(参见答案Programmatically change log level in Log4j2)。

相关问题