美文网首页我爱编程
SpringBoot(三)配置信息+日志文件+Filter

SpringBoot(三)配置信息+日志文件+Filter

作者: 熊猫测试版 | 来源:发表于2018-04-07 10:23 被阅读0次

一、配置文件

在开发过程中我们经常会使用一些配置信息,这块配置在application.properties文件中

com.pandabeta.name=Pandabeta

在controller文件中 使用配置信息 添加:

@Value("${com.pandabeta.name}")
private String name;
@RequestMapping("/pandabeta")
public String index() {
    return name;
}

运行可看到如下结果:


image.png

二、添加日志

在 application.properties 配置输出的地址和输出级别:

#logging.path=C:\\develop\\ide\\log
logging.file=pandabeta
logging.level.com.favorites=DEBUG
logging.level.org.springframework.web=INFO
logging.level.org.hibernate=ERROR

在controller文件中 使用日志记录:

@RestController
public class HelloWorldController {
    
    @Value("${com.pandabeta.name}")
    private String name;
    
    public static Log log = LogFactory.getLog(HelloWorldController.class);
    
    @RequestMapping("/pandabeta")
    public String index() {
        log.info(name);
        return name;
    }

    @RequestMapping("/hello/{name}")  
    String index(@PathVariable String name) {  
        return "Hello "+name+"!!!";  
    }
}

ps:

  1. 这里若不配置具体的包的日志级别,日志文件信息将为空
  2. 若只配置logging.path,那么将会在C:\develop\ide\log文件夹生成一个日志文件为spring.log
  3. 若只配置logging.file,那将会在项目的当前路径下生成一个pandabeta.log日志文件
  4. logging.path和logging.file同时配置,不会有在这个路径有F:\demo\demo.log日志生成,logging.path和logging.file不会进行叠加
  5. logging.path和logging.file的value都可以是相对路径或者绝对路径

三、Filter过滤器

在项目中会使用filters用于录调用日志、排除有XSS威胁的字符、鉴权等等。Spring Boot自动添加了OrderedCharacterEncodingFilter和HiddenHttpMethodFilter,并且我们可以自定义Filter。
主要是2个步骤:

  1. 实现Filter接口,实现Filter方法
  2. 添加@Configuration 注解,将自定义Filter加入过滤链
@Configuration
public class TestFilter {
    
    public static Log log = LogFactory.getLog(TestFilter.class);
    
    @Bean
    public RemoteIpFilter remoteIpFilter() {
        return new RemoteIpFilter();
    }
    
    @Bean
    public FilterRegistrationBean testFilterRegistration() {

        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new MyFilter());
        registration.addUrlPatterns("/*");
        registration.addInitParameter("paramName", "paramValue");
        registration.setName("MyFilter");
        registration.setOrder(1);
        return registration;
    }
    
    public class MyFilter implements Filter {
        @Override
        public void destroy() {
            log.info("filter destory");
            // TODO Auto-generated method stub
        }

        @Override
        public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain filterChain)
                throws IOException, ServletException {
            // TODO Auto-generated method stub
            HttpServletRequest request = (HttpServletRequest) srequest;
            System.out.println("this is MyFilter,url :"+request.getRequestURI());
            log.info("filter do before");
            filterChain.doFilter(srequest, sresponse);
            log.info("filter do after");
        }

        @Override
        public void init(FilterConfig arg0) throws ServletException {
            log.info("filter init");
            // TODO Auto-generated method stub
        }
    }
}

运行结果:


image.png

相关文章

网友评论

    本文标题:SpringBoot(三)配置信息+日志文件+Filter

    本文链接:https://www.haomeiwen.com/subject/kaikhftx.html