Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用Spring MVC初始化记录器_Spring_Spring Mvc_Log4j - Fatal编程技术网

无法使用Spring MVC初始化记录器

无法使用Spring MVC初始化记录器,spring,spring-mvc,log4j,Spring,Spring Mvc,Log4j,这是我的bean类apploger.java public class AppLogger { private String logMessage; public String getLogMessage() { return logMessage; } public void setLogMessage(String logMessage) { this.logMessage = logMessage; } } 我

这是我的bean类apploger.java

public class AppLogger {

    private String logMessage;

    public String getLogMessage() {
        return logMessage;
    }

    public void setLogMessage(String logMessage) {
        this.logMessage = logMessage;
    }
}
我的log4j.properties

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\loging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Root logger option
log4j.rootLogger=debug, file, stdout
我的Log4j.xml

<bean id="appLogger" class="com.sort.model.AppLogger">
   <property name="message" value="Logger!"/>
   </bean>

My Web.xml

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/Log4J.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

log4jConfigLocation
/WEB-INF/Log4J.xml
org.springframework.web.util.Log4jConfigListener
我的控制器,在这里我试图记录一条示例消息

public class BasicFormController {

    private static final Logger logger = Logger.getLogger(BasicFormController.class);

    @ModelAttribute("evaluation")
    protected List<String> referenceData(HttpServletRequest request) throws Exception {

        logger.info("creating a message");
        List<String> evaluation = new ArrayList<String>();
        evaluation.add("Evaluated");
        evaluation.add("Not Evaluated");
        return evaluation;

    }
}
公共类基本控制器{
私有静态最终记录器Logger=Logger.getLogger(BasicFormController.class);
@模型属性(“评估”)
受保护列表引用数据(HttpServletRequest请求)引发异常{
logger.info(“创建消息”);
列表求值=新建ArrayList();
评估。添加(“评估”);
评估。添加(“未评估”);
回报评估;
}
}
myservlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- Enable annotation driven controllers, validation etc... -->
    <mvc:annotation-driven />
    <context:component-scan base-package="com.sort.controller" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value></value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    </bean>
    </beans>

.jsp
我的构建路径中有log4j-1.2.14.jar。 现在的问题是,我得到404错误。应用程序运行良好,没有所有这些Log4j相关问题。
你能帮我解决这个问题吗?

或者有人能给我介绍一个合适的例子,演示SpringMVC项目的日志记录吗?

我使用了xml方法并记录了代码流。谢谢你的回复

这是我使用的log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<log4j:configuration debug="true">

    <appender name="ROLL" class="org.apache.log4j.RollingFileAppender">
        <!-- The active file to log to -->
        <param name="file" value="D:/portal.log" />
        <param name="append" value="true" />
        <param name="encoding" value="UTF-8" />

        <rollingPolicy class="org.apache.log4j.TimeBasedRollingPolicy">
            <!-- The file to roll to, this is a fairly intelligent parameter, if the 
                file ends in .gz, it gzips it, based on the date stamp it rolls at that time, 
                default is yyyy-MM-dd, (rolls at midnight) See: http://logging.apache.org/log4j/companions/extras/apidocs/org/apache/log4j/rolling/TimeBasedRollingPolicy.html -->
            <param name="FileNamePattern" value="D:/portal.%d.log.gz" />
        </rollingPolicy>

        <layout class="org.apache.log4j.PatternLayout">
            <!-- The log message pattern -->
            <param name="ConversionPattern" value="%5p %d{ISO8601} [%t][%x] %c - %m%n" />
        </layout>
    </appender>

    <!-- Loggers to filter out various class paths -->

    <logger name="org.hibernate.engine.loading.LoadContexts"
        additivity="false">
        <level value="error" />
        <appender-ref ref="ROLL" />
    </logger>

    <!-- Debugging loggers -->

    <!-- Uncomment to enable debug on calpoly code only -->
    <!-- <logger name="edu.calpoly"> <level value="debug"/> <appender-ref ref="ROLL" 
        /> </logger> -->

    <root>
        <priority value="info" />
        <appender-ref ref="ROLL" />
    </root>

</log4j:configuration>


我删除了属性文件并放弃了该方法。

我使用了xml方法并记录了代码流。谢谢你的回复

这是我使用的log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<log4j:configuration debug="true">

    <appender name="ROLL" class="org.apache.log4j.RollingFileAppender">
        <!-- The active file to log to -->
        <param name="file" value="D:/portal.log" />
        <param name="append" value="true" />
        <param name="encoding" value="UTF-8" />

        <rollingPolicy class="org.apache.log4j.TimeBasedRollingPolicy">
            <!-- The file to roll to, this is a fairly intelligent parameter, if the 
                file ends in .gz, it gzips it, based on the date stamp it rolls at that time, 
                default is yyyy-MM-dd, (rolls at midnight) See: http://logging.apache.org/log4j/companions/extras/apidocs/org/apache/log4j/rolling/TimeBasedRollingPolicy.html -->
            <param name="FileNamePattern" value="D:/portal.%d.log.gz" />
        </rollingPolicy>

        <layout class="org.apache.log4j.PatternLayout">
            <!-- The log message pattern -->
            <param name="ConversionPattern" value="%5p %d{ISO8601} [%t][%x] %c - %m%n" />
        </layout>
    </appender>

    <!-- Loggers to filter out various class paths -->

    <logger name="org.hibernate.engine.loading.LoadContexts"
        additivity="false">
        <level value="error" />
        <appender-ref ref="ROLL" />
    </logger>

    <!-- Debugging loggers -->

    <!-- Uncomment to enable debug on calpoly code only -->
    <!-- <logger name="edu.calpoly"> <level value="debug"/> <appender-ref ref="ROLL" 
        /> </logger> -->

    <root>
        <priority value="info" />
        <appender-ref ref="ROLL" />
    </root>

</log4j:configuration>


我删除了属性文件并放弃了这种方法。

我看起来你的
log4jconfigolocation
应该是
/…/log4j.properties
至于你的
log4j.xml
我不明白这是什么-除了log4j配置文件之外,这是所有东西@BorisTreukhov感谢您的快速回复。我在我的web.xml
。/conf/log4j.properties
中尝试了这个,但仍然不起作用。什么不起作用?您有404错误这一事实与日志记录无关,而是DispatcherServlet/mapping/controller问题。你的应用程序启动了吗?Spring容器初始化了吗?当它被初始化时,你会得到任何bean初始化异常吗?@BorisTreukhov,正如我前面提到的,每当我添加新的东西,例如jar文件时,都会出现一些错误,而不是因为应用程序中存在其他错误。现在,我已经给了我的整个项目相关的记录器,我补充说。应用程序在启动时没有此选项,但没有此选项。无论如何,我已经发布了我的servlet.xml。@BorisTreukhov我看起来你的
log4jconfigolocation
应该是
/…/log4j.properties
至于你的
log4j.xml
我不明白这是什么-除了log4j配置文件之外,这就是一切@BorisTreukhov感谢您的快速回复。我在我的web.xml
。/conf/log4j.properties
中尝试了这个,但仍然不起作用。什么不起作用?您有404错误这一事实与日志记录无关,而是DispatcherServlet/mapping/controller问题。你的应用程序启动了吗?Spring容器初始化了吗?当它被初始化时,你会得到任何bean初始化异常吗?@BorisTreukhov,正如我前面提到的,每当我添加新的东西,例如jar文件时,都会出现一些错误,而不是因为应用程序中存在其他错误。现在,我已经给了我的整个项目相关的记录器,我补充说。应用程序在启动时没有此选项,但没有此选项。无论如何,我已经发布了我的servlet.xml。@BorisTreukhov