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

一、配置文件

在开发过程中我们经常会使用一些配置信息,这块配置在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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,993评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,974评论 6 342
  • 要加“m”说明是MB,否则就是KB了. -Xms:初始值 -Xmx:最大值 -Xmn:最小值 java -Xms8...
    dadong0505阅读 4,924评论 0 53
  • 我不知道自己的初中生活样子,可是当我坐在孩子的初中课堂的时候,我无语了。 孩子生病了在家休息。要进行体育达标考试,...
    乔的园子阅读 366评论 0 1
  • 摘要: 人是会变的,所以人本身是靠不住的,唯一靠得住的就是规则。 规则是一种契约。若不能够遵守和执行,一切规则都形...
    tsglxy阅读 411评论 0 1