如果你在 Spring 引导,那么你可以直接 在 application.properties 中添加以下属性文件设置日志记录级别,
自定义日志模式并将日志存储在外部文件中。
这些是不同的日志记录级别及其从最小 < < max 的顺序。
OFF < < FATAL < < ERROR < < WARN < < INFO < < DEBUG < < TRACE < < ALL
# To set logs level as per your need.
logging.level.org.springframework = debug
logging.level.tech.hardik = trace
# To store logs to external file
# Here use strictly forward "/" slash for both Windows, Linux or any other os, otherwise, your logs it won't work.
logging.file=D:/spring_app_log_file.log
# To customize logging pattern.
logging.pattern.file= "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"
[edit] In other case (I always seem to bump into this problem), I was referencing a JAR file with classes stripped from a web application (WAR), which contained a logback.xml, not to mention AppInitializer - I suspect AppInitializer wouldn't be a problem, since it has a completely different package name and shouldn't be scanned by Spring auto config.. but logback.xml was being detected, I guess, in the classpath, and messed everything completely. I knew it was a hack to reference a WAR, but I was hoping for a quick fix - fixing that, breaking something else. Mani 的回答 is related.