【问题标题】:log4j2 fails to write to filelog4j2 无法写入文件
【发布时间】:2017-10-19 21:36:47
【问题描述】:

理想情况下,我想在本地主机上记录开发过程中的所有内容,并且只在实时服务器上记录错误。我在我的开发平台(Windows 10、Java with Wicket)上登录文件时遇到问题。

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- https://logging.apache.org/log4j/2.x/articles.html
http://mycuteblog.com/log4j2-xml-configuration-example/ -->
<Configuration status="DEBUG">
    <Properties>
        <Property name="log-path">${sys:catalina.home}/logs</Property>
    </Properties>
    <Appenders>
        <Console name="console-log" target="SYSTEM_OUT">
            <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
        </Console>
        <RollingFile name="trace-log" fileName="${log-path}/luminous-trace.log"
            filePattern="${log-path}/luminous-trace-%d{yyyy-MM-dd}.log">
            <PatternLayout>
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
        </RollingFile>
        <RollingFile name="error-log" fileName="${log-path}/luminous-error.log"
            filePattern="${log-path}/luminous-error-%d{yyyy-MM-dd}.log">
            <PatternLayout>
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Logger name="com.linguaclassica" level="debug" additivity="false">
            <appender-ref ref="trace-log" level="debug"/>
            <appender-ref ref="error-log" level="error"/>
            <appender-ref ref="console-log" level="debug"/>
        </Logger>
        <Root level="info" additivity="false">
            <AppenderRef ref="console-log"/>
        </Root>
    </Loggers>
</Configuration>

我已经设法将一些日志写入我的 tomcat 的 logs 文件夹。

只显示了我的一些消息。我不知道哪些符号是重要的,哪些不是。

可公开访问的页面的基页面记录子类。

public class PublicBasePage extends WebPage
{
    protected static final Logger logger = LogManager.getLogger(PublicBasePage.class);

    public PublicBasePage()
    {
        super();

        logger.info(this.getClass().getName());
    }
}

[信息] 2017-10-19 17:07:13.208 [http-nio-8080-exec-57] PublicBasePage - com.linguaclassica.access.HomePage 私人网页的基本页面记录一条消息。

public class PrivateBasePage extends WebPage
{
    private static final Logger logger = LogManager.getLogger(PrivateBasePage.class);

    public PrivateBasePage()
    {
        super();

        logger.debug("()");
    }
}

[调试] 2017-10-19 17:12:05.662 [http-nio-8080-exec-62] PrivateBasePage - ()

但是,私人页面的消息不会记录到文件或控制台中。

public class ClientLandingPage extends PrivateBasePage
{
    private static final Logger logger = LogManager.getLogger(ClientLandingPage.class);

    public ClientLandingPage()
    {
        super();    

        logger.info("(info)");
        logger.debug("(debug)");
        logger.warn("(warn)");
    }
}

我不明白失败的原因。

【问题讨论】:

  • 当您的应用程序 log4j 加载其设置时,它会执行一些 sysout。你能检查它们并发布它们吗?
  • 在哪个包中,ClientLandingPage 和其他类存在?
  • ClientLandingPage 位于文件夹 com.linguaclassica.client 中。还有 3 个文件夹用于其他用户角色,另外 1 个文件夹用于共享角色。

标签: java wicket log4j2 tomcat8


【解决方案1】:

好吧,这很粗心。 ClientLandingPage 已过时并且从未被调用,被 shared/CommonOverviewPage 取代。我将声明更改为

private Logger logger = LogManager.getLogger(SomethingPage.class);

日志似乎按照我想要的方式显示出来。

昨天的日志在文件名后面附加了日期。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-12
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多