我有 log4j2 在 Springboot 中工作,在 Mac 上运行良好。 log4j.properties 文件位于 /src/main/resources 文件夹中。
在 Mac 上,日志工作正常。然而,在 Windows 上,同一个 SpringBoot Tomcat 应用程序会引发记录器的 JNDI 错误。
""2024-02-18 06:56:52 [main] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/log4j2.ContextData]
""2024-02-18 06:56:52 [main] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/AsyncLogger.ThreadNameStrategy]
""2024-02-18 06:56:52 [main] DEBUG org.springframework.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/AsyncLogger.ThreadNameStrategy] not found - trying original name [AsyncLogger.ThreadNameStrategy]. javax.naming.NameNotFoundException: Name [AsyncLogger.ThreadNameStrategy] is not bound in this Context. Unable to find [AsyncLogger.ThreadNameStrategy].
""2024-02-18 06:56:52 [main] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [AsyncLogger.ThreadNameStrategy]
""2024-02-18 06:56:52 [main] DEBUG org.springframework.jndi.JndiPropertySource - JNDI lookup for name [AsyncLogger.ThreadNameStrategy] threw NamingException with message: Name [AsyncLogger.ThreadNameStrategy] is not bound in this Context. Unable to find [AsyncLogger.ThreadNameStrategy].. Returning null.
""2024-02-18 06:56:52 [main] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/log4j2.debug]
""2024-02-18 06:56:52 [main] DEBUG org.springframework.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/log4j2.debug] not found - trying original name [log4j2.debug]. javax.naming.NameNotFoundException: Name [log4j2.debug] is not bound in this Context. Unable to find [log4j2.debug].
""2024-02-18 06:56:52 [main] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [log4j2.debug]
""2024-02-18 06:56:52 [main] DEBUG org.springframework.jndi.JndiPropertySource - JNDI lookup for name [log4j2.debug] threw NamingException with message: Name [log4j2.debug] is not bound in this Context. Unable to find [log4j2.debug].. Returning null.
""2024-02-18 06:56:52 [main] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/log4j.Clock]
""2024-02-18 06:56:52 [main] DEBUG org.springframework.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/log4j.Clock] not found - trying original name [log4j.Clock]. javax.naming.NameNotFoundException: Name [log4j.Clock] is not bound in this Context. Unable to find [log4j.Clock].
""2024-02-18 06:56:52 [main] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [log4j.Clock]
""2024-02-18 06:56:52 [main] DEBUG org.springframework.jndi.JndiPropertySource - JNDI lookup for name [log4j.Clock] threw NamingException with message: Name [log4j.Clock] is not bound in this Context. Unable to find [log4j.Clock].. Returning null.
""2024-02-18 06:56:52 [main] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/log4j2.debug]
""2024-02-18 06:56:52 [main] DEBUG org.springframework.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/log4j2.debug] not found - trying original name [log4j2.debug]. javax.naming.NameNotFoundException: Name [log4j2.debug] is not bound in this Context. Unable to find [log4j2.debug].
""2024-02-18 06:56:52 [main] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [log4j2.debug]]
在 log4j2.properties 文件中,我读取日志目录的系统环境变量,如下所示:
property.basePath = ${sys:LOG_DIR}
同样,这种方法在 Mac 上运行良好。它还在 Windows 上创建日志文件本身(因此看起来它从 Windows 上的环境变量获取正确的日志目录),但随后在 Windows 上它会用数百个 JNDI 错误填充日志,就像我上面发布的那样。
我还尝试了 Windows 上日志目录的硬编码完整路径,但我在 Windows SpringBoot log4j2 日志中遇到了相同的数百个 JNDI 错误。
我还尝试按照某人的建议将 log4j2.properties 文件移动到应用程序中的 /src 文件夹。不幸的是这个想法没有实现。
如何使用确切的 Tomcat 和 WAR 文件在 Mac 上完美运行,但在 Windows 上却完全不行?
接下来我需要尝试什么?
谢谢,
我在 Windows 上遇到的所有不需要的日志消息的问题似乎是在 application.properties 中,我将日志级别设置为 DEBUG,而在 log4j2.properties 文件中我也将其设置为 DEBUG。 看来 application.properties 文件实际上是您配置 rootLogger 的位置。当我在 log4j2.properties 文件中将 DEBUG 更改为 INFO 后,问题就消失了。
由于此问题在 Mac 上的发生方式不同(启动时没有输入额外的日志消息),因此很难弄清楚。
感谢 Piotr 的出色帮助。