为什么我的日志会被重定向到catalina.out?

时间:2014-06-03 07:17:49

标签: java tomcat7 log4j2 catalina.out

在我的应用程序中,我使用log4j2输出日志。

<!-- log4j2 -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.0-beta9</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.0-beta9</version>
</dependency>

应用程序托管在Tomcat7中。

首先我使用默认方式输出Catalina.out,但我发现我的应用程序日志将被重定向到Catalina.out。所以我按照Tomcat的文件。 http://tomcat.apache.org/tomcat-7.0-doc/logging.html

然后用log4j的方式改变它。但最后,它默认运行。

log4j.rootLogger = INFO, CATALINA

# Define all the appenders
log4j.appender.CATALINA = org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.File = ${catalina.base}/logs/catalina
log4j.appender.CATALINA.Append = true
log4j.appender.CATALINA.Encoding = UTF-8
# Roll-over the log once per day
log4j.appender.CATALINA.DatePattern = '.'yyyy-MM-dd'.log'
log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

log4j.appender.LOCALHOST = org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOCALHOST.File = ${catalina.base}/logs/localhost
log4j.appender.LOCALHOST.Append = true
log4j.appender.LOCALHOST.Encoding = UTF-8
log4j.appender.LOCALHOST.DatePattern = '.'yyyy-MM-dd'.log'
log4j.appender.LOCALHOST.layout = org.apache.log4j.PatternLayout
log4j.appender.LOCALHOST.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

log4j.appender.MANAGER = org.apache.log4j.DailyRollingFileAppender
log4j.appender.MANAGER.File = ${catalina.base}/logs/manager

任何人都遇到这样的问题,可以告诉我避免这种情况的解决方案吗?

PS: 记录器模块设计为:

import java.io.BufferedWriter;
import java.util.Date;

public class Logger {

    private static org.apache.logging.log4j.Logger logger = org.apache.logging.log4j.LogManager.getLogger(Logger.class
            .getName());

    public static void error(String proj, String opera, String param, String msg) {
        logger.error(log("Error", proj, opera, param, msg));
    }

    public static void debug(String proj, String opera, String param, String msg) {
        logger.debug(log("Debug", proj, opera, param, msg));
    }

    public static void warn(String proj, String opera, String param, String msg) {
        logger.warn(log("Warn", proj, opera, param, msg));
    }

    public static void info(String proj, String opera, String param, String msg) {
        logger.info(log("Info", proj, opera, param, msg));
    }

    private static String log(String type, String proj, String opera, String param, String msg) {
        // Build log content with JSON
        StringBuilder sb = new StringBuilder();
        sb.append("{\"time\": \"");
        sb.append(DateUtil.formatMilliSecond(new Date()));
        sb.append("\", \"level\": ");
        sb.append("\"" + type + "\", \"module\": ");
        sb.append("\"" + proj + "\", \"operation\": ");
        sb.append("\"" + opera + "\"");

        if (!StringUtil.isEmpty(param)) {
            sb.append(", \"parameter\": \"" + param + "\"");
        }

        if (!StringUtil.isEmpty(msg)) {
            sb.append(", \"message\": \"" + msg + "\"");
        }
        sb.append("}");
        writeLog(sb.toString());
        return sb.toString();
    }


    public static void setLog(String module) {
        logger = org.apache.logging.log4j.LogManager.getLogger(module);
    }

}

log4j2配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
    <appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%msg%n"/>
        </Console>
        <RollingFile name="MinerLog" fileName="/local/logs/miners.log"
                     filePattern="/local/logs/$${date:yyyy-MM}/miners-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout pattern="%msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="20 MB"/>
            </Policies>
        </RollingFile>
        <RollingFile name="CaptainLog" fileName="/local/logs/captain.log"
                     filePattern="/local/logs/$${date:yyyy-MM}/captain-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout pattern="%msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="20 MB"/>
            </Policies>
        </RollingFile>
        <RollingFile name="DataProviderLog" fileName="/local/logs/das_dataprovider.log"
                     filePattern="/local/logs/$${date:yyyy-MM}/dataprovider-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout pattern="%msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="20 MB"/>
            </Policies>
        </RollingFile>
    </appenders>
    <loggers>
        <root level="debug">
            <appender-ref ref="Console"/>
        </root>
        <logger name="miner" level="debug">
            <appender-ref ref="MinerLog" level="info"/>
            <appender-ref ref="Console"/>
        </logger>
        <logger name="captain" level="debug">
            <appender-ref ref="CaptainLog" level="info"/>
            <appender-ref ref="Console"/>
        </logger>
        <logger name="dataprovider" level="debug">
            <appender-ref ref="DataProviderLog" level="info"/>
            <appender-ref ref="Console"/>
        </logger>
    </loggers>
</configuration>

4 个答案:

答案 0 :(得分:0)

以下内容将始终指向默认日志文件Catalina.out。

log4j.appender.CATALINA.File = ${catalina.base}/logs/catalina

将此标记的值更改为本地文件

log4j.appender.CATALINA.File = C:\outputlog

答案 1 :(得分:0)

如果应用程序的某些部分使用log4j-1.2,则可以尝试将log4j-1.2-api-2.0.jar添加到类路径中,以便对1.2 api进行的所有调用都重定向到log4j2实现。 (参见&#34; log4j2常见问题解答页面中的哪个罐子和#34;部分。) 您将不再需要log4j-1.2样式属性文件配置。 log4j2将仅使用log4j2.xml配置。

答案 2 :(得分:0)

最后,我用:

更正了我的代码
import java.util.Date;

import org.apache.logging.log4j.core.config.XMLConfigurationFactory;


public class Logger {

    static {
        System.setProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "/local/logs/log4j2.xml");
    }

    private static org.apache.logging.log4j.Logger logger = org.apache.logging.log4j.LogManager.getLogger(Logger.class
            .getName());

    ...

然后可以在我的日志文件中生成正确的内容。 并且Tomcat也会在没有应用程序日志内容的情况下更正。  /local/logs/log4j2.xml现在看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
    <appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%msg%n"/>
        </Console>
        <RollingFile name="AppLog" fileName="/local/das/logs/das_app.log"
                     filePattern="/local/das/logs/$${date:yyyy-MM}/das_app-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout pattern="%msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="20 MB"/>
            </Policies>
        </RollingFile>        
    </appenders>
    <loggers>
        <root level="debug">
            <appender-ref ref="AppLog" level="info"/>
        </root>
    </loggers>
</configuration>

感谢vanOekel

答案 3 :(得分:0)

它被重定向到catalina.out,因为你将控制台日志定义为一个appender,这些被重定向到catalina.out。

但是,我们确实需要用于本地开发的控制台日志,但不希望它们在其他环境中显示。您可以使用ThresholdFilter和配置属性来仅使用以下示例代码在本地而不是在其他环境中控制日志:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" monitorInterval="300">
    <Properties>
        <Property name="consoleLogLevel">error</Property>
    </Properties>
    <Appenders>
        <RollingFile name="FileOut" fileName="/dev/pref-api.log"
                     filePattern="/dev/pref-api-%d{yyyy-MM-dd}.gz">
            <PatternLayout pattern="%date{yyyy/MM/dd HH:mm:ss.SSS} %-5level [%t] [%logger{36}] %msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
        </RollingFile>

        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%date{yyyy/MM/dd HH:mm:ss.SSS} %-5level [%t] [%logger{36}] %msg%n"/>
            <Filters>
                <ThresholdFilter level="${sys:consoleLogLevel}"  onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="warn" additivity="false">
            <AppenderRef ref="FileOut"/>
            <AppenderRef ref="Console"/>
        </Root>

        <Logger name="com.github.api" level="debug" additivity="false">
            <AppenderRef ref="FileOut"/>
            <AppenderRef ref="Console"/>
        </Logger>
    </Loggers>
</Configuration>

如果您在本地传递 -DconsoleLogLevel = debug ,您会看到来自 com.github.api 的所有日志都会记录到控制台,但在其他环境中如果您不记录# 39; t传递任何默认值的值,只显示catalina中的错误堆栈。