Skip to content

Commit

Permalink
1. optimize code
Browse files Browse the repository at this point in the history
2. add release workflow
3. modify project version and artifact
  • Loading branch information
supervate committed Apr 30, 2024
1 parent a5af59d commit 13da347
Show file tree
Hide file tree
Showing 12 changed files with 605 additions and 334 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Release

# Run workflow on commits to the `main` branch
on:
create:
tags:
- '*'

env:
build-number: ${GITHUB_RUN_NUMBER}

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Check out Git repository
uses: actions/checkout@v4
- name: Install Java and Maven
uses: actions/setup-java@v4
with:
distribution: 'adopt-hotspot'
java-version: '21'
cache: 'maven'
- name: Run Tests
run: make test
- name: Upload Agent Jar Artifact
uses: actions/upload-artifact@v3
with:
name: Package
path: target
retention-days: 1
- name: Automatic releases
uses: marvinpinto/action-automatic-releases@latest
with:
repo_token: "${{ secrets.ARTLIBS_TOKEN }}"
automatic_release_tag: "${{ github.run_number }}"
title: "Automated Build ${{ github.run_number }}"
prerelease: true
files: target/autotrace4j.jar
- name: Release Maven Central
uses: samuelmeuli/action-maven-publish@v1
with:
server_id: central
gpg_private_key: ${{ secrets.gpg_private_key }}
gpg_passphrase: ${{ secrets.gpg_passphrase }}
nexus_username: ${{ secrets.nexus_username }}
nexus_password: ${{ secrets.nexus_password }}
93 changes: 87 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>vt.github</groupId>
<artifactId>V-Log</artifactId>
<version>1.0-SNAPSHOT</version>
<groupId>io.github.vt</groupId>
<artifactId>v-log</artifactId>
<version>1.0.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>5.8.1</junit.version>
<maven-compiler.version>3.12.0</maven-compiler.version>
<compiler-release.version>8</compiler-release.version>
<jackson.version>2.17.0</jackson.version>
</properties>

<dependencies>
Expand All @@ -29,12 +30,28 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.32</version>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand All @@ -54,6 +71,70 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
</plugin>
<!-- Setup plugin for deployment to Maven Central. -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.3.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.6.3</version>
<configuration>
<doclint>none</doclint>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
<configuration>
<!-- Prevent `gpg` from using pinentry programs -->
<gpgArguments>
<arg>--pinentry-mode</arg>
<arg>loopback</arg>
</gpgArguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.central</groupId>
<artifactId>central-publishing-maven-plugin</artifactId>
<version>0.4.0</version>
<extensions>true</extensions>
<configuration>
<publishingServerId>central</publishingServerId>
<tokenAuth>true</tokenAuth>
<!--<waitUntil>published</waitUntil>
<waitMaxTime>1800</waitMaxTime>-->
<autoPublish>true</autoPublish>
</configuration>
</plugin>
</plugins>
</build>
</project>
2 changes: 1 addition & 1 deletion src/main/java/vt/suopo/vlog/DefaultLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.time.ZoneId;
import java.util.Optional;

import static vt.suopo.vlog.common.LogConstants.*;
import static vt.suopo.vlog.common.Constants.*;


/**
Expand Down
30 changes: 22 additions & 8 deletions src/main/java/vt/suopo/vlog/LoggerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

import static vt.suopo.vlog.common.LogConstants.SYSTEM_PROPERTY_LOG_DIR;
import static vt.suopo.vlog.common.LogConstants.SYSTEM_PROPERTY_LOG_LEVEL;
import static vt.suopo.vlog.common.Constants.*;

/**
* 日志工厂
* <p>
* 暂提供如下配置(通过SystemProperty配置):
* 1. autotrace4j.log.dir autotrace4j产生的日志文件存放目录
* 2. autotrace4j.log.level autotrace4j产生的日志的最低级别,大于对应级别的日志才会被打印.
* <li>
* 1. vt.vlog.dir 产生的日志文件存放目录
* <li>
* 2. vt.vlog.level 产生的日志的最低级别,大于对应级别的日志才会被打印.
* <li>
* 3. vt.vlog.file.retention 产生的日志文件存放时间,保留多少天,默认为7天.(单位天)
* <li>
* 4. vt.vlog.file.size 产生的日志文件大小,超过多少字节后,产生新的文件,默认为0,不限制.(单位字节)
*
* @author suopovate
* @since 2024/04/27
Expand All @@ -43,10 +48,19 @@ public class LoggerFactory {
);
defaultPrintStreamAppender.start();
APPENDER_COMBINER.addAppender(defaultPrintStreamAppender);
Optional
.ofNullable(SystemUtils.getSysPropertyPath(SYSTEM_PROPERTY_LOG_DIR))
SystemUtils
.getSysPropertyPath(SYSTEM_PROPERTY_LOG_DIR)
.ifPresent(path -> {
DefaultFileAppender defaultFileAppender = new DefaultFileAppender(new DefaultLayout(), path);
DefaultFileAppender defaultFileAppender = new DefaultFileAppender(
new DefaultLayout(),
path,
SystemUtils
.getSysPropertyInteger(SYSTEM_PROPERTY_LOG_FILE_RETENTION)
.orElse(DEFAULT_LOG_FILE_RETENTION),
SystemUtils
.getSysPropertyInteger(SYSTEM_PROPERTY_LOG_FILE_SIZE)
.orElse(DEFAULT_LOG_FILE_SIZE)
);
defaultFileAppender.start();
APPENDER_COMBINER.addAppender(defaultFileAppender);
});
Expand All @@ -67,7 +81,7 @@ public static Logger logger(Class<?> clazz) {
return logger(clazz.getCanonicalName());
}

static public Logger logger(String name) {
public static Logger logger(String name) {
Logger logger = LOGGER_MAP.get(name);
if (logger == null) {
logger = new Logger(name, APPENDER_COMBINER, LEVEL);
Expand Down
25 changes: 22 additions & 3 deletions src/main/java/vt/suopo/vlog/appender/DefaultFileAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.spi.AbstractInterruptibleChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -30,6 +31,9 @@
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import static vt.suopo.vlog.common.Constants.DEFAULT_LOG_FILE_RETENTION;
import static vt.suopo.vlog.common.Constants.DEFAULT_LOG_FILE_SIZE;

/**
* 默认日志输出-文件
* <p>
Expand Down Expand Up @@ -65,11 +69,11 @@ public class DefaultFileAppender extends AsyncAppender<LogEvent> {
ThreadLocal<ByteBuffer> logWriteBuffer = ThreadLocal.withInitial(() -> ByteBuffer.allocateDirect(WRITE_BUFFER_SIZE));

public DefaultFileAppender(Layout<LogEvent> layout, Path directory) {
this(layout, directory, 7, 0);
this(layout, directory, DEFAULT_LOG_FILE_RETENTION, DEFAULT_LOG_FILE_SIZE);
}

public DefaultFileAppender(Layout<LogEvent> layout, Path directory, int logFileRetentionDays) {
this(layout, directory, logFileRetentionDays, 0);
this(layout, directory, logFileRetentionDays, DEFAULT_LOG_FILE_SIZE);
}

public DefaultFileAppender(Layout<LogEvent> layout, Path directory, int logFileRetentionDays, int logFileSizeBytes) {
Expand Down Expand Up @@ -170,7 +174,6 @@ void doAppend(LogEvent event) {
} catch (Exception e) {
System.err.println(ThrowableUtils.throwableToStr(e));
}

}

private Tuple2<Path, FileChannel> getLogFile(int messageLength) throws IOException {
Expand All @@ -183,6 +186,8 @@ private Tuple2<Path, FileChannel> getLogFile(int messageLength) throws IOExcepti
return fileAndChannel;
}
try {
// close the old channel
closeFileChannel(logFile.get());
LocalDateTime date = LocalDateTime.now();
int fileIndex = computeFileIndex(date, true);
fileAndChannel = openLogFile(logFileNamePrefix, fileIndex);
Expand Down Expand Up @@ -286,6 +291,20 @@ public static String dateToLogFileName(LocalDateTime date) {
return date.format(DateTimeFormatter.ISO_LOCAL_DATE);
}

private void closeFileChannel(Tuple2<Path, FileChannel> logFile) {
Optional
.ofNullable(logFile)
.map(Tuple2::getO2)
.filter(AbstractInterruptibleChannel::isOpen)
.ifPresent(fileChannel -> {
try {
fileChannel.close();
} catch (IOException e) {
System.out.println("[DefaultFileAppender] close log file failed.");
}
});
}

private static class CleanerTask implements Runnable {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* <p>
* All rights Reserved.
*/
public class LogConstants {
public class Constants {

public static final String SPACE = " ";
public static final String LEFT_MIDDLE_BRACKET = "[";
Expand All @@ -17,5 +17,10 @@ public class LogConstants {

public static final String SYSTEM_PROPERTY_LOG_DIR = "vt.vlog.dir";
public static final String SYSTEM_PROPERTY_LOG_LEVEL = "vt.vlog.level";
public static final String SYSTEM_PROPERTY_LOG_FILE_RETENTION = "vt.vlog.file.retention";
public static final String SYSTEM_PROPERTY_LOG_FILE_SIZE = "vt.vlog.file.size";

public static final int DEFAULT_LOG_FILE_RETENTION = 7;
public static final int DEFAULT_LOG_FILE_SIZE = 0;

}
21 changes: 12 additions & 9 deletions src/main/java/vt/suopo/vlog/common/SystemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.Optional;

/**
* 系统工具
Expand All @@ -16,21 +17,23 @@
* All rights Reserved.
*/
public final class SystemUtils {

private SystemUtils() {}

/**
* @throws java.nio.file.InvalidPathException if a {@code Path} object cannot be constructed from the abstract path
* (see {@link java.nio.file.FileSystem#getPath FileSystem.getPath})
* @throws java.nio.file.InvalidPathException if a {@code Path} object cannot be constructed from the abstract
* path (see {@link java.nio.file.FileSystem#getPath FileSystem.getPath})
*/
public static Path getSysPropertyPath(String name) {
if (Objects.isNull(System.getProperty(name)) || System.getProperty(name).isEmpty()) {
return null;
}
return new File(System.getProperty(name)).toPath();
public static Optional<Path> getSysPropertyPath(String name) {
return Optional.ofNullable(System.getProperty(name)).map(path -> new File(System.getProperty(name)).toPath());
}

public static Optional<Boolean> getSysPropertyBool(String name) {
return Optional.ofNullable(System.getProperty(name)).map(Boolean::parseBoolean);
}

public static boolean getSysPropertyBool(String name) {
return Boolean.parseBoolean(System.getProperty(name));
public static Optional<Integer> getSysPropertyInteger(String name) {
return Optional.ofNullable(System.getProperty(name)).map(Integer::parseInt);
}

public static Path getSysTempDir() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/vt/suopo/vlog/common/ThrowableUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.LinkedList;
import java.util.List;

import static vt.suopo.vlog.common.LogConstants.CAUSED_BY;
import static vt.suopo.vlog.common.Constants.CAUSED_BY;

/**
* 异常处理工具类
Expand Down
Loading

0 comments on commit 13da347

Please sign in to comment.