Spring boot-logback and access log _spring

Source: Internet
Author: User
Tags system log throw exception custom name log4j
Logback Logback from log4j, performance and functionality compared to log4j made some improvements, and configuration methods and log4j are similar to the default log component of Spring boot. In the Application.properties file, you can do some simple log configuration, refer to the fifth article. If you want to make some more detailed configuration, you need to configure Logback-spring.xml Logback-spring.xml spring in the resources directory Boot officially recommends using the Logback-spring.xml file name to configure the default Logback log. About Logback detailed knowledge can check official documentation, here provides a more practical configuration: Output info level log In the console, in addition, warn warning log and error log saved to different directory files, specifically look at the comments [HTML] View plain copy <?xml version= "1.0"  encoding= "UTF-8"?>   <configuration>        <!-- appender is the configuration output terminal, Consoleappender is the console, name is the custom name  -->        <appender name= "STDOUT"  class= "Ch.qos.logback.core.ConsoleAppender" >            <encoder>                <!--  Configuration log format, which is a more general format  -->                <pattern>%d{hh:mm:ss. sss} %-5level %logger{35} - %msg%n</pattern>            </encoder>       </appender>               <!--  Output terminal is scrolling file  -->        <appender name= "WARN"  class= "Ch.qos.logback.core.rolling.RollingFileAppender" >           <!--  based on time scrolling, is the daily log output to a different file  -- >           <rollingpolicy class= " Ch.qos.logback.core.rolling.TimeBasedRollingPolicy ">                <!--  Output log directory file name, window default partition is the current program's hard disk partition,%D{YYYY-MM-DD} is the current date  -->                <filenamepattern>/ Log/warn/warn.%d{yyyy-mm-dd}.log</filenamepattern>               <!--   Maximum save 99 files, exceeding history files will be deleted  -->                <maxHistory>99</maxHistory>            </rollingPolicy>           <!--  Filter by log level  -->           <filter class= " Ch.qos.logback.classic.filter.LevelFilter ">                <!--  Only collect warn level logs, other high-level and low-level logs are discarded  -->                <level>WARN</level>                <onMatch>ACCEPT</onMatch>               <onmismatch>deny</onmismatch>            </filter>            <encoder>               < Pattern>%d{hh:mm:ss. sss} %-5level %logger{35} - %msg%n</pattern>            </encoder>       </appender>              <!--  In addition to filter, other   configurations are the same as above,  Just name and file path different-->       <appender name= "ERROR"  class= " Ch.qos.logback.core.rolling.RollingFileAppender ">            <rollingpolicy class= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy" >                <filenamepattern>/log/error/error.%d{yyyy-mm-dd}.log</ filenamepattern>               < maxhistory>99</maxhistory>           </ rollingpolicy>           <!--  threshold filter  -->            <filter class= " Ch.qos.logback.classic.filter.ThresholdFilter ">                <!--  Collect error and error above level of log  -->                <level>ERROR</level>            </filter>           < encoder>   &NBSP;&NBSP;&NBSP;&NBSP;&NBSp;       <pattern>%d{hh:mm:ss. sss} %-5level %logger{35} - %msg%n</pattern>            </encoder>       </appender>              <!-- root is the root log printer, only one, responsible for the entire system log output    -->       <root level= "INFO" >            <!--  Configure the top three output terminals to the root printer, which will take effect on the entire system  .  -->           <appender-ref ref= "STDOUT"  />           <appender-ref ref= "WARN"  / >           <appender-ref ref= "ERROR"  />        </root>          <!-- logger is the child printer of root, and can have multiple, output logs in a package of name configuration.  -->       <!--  Hello.dao is my mybatis. Map DAO's package name, set to debug SQL statements that can print MyBatis  -->       < Logger name= "Hello.dao"  level= "DEBUG"  />   </configuration>     

After running the result is this, e disk is my program current partition, will automatically generate the log directory below, will save the error and warn level of log

Level log Levels

In the above configuration of the Leven parameters, the main use of four, the level from low to high respectively for Debug,info,warn,error, when setting a level of log, than his low-level log will not output.

Debug: Debugging, from the name can see its role. Use debugging to see more error messages when the errors in your program cannot be resolved.  This log is used in mybatis to output SQL statements. Debug output is too large, and do not understand the source of many people do not understand, there are many no practical role of the log information brush screen, only suitable for the debugging of abnormal procedures.

Info: Information, much less than debug, will only output some important information, suitable for development use.

Warn: warning, usually can fix, do not fix also not necessarily throw exception, sometimes is some hint information, but more important than info information

Error: Wrong, obviously, is program exception access access log

Tomcat has the default access log to record the visitor's IP, but this log is turned off by default in spring boot. If you have a Nginx class HTTP server in your server structure, the access log should be configured in Nginx. A background server such as Tomcat only needs to record the background log, and does not need to manage access logs. If you want to open an access log in spring boot, you can configure the in Application.properties

[Plain] View plain copy #配置tomcat工作目录, Server.tomcat.basedir=/tomcat #开启accesslog for the Tomcat directory for the current partition, which is recorded in the directory above server.to Mcat.accesslog.enabled=true

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.