Springboot -- build your own springboot project (with log configuration)

Keywords: Java Spring encoding SpringBoot

Using spring Initalizr to build spring boot in idea

  1. Click new item and select as shown in the figure

  1. After clicking next

  1. Click next, and then select as shown in the figure

  1. Selection path

  1. Click finish, as shown in the figure, delete what you don't want, and the project is completed

  1. Build a controller and start the project to see the returned results

Build your own springboot project on your own server

Using idea to deliver projects to remote services

  1. Set idea

  1. Configuration related information

  1. Upload to specified machine

Configure startup script based on Java jar command

  • start.sh
#!/bin/bash
nohup java -jar target/zplxjj.jar  &
  • stop.sh
#!/bin/bash
PID=$(ps -ef | grep target/zplxjj.jar | grep -v grep | awk '{ print $2 }')
if [ -z "$PID" ]
then
    echo Application is already stopped
else
    echo kill $PID
    kill $PID
fi
~
  • run.sh
#!/bin/bash
echo stop application
source stop.sh
echo start application
source start.sh

To start your own project, you only need to execute run.sh, and a spring boot of your own will be built

logback configuration

In the actual project, we hope that the log can be recorded on the server. Here we use logback, which comes with springboot. My integration method is to add the logback-spring.xml file. After adding, we can start the project. The content of the file is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!--Label used to define the value of a variable-->
    <property name="LOG_HOME" value="./logs"/>
    <property name="encoding" value="UTF-8"/>
    <!--Format output:%d Represents the date,%thread Represents the thread name,%-5level: The level displays 5 character width from the left;%M:%L Is the method and line number;%msg Is a log message;%n Newline character-->
    <property name="normal-pattern"
              value="%d{yyyy-MM-dd/HH:mm:ss.SSS}|%X{localIp}|%X{requestId}|%X{requestSeq}|%X{country}|%X{deviceType}|%X{deviceId}|%X{userId}|^_^|[%t] %-5level %logger{50} %line - %m%n"/>
    <property name="plain-pattern" value="%d{yyyy-MM-dd.HH:mm:ss} %msg%n"/>

    <!-- Generate log files on a daily basis -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!--Filename of log file output-->
        <file>${LOG_HOME}/zplxjj.log</file>
        <Append>true</Append>
        <prudent>false</prudent>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${normal-pattern}</pattern>
            <charset>${encoding}</charset>
        </encoder>
        <!--Split by time-->
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/zplxjj.log.%d{yyyy-MM-dd}.%i</fileNamePattern>
            <maxFileSize>128MB</maxFileSize>
            <maxHistory>15</maxHistory>
            <totalSizeCap>32GB</totalSizeCap>
        </rollingPolicy>
    </appender>

    <!--console output -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--Format output:%d Represents the date,%thread Represents the thread name,%-5level: Level 5 character width from left%msg: Log messages,%n Newline character-->
            <pattern>${normal-pattern}</pattern>
        </encoder>
    </appender>

    <!-- log file error -->
    <appender name="ERROR"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
        <file>${LOG_HOME}/zplxjj-error.log</file>
        <prudent>false</prudent>
        <Append>true</Append>
        <encoder>
            <pattern>${normal-pattern}</pattern>
            <charset>${encoding}</charset>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/zplxjj-error.log.%d{yyyy-MM-dd}.%i</fileNamePattern>
            <maxFileSize>128MB</maxFileSize>
            <maxHistory>15</maxHistory>
            <totalSizeCap>32GB</totalSizeCap>
        </rollingPolicy>
    </appender>

    <root level="INFO">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="FILE"/>
        <appender-ref ref="ERROR"/>
    </root>
</configuration>

The effect is as follows:

I also opened the WeChat public address: stonezplxjj, and more articles are welcome to pay attention to the public address:

Posted by sureshmaharana on Sun, 24 Nov 2019 07:17:55 -0800