zoukankan      html  css  js  c++  java
  • spring mvc: log4j插件 log日志的输出

    准备:

    log插件:log4j

    <!-- log日志插件 -->
    	<!-- https://mvnrepository.com/artifact/log4j/log4j -->
    	<dependency>
    	    <groupId>log4j</groupId>
    	    <artifactId>log4j</artifactId>
    	    <version>1.2.17</version>
    	</dependency>
    

      

    访问地址:http://localhost:8080/gugua3/hello/log

    包:json

    需要在/WEB-INF/classes/文件夹下,新建log4j的配置

    log4j.properties

    # Root logger option
    log4j.rootLogger=DEBUG, stdout, file
    
    # Redirect log messages to console
    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{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
    
    # Redirect log messages to a log file
    log4j.appender.file=org.apache.log4j.RollingFileAppender
    
    #outputs to Tomcat home
    log4j.appender.file.File=${catalina.home}/logs/myapp.log
    log4j.appender.file.MaxFileSize=5MB
    log4j.appender.file.MaxBackupIndex=10
    log4j.appender.file.layout=org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
    

      

    那么生成的日志文件 位置为: apache-tomcat-7.0.82logs目录下(以你的tomcat目录为主)

    配置文件如下:

    <web-app>
      <display-name>Archetype Created Web Application</display-name>
      
    <!--配置文件路径-->
    <context-param>
     	<param-name>contextConfigLocation</param-name>
    	<param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    
    <!-- 字符过滤器 -->  
    <filter>
       <filter-name>encodingFilter</filter-name>
       <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
       <init-param>
           <param-name>encoding</param-name>
           <param-value>UTF-8</param-value>
       </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>encodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping> 
    
    
    <!-- 监听转发 -->
    <listener>
    	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>  
        <servlet-name>json</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>json</servlet-name>  
        <url-pattern>/</url-pattern>  
    </servlet-mapping>      
      
    </web-app>
    

      

    applicationContext.xml自动导入bean

    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    					http://www.springframework.org/schema/beans/spring-beans.xsd 
    					http://www.springframework.org/schema/mvc 
    					http://www.springframework.org/schema/mvc/spring-mvc.xsd 
    					http://www.springframework.org/schema/context 
    					http://www.springframework.org/schema/context/spring-context.xsd">
    
    
    <!-- 默认:注解映射支持 -->		
    <mvc:annotation-driven/>
    <!-- 静态资源配置 -->
    <mvc:resources location="/pages/**" mapping="/pages/"/>
    
    <!-- 自动扫描包名,controller -->
    <context:component-scan base-package="json"/>		
    
    
    </beans>
    

      

    json-servlet.xml

    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    					http://www.springframework.org/schema/beans/spring-beans.xsd 
    					http://www.springframework.org/schema/mvc 
    					http://www.springframework.org/schema/mvc/spring-mvc.xsd 
    					http://www.springframework.org/schema/context 
    					http://www.springframework.org/schema/context/spring-context.xsd">
    			
    <!-- bean视图解析 -->			
    <!-- 
    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>		
    <bean id="rssViewer" class="json.RSSFeedViewer"/>	
    -->
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix" value="/WEB-INF/jsp/" />
          <property name="suffix" value=".jsp" />
       </bean>		
    					
    </beans>	
    

      

    HelloController.java

    package json;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.ui.ModelMap;
    import org.apache.log4j.Logger;
    
    
    @Controller
    public class HelloController {
    	
    	private static final Logger LOGGER = Logger.getLogger(HelloController.class);
    
    	@RequestMapping(value="/hello/log", method=RequestMethod.GET)
    	public String log(ModelMap model)
    	{
    		LOGGER.info("printHello started");
    		
    		if(LOGGER.isDebugEnabled())
    		{
    			LOGGER.debug("Inside: printHello");
    		}
    		
    		//logs exception
    		LOGGER.error("Logging a sample exception", new Exception("Testing"));		
    	    //LOGGER.error("Logging a sample exception", new Exception("Testing"));
    		
    		model.addAttribute("message", "hello Spring mvc Framework");
    		LOGGER.info("pringHello ended");
    		
    		return "hello_log";
    	}
    }
    

      

  • 相关阅读:
    python之os模块
    python之字符串
    python之爬虫(beautifulsoup)
    python之常见算法
    python之装饰器(类装饰器,函数装饰器)
    python之mock使用,基于unittest
    python之定时器
    python基础语法随记
    redis基础
    移动端页面开发(二)
  • 原文地址:https://www.cnblogs.com/achengmu/p/9056893.html
Copyright © 2011-2022 走看看