Cattle back-end customer network project combat (VII): Project debugging techniques

Project debugging techniques

  • Meaning Response status code
  • Server breakpoint debugging skills
  • The client breakpoint debugging skills
  • Set the log level and log output to a different terminal

The response status code

Or the previously mentioned documents, HTTP response status code indicates a specific HTTP request is completed successfully. Response is divided into five categories: information response ( 100- 199), a successful response ( 200- 299), redirect ( 300- 399), client error ( 400- 499) and server errors ( 500- 599).

File

Some common:

  • 200 OK
    • Request was successful. The meaning depends on the success of HTTP methods:
    • GET: resources have been extracted and transmitted in the message body.
    • HEAD: the entity header located in the message body.
    • POST: description of the operation result transmission resource in the message body.
    • TRACE: message body contains a server request message received
  • 302 Found
    • Now temporary resource request response to a request from a different URI. Because of this redirection is temporary after, the client should continue to be sent to the address of the original request. When only is specified in the Cache-Control or Expires in this response it is cacheable.
  • 404 Not Found
    • Request fails, the request would have liked resource is not found on the server. Information can not tell in the end user of this situation is temporary or permanent. If the server knows the case, the status code 410 should be used to inform the old resource allocation mechanism because of some internal problems, it has permanently unavailable, and no addresses can jump. This status code 404 is widely used in the case where the server does not want to reveal why the request was denied in the end or any other suitable response available.
  • 500 Internal Server Error
    • The server encountered do not know how to handle the situation.

Breakpoint server

During development, sometimes you need to troubleshoot a problem, we can use breakpoints. Breakpoint debugging is to lay in a line of code breakpoints, program execution will pause to set breakpoints, and display relevant data. To IDEA, for example, is a break point on the line next to one click, you can set a breakpoint.

More than a written HomeController example, I set a breakpoint in the position as shown, then the program will run in debug mode, the shortcut keys shift + F9 or small insects like button click the upper right corner.

Then open the browser and go home to let the program run we set the breakpoint, the program can be seen stuck in the break this line, and the bottom shows the current number of objects, such as page objects, we can point to open view the page object value . View this line found no problem, you can use shortcut keys or by clicking a button or enter a line running down the inside line of code. These buttons are used on their own how to practice. Including functional lower left corner, you can manage breakpoints.

Client Breakpoints

The client and server break almost, open a browser to enter the home inspection, found js file in the source, click on the line number to set a breakpoint after breakpoint refresh will be stuck in the same run-down and enter the code.

Journal

Before breakpoint debugging, it is best to view the log, a log book can not understand Baidu. springboot supports multiple logging tools, the default is logback

logback official website

There are five log levels can see, we can use different levels in the development and on-line, you can easy to develop,

  • trace trace
  • the debug
  • info normal
  • warn warning
  • error error

Our first experience in a test class, and then configure it in application.properties log level and log storage path

package com.neu.langsam.community;


import org.junit.Test;
import org.slf4j.Logger;

import org.junit.runner.RunWith;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
public class LoggerTests {
	
    //新建一个logger类
    private static final Logger logger= LoggerFactory.getLogger(LoggerTests.class);

    @Test
    public void testLogger(){
        System.out.println(logger.getName());

        logger.debug("debug log");
        logger.info("info log");
        logger.warn("warn log");
        logger.error("error log");


    }
}

# logger
logging.level.com.neu.langsam.community=debug
logging.file.name=d:/workspace/community.log

In the actual development, the need to separate the various levels of logs that use this approach is not enough, you need to write the configuration file, complex, directly copy ready-made, change it. logback-spring.xml on the resource, the main change is the beginning of the path and the local project name, and package name at the end.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <contextName>community</contextName>
    <property name="LOG_PATH" value="D:/workspace"/>
    <property name="APPDIR" value="community"/>

    <!-- error file -->
    <appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}/${APPDIR}/log_error.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/${APPDIR}/error/log-error-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>5MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <append>true</append>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
            <charset>utf-8</charset>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>error</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!-- warn file -->
    <appender name="FILE_WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}/${APPDIR}/log_warn.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/${APPDIR}/warn/log-warn-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>5MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <append>true</append>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
            <charset>utf-8</charset>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>warn</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!-- info file -->
    <appender name="FILE_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}/${APPDIR}/log_info.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/${APPDIR}/info/log-info-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>5MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <append>true</append>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
            <charset>utf-8</charset>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>info</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!-- console -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
            <charset>utf-8</charset>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>debug</level>
        </filter>
    </appender>

    <logger name="com.neu.langsam.community" level="debug"/>

    <root level="info">
        <appender-ref ref="FILE_ERROR"/>
        <appender-ref ref="FILE_WARN"/>
        <appender-ref ref="FILE_INFO"/>
        <appender-ref ref="STDOUT"/>
    </root>

</configuration>

I then output to the project directory.

Published 12 original articles · won praise 0 · Views 102

Guess you like

Origin blog.csdn.net/weixin_42700635/article/details/104805400