当前位置:   article > 正文

SpringBoot(四)spring-boot-web-log4j_spring-boot-starter-web对应logj版本

spring-boot-starter-web对应logj版本
spring boot 中打印日志的配置方式
首先看maven的配置文件
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.julongtech</groupId>
  5. <artifactId>SpringBootWebTest</artifactId>
  6. <packaging>war</packaging>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>SpringBootWebTest Maven Webapp</name>
  9. <url>http://maven.apache.org</url>
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-parent</artifactId>
  13. <version>1.4.2.RELEASE</version>
  14. </parent>
  15. <dependencies>
  16. <!-- This is a web application -->
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-web</artifactId>
  20. <!-- 过滤掉 -->
  21. <exclusions>
  22. <exclusion>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-logging</artifactId>
  25. </exclusion>
  26. </exclusions>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-log4j2</artifactId>
  31. </dependency>
  32. <!-- Tomcat embedded container -->
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-tomcat</artifactId>
  36. <scope>provided</scope>
  37. </dependency>
  38. <!-- JSTL for JSP -->
  39. <dependency>
  40. <groupId>javax.servlet</groupId>
  41. <artifactId>jstl</artifactId>
  42. </dependency>
  43. <!-- Need this to compile JSP -->
  44. <dependency>
  45. <groupId>org.apache.tomcat.embed</groupId>
  46. <artifactId>tomcat-embed-jasper</artifactId>
  47. <scope>provided</scope>
  48. </dependency>
  49. <!-- Need this to compile JSP, tomcat-embed-jasper version is not working,
  50. no idea why -->
  51. <dependency>
  52. <groupId>org.eclipse.jdt.core.compiler</groupId>
  53. <artifactId>ecj</artifactId>
  54. <version>4.6.1</version>
  55. <scope>provided</scope>
  56. </dependency>
  57. <!-- Optional, test for static content, bootstrap CSS -->
  58. <dependency>
  59. <groupId>org.webjars</groupId>
  60. <artifactId>bootstrap</artifactId>
  61. <version>3.3.7</version>
  62. </dependency>
  63. </dependencies>
  64. <build>
  65. <finalName>SpringBootWebTest</finalName>
  66. <plugins>
  67. <!-- Package as an executable jar/war -->
  68. <plugin>
  69. <groupId>org.springframework.boot</groupId>
  70. <artifactId>spring-boot-maven-plugin</artifactId>
  71. </plugin>
  72. </plugins>
  73. </build>
  74. </project>
在项目中建立 application.properties 文件

在文件中添加log4j配置

  1. #设置访问的路径
  2. spring.mvc.view.prefix: /WEB-INF/pages/
  3. spring.mvc.view.suffix: .jsp
  4. # Context path of the application.
  5. #设置访问的目录 此处可以是项目名称也可以自己定义 http://localhost:8080/SpringBoot/
  6. server.context-path: /SpringBoot
  7. #日志配置
  8. #日志级别
  9. logging.level.root=DEBUG
  10. logging.level.org.springframework.web=DEBUG
  11. logging.level.com.julongtech=DEBUG
  12. #logger存放文件 Log file name. For instance `myapp.log`
  13. logging.file=C://LOG4J/log.log
  14. # Logging pattern for the console
  15. #logging.pattern.console= "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"
  16. # Logging pattern for file
  17. #logging.pattern.file= "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"
创建的controller
  1. package com.julongtech.action;
  2. import java.util.Map;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. @Controller
  8. public class HelloController {
  9. private Logger logger = LoggerFactory.getLogger(HelloController.class);
  10. @RequestMapping("/")
  11. public String welcome(Map<String, Object> model) {
  12. logger.debug("11111111111111111111111");
  13. model.put("message", "123");
  14. return "index";
  15. }
  16. }
创建启动类SpringBootWebApplication
  1. package com.julongtech.action;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.builder.SpringApplicationBuilder;
  5. import org.springframework.boot.web.support.SpringBootServletInitializer;
  6. @SpringBootApplication
  7. public class SpringBootWebApplication extends SpringBootServletInitializer {
  8. @Override
  9. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  10. return application.sources(SpringBootWebApplication.class);
  11. }
  12. public static void main(String[] args) throws Exception {
  13. SpringApplication.run(SpringBootWebApplication.class, args);
  14. }
  15. }
创建文件夹src\main\webapp\WEB-INF\pages 创建jsp界面 ,完成后运行项目 会看到日志级别调整了
官方给的参数如下
  1. # LOGGING
  2. logging.config= # Location of the logging configuration file. For instance `classpath:logback.xml` for Logback
  3. logging.exception-conversion-word=%wEx # Conversion word used when logging exceptions.
  4. logging.file= # Log file name. For instance `myapp.log`
  5. logging.level.*= # Log levels severity mapping. For instance `logging.level.org.springframework=DEBUG`
  6. logging.path= # Location of the log file. For instance `/var/log`
  7. logging.pattern.console= # Appender pattern for output to the console. Only supported with the default logback setup.
  8. logging.pattern.file= # Appender pattern for output to the file. Only supported with the default logback setup.
  9. logging.pattern.level= # Appender pattern for log level (default %5p). Only supported with the default logback setup.
  10. logging.register-shutdown-hook=false # Register a shutdown hook for the logging system when it is initialized.






声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/813266
推荐阅读
相关标签
  

闽ICP备14008679号