Skip to content

Commit

Permalink
add log.path setting logic
Browse files Browse the repository at this point in the history
  • Loading branch information
blackstar-baba committed Sep 26, 2020
1 parent cd74047 commit d2e08c1
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**
!**/src/test/**
logs/

### STS ###
.apt_generated
Expand Down
2 changes: 1 addition & 1 deletion src/bin/startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ JAVA_OPT="-server -Xms1g -Xmx1g"
JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${BASE_PATH}/"

# start app
nohup java ${JAVA_OPT} -jar ${JAR_DIR}/${APPLICATION_JAR} --spring.config.location=${CONFIG_DIR} --logging.config=${CONFIG_DIR}/logback.xml > /dev/null 2>&1 &
nohup java ${JAVA_OPT} -jar ${JAR_DIR}/${APPLICATION_JAR} --spring.config.location=${CONFIG_DIR} --logging.config=${CONFIG_DIR}/logback.xml --log.path=${LOG_DIR} > /dev/null 2>&1 &

echo ${APPLICATION} start successfully

Expand Down
5 changes: 5 additions & 0 deletions src/main/assembly/assembly.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
</includes>
<fileMode>0644</fileMode>
</fileSet>
<fileSet>
<directory>src/main/resources/source</directory>
<outputDirectory>source</outputDirectory>
<fileMode>0755</fileMode>
</fileSet>
</fileSets>

</assembly>
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,35 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Optional;

@SpringBootApplication
@Transactional
public class SoftwareLabApplication {


private static final String LOG_PATH = "--log.path";

private static final String DEFAULT_LOG_PATH = "/logs";

public static void main(String[] args) {
args = process(args);
SpringApplication.run(SoftwareLabApplication.class, args);
}

private static String[] process(String[] args) {
// process
//log_path set
Optional<String> logArgs = Arrays.stream(args).filter(arg -> arg.startsWith(LOG_PATH)).findFirst();

if (logArgs.isPresent()) {
System.setProperty("log.path", logArgs.get().split("=")[1]);
} else {
System.setProperty("log.path", System.getProperty("user.dir")+DEFAULT_LOG_PATH);
}
return args;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.blackstar.softwarelab.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
public class EncoderConfiguration {

@Bean
protected BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,6 @@ protected void configure(HttpSecurity http) throws Exception {
.addFilterBefore(buildWsJwtTokenAuthenticationProcessingFilter(), UsernamePasswordAuthenticationFilter.class);
}

@Bean
protected BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}


protected RestLoginProcessingFilter buildRestLoginProcessingFilter() throws Exception {
RestLoginProcessingFilter filter = new RestLoginProcessingFilter(FORM_BASED_LOGIN_ENTRY_POINT, successHandler, failureHandler, objectMapper);
Expand Down
35 changes: 29 additions & 6 deletions src/main/java/com/blackstar/softwarelab/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;

public class FileUtil {


public static String upload(MultipartFile file, String path){
public static String upload(MultipartFile file, String targetPath) {
String fileName = file.getOriginalFilename();
// 扩展名 abc.jpg
String fileExtensionName = fileName.substring(fileName.lastIndexOf(".")+1);
String fileExtensionName = fileName.substring(fileName.lastIndexOf(".") + 1);
// 避免重名
String uploadFileName = UUID.randomUUID().toString()+"."+fileExtensionName;
String uploadFileName = UUID.randomUUID().toString() + "." + fileExtensionName;

File fileDir = new File(path);
if(!fileDir.exists()){
File fileDir = new File(targetPath);
if (!fileDir.exists()) {
fileDir.mkdirs();
}

File targetFile = new File(path,uploadFileName);
File targetFile = new File(targetPath, uploadFileName);
try {
file.transferTo(targetFile); // 文件已经上传成功了 // 已经上传到ftp服务器上
} catch (IOException e) {
Expand All @@ -33,4 +35,25 @@ public static String upload(MultipartFile file, String path){
}


public static byte[] getContent(String filePath) {
byte[] bytes = null;
File file = new File(filePath);
if (!file.exists()) {
return bytes;
}else {
try {
FileInputStream fileInputStream = new FileInputStream(file);
bytes = new byte[fileInputStream.available()];
fileInputStream.read(bytes);
fileInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return bytes;
}


}
8 changes: 4 additions & 4 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<!DOCTYPE configuration>
<configuration scan="true" scanPeriod="10 seconds">

<property name="log.root.level" value="INFO"/> <!-- 日志级别 -->
<property name="log.fileName" value="softwarelab"/> <!-- 模块名称, 影响日志配置名,日志文件名 -->
<property name="log.base" value="../logs/"/>
<property name="log.root.level" value="INFO"/>
<property name="log.fileName" value="softwarelab"/>
<property name="log.path" value="${log.path}"/>

<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
Expand All @@ -20,7 +20,7 @@
<!--滚动策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!--路径 -->
<fileNamePattern>${log.base}/${log.fileName}-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<fileNamePattern>${log.path}/${log.fileName}-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>100MB</maxFileSize>
<maxHistory>30</maxHistory>
<totalSizeCap>3GB</totalSizeCap>
Expand Down

0 comments on commit d2e08c1

Please sign in to comment.