maven package,logback采集日志,jar包启动脚本模板

maven package,logback采集日志,jar包启动脚本模板

一. logback.xml

1. pom依赖
<dependencies>
    <!-- logback必备依赖 -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.30</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.2.3</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.3</version>
    </dependency>
</dependencies>
2.logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
    <!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径-->
    <property name="LOG_HOME" value="/home/xiahu" />
    <!-- 控制台输出 -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
    </appender>
    <!-- 按照每天生成日志文件 -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日志文件输出的文件名-->
            <FileNamePattern>${LOG_HOME}/fileMonitor.log.%d{yyyy-MM-dd}.log</FileNamePattern>
            <!--日志文件保留天数-->
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
        <!--日志文件最大的大小-->
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

    <!-- 日志输出级别 -->
    <root level="INFO">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>
</configuration>
3.代码内log使用
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @author XiaHu
 * @create 2020/4/21
 */
public class MonitorStart {
    private static final Logger LOG = LoggerFactory.getLogger(MonitorStart.class);

}
二. maven内打包配置
1.pom

<build>
    <plugins>
        <!-- java打包插件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <!-- scala打包插件 -->
        <plugin>
            <groupId>org.scala-tools</groupId>
            <artifactId>maven-scala-plugin</artifactId>
            <version>2.15.2</version>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <finalName>fileMonitor</finalName>
                <descriptors>
                    <descriptor>src/main/assembly/package.xml</descriptor>
                </descriptors>
                <archive>
                    <manifest>
                        <mainClass>com.clb.MonitorStart</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

2. package.xml
<assembly
	xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
	<id></id>
	<formats>
		<!-- 将项目打成一个zip包 -->
		<format>zip</format>
		<!-- 将项目打成一个文件目录 -->
		<format>dir</format>
	</formats>
	<fileSets>
		<!-- 将conf打包成zip包下的conf目录 -->
		<fileSet>
			<directory>src/main/resources/conf</directory>
			<outputDirectory>conf</outputDirectory>
		</fileSet>
		<!-- resources目录下的logback.xml打包到根目录 -->
		<fileSet>
			<directory>src/main/resources</directory>
			<includes>
				<include>logback.xml</include>
				<!-- <include>*.*</include> -->
			</includes>
			<outputDirectory>/</outputDirectory>
		</fileSet>
		<!-- 将bin目录下的所有文件打包成zip包下的bin目录,并且权限为755 -->
		<fileSet>
			<directory>src/main/resources/bin</directory>
			<includes>
				<include>*.*</include>
			</includes>
			<directoryMode>775</directoryMode>
			<outputDirectory>bin</outputDirectory>
		</fileSet>
	</fileSets>
	<dependencySets>
        <!-- 使用项目的artifact,第三方jar不要解压,打包进zip文件的libs目录 -->
		<dependencySet>
			<useProjectArtifact>true</useProjectArtifact>
            <unpack>false</unpack>
			<outputDirectory>libs</outputDirectory>
			<!-- 将scope为runtime的依赖包打包到lib目录下。 -->
			<scope>runtime</scope>
		</dependencySet>
	</dependencySets>
</assembly>
三. shell的使用
1. start.sh
#!/bin/bash
bashurl=$(cd `dirname $0`; pwd)/..

jar_path=${bashurl}/libs
conf_path=${bashurl}/conf
echo "执行命令: nohup  java -server -Xms1G -Xmx1G -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${bashurl}/log -Dlogback.configurationFile=${conf_path}/logback.xml  -classpath ${jar_path}/*:. com.clb.MonitorStart ${conf_path}/fileMonitor.properties  &"

if [ ! -d "${bashurl}/log" ]; then
 mkdir -p ${bashurl}/log
fi

nohup  nohup  java -server -Xms1G -Xmx1G -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${bashurl}/log -Dlogback.configurationFile=${conf_path}/logback.xml   -classpath ${jar_path}/*:. com.clb.MonitorStart ${conf_path}/fileMonitor.properties  &
pid=$!
if [[ -z "${pid}" ]]; then
    echo "start failed!"
    exit 1
else
    echo "start succeeded!"
    echo $pid > ${bashurl}/bin/server.pid
    sleep 1
fi
2. stop.sh
#!/bin/bash

pid_path=$(cd `dirname $0`; pwd)/server.pid

function wait_for_server_to_die() {
  local pid
  local count
  pid=$1
  timeout=$2
  count=0
  timeoutTime=$(date "+%s")
  let "timeoutTime+=$timeout"
  currentTime=$(date "+%s")
  forceKill=1

  while [[ $currentTime -lt $timeoutTime ]]; do
    $(kill ${pid} > /dev/null 2> /dev/null)
    if kill -0 ${pid} > /dev/null 2>&1; then
      sleep 3
    else
      forceKill=0
      break
    fi
    currentTime=$(date "+%s")
  done

  if [[ forceKill -ne 0 ]]; then
    $(kill -9 ${pid} > /dev/null 2> /dev/null)
  fi
}

if [[ ! -f "${pid_path}" ]]; then
    echo "server is not running"
else
    pid=$(cat ${pid_path})
    if [[ -z "${pid}" ]]; then
      echo "server is not running"
    else
      wait_for_server_to_die $pid 40
      $(rm -f ${pid_path})
      echo "server is stopped."
    fi
fi
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值