Skip to content

Commit

Permalink
feat: 日志输出格式优化,代码合理归类
Browse files Browse the repository at this point in the history
  • Loading branch information
i36lib committed Dec 11, 2024
1 parent 8612eae commit 430f691
Show file tree
Hide file tree
Showing 17 changed files with 36 additions and 478 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.artlibs.autotrace4j.logger.exception;
package io.github.artlibs.autotrace4j.exception;

/**
* 功能:Create Appender Exception
Expand All @@ -8,7 +8,7 @@
* <p>
* All rights Reserved.
*/
public class CreateAppenderException extends RuntimeException {
public final class CreateAppenderException extends RuntimeException {
/** Constructs a new runtime exception with {@code null} as its
* detail message. The cause is not initialized, and may subsequently be
* initialized by a call to {@link #initCause}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.artlibs.autotrace4j.support.exception;
package io.github.artlibs.autotrace4j.exception;

/**
* Unlock Method Exception
Expand All @@ -8,7 +8,7 @@
* <p>
* All rights Reserved.
*/
public class UnlockMethodException extends RuntimeException {
public final class UnlockMethodException extends RuntimeException {
/** Constructs a new runtime exception with {@code null} as its
* detail message. The cause is not initialized, and may subsequently be
* initialized by a call to {@link #initCause}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.artlibs.autotrace4j.support.exception;
package io.github.artlibs.autotrace4j.exception;

/**
* Walk Class File Exception
Expand All @@ -8,7 +8,7 @@
* <p>
* All rights Reserved.
*/
public class WalkClassFileException extends RuntimeException {
public final class WalkClassFileException extends RuntimeException {
/** Constructs a new runtime exception with {@code null} as its
* detail message. The cause is not initialized, and may subsequently be
* initialized by a call to {@link #initCause}.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

import static io.github.artlibs.autotrace4j.logger.LogConstants.*;
import static io.github.artlibs.autotrace4j.support.Constants.*;

/**
* 功能:日志工厂
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* <p>
* All rights Reserved.
*/
public class AppenderCombiner<E> extends AbstractAppender<E> {
public final class AppenderCombiner<E> extends AbstractAppender<E> {

private volatile List<Appender<E>> appenderList = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.github.artlibs.autotrace4j.logger.event.Level;
import io.github.artlibs.autotrace4j.logger.event.LogEvent;
import io.github.artlibs.autotrace4j.logger.exception.CreateAppenderException;
import io.github.artlibs.autotrace4j.exception.CreateAppenderException;
import io.github.artlibs.autotrace4j.logger.layout.Layout;

import java.io.PrintStream;
Expand All @@ -16,7 +16,7 @@
* <p>
* All rights Reserved.
*/
public class ConsoleAppender extends AsyncAppender<LogEvent> {
public final class ConsoleAppender extends AsyncAppender<LogEvent> {
Layout<LogEvent> layout;
private final PrintStream outPrintStream;
private final PrintStream errPrintStream;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.artlibs.autotrace4j.logger.appender;

import io.github.artlibs.autotrace4j.logger.event.LogEvent;
import io.github.artlibs.autotrace4j.logger.exception.CreateAppenderException;
import io.github.artlibs.autotrace4j.exception.CreateAppenderException;
import io.github.artlibs.autotrace4j.logger.layout.Layout;
import io.github.artlibs.autotrace4j.support.ThrowableUtils;
import io.github.artlibs.autotrace4j.support.Tuple2;
Expand Down Expand Up @@ -30,8 +30,7 @@
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import static io.github.artlibs.autotrace4j.logger.LogConstants.DEFAULT_LOG_FILE_RETENTION;
import static io.github.artlibs.autotrace4j.logger.LogConstants.DEFAULT_LOG_FILE_SIZE;
import static io.github.artlibs.autotrace4j.support.Constants.*;

/**
* 功能:默认日志输出-文件
Expand All @@ -44,7 +43,7 @@
* All rights Reserved.
*/
@SuppressWarnings({"resource", "UnusedReturnValue" })
public class FileAppender extends AsyncAppender<LogEvent> {
public final class FileAppender extends AsyncAppender<LogEvent> {

public static final int MIN_FILE_SIZE = 10 * 1024 * 1024;
private static final int WRITE_BUFFER_SIZE = 1024;
Expand Down Expand Up @@ -114,7 +113,7 @@ private int computeFileIndex(LocalDateTime date, boolean increment) throws IOExc
String todayFileNamePrefix = dateToLogFileName(date);
return Files.list(directory).filter(p -> p.toString().endsWith(FILE_SUFFIX))
.map(path -> {
String fileName = path.getFileName().toString().replace(FILE_SUFFIX, "");
String fileName = path.getFileName().toString().replace(FILE_SUFFIX, EMPTY);
int prefixIndex = fileName.lastIndexOf(todayFileNamePrefix);
if (prefixIndex != -1) {
if (fileName.length() == todayFileNamePrefix.length()) {
Expand Down Expand Up @@ -216,7 +215,7 @@ private ScheduledFuture<?> triggerCleanTask(LocalDateTime date) {
}

private Tuple2<Path, FileChannel> openLogFile(String logFileNamePrefix, int fileIndex) throws IOException {
Path path = directory.resolve(logFileNamePrefix + (fileIndex == 0 ? "" : "_" + fileIndex) + FILE_SUFFIX);
Path path = directory.resolve(logFileNamePrefix + (fileIndex == 0 ? EMPTY : "_" + fileIndex) + FILE_SUFFIX);
FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.APPEND, StandardOpenOption.CREATE);
return new Tuple2<>(path, fileChannel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* <p>
* All rights Reserved.
*/
public class DefaultLogEvent extends AbstractLogEvent {
public final class DefaultLogEvent extends AbstractLogEvent {

public DefaultLogEvent(Level level, String threadName, Long eventTime, Logger logger, String message, Object[] arguments) {
super(level, threadName, eventTime, logger, message, arguments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.time.ZoneId;
import java.util.Optional;

import static io.github.artlibs.autotrace4j.logger.LogConstants.*;
import static io.github.artlibs.autotrace4j.support.Constants.*;

/**
* 功能:默认日志格式化器
Expand All @@ -19,7 +19,7 @@
* <p>
* All rights Reserved.
*/
public class DefaultLayout implements Layout<LogEvent> {
public final class DefaultLayout implements Layout<LogEvent> {

public static final String UNDEFINE = "undefine";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.artlibs.autotrace4j.support;

import io.github.artlibs.autotrace4j.support.exception.WalkClassFileException;
import io.github.artlibs.autotrace4j.exception.WalkClassFileException;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.loading.ClassInjector;

Expand Down
26 changes: 13 additions & 13 deletions src/main/java/io/github/artlibs/autotrace4j/support/Constants.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package io.github.artlibs.autotrace4j.support;

import java.util.Objects;

/**
* Constants
* Constants definition
*
* @author Fury
* @since 2024-03-30
Expand All @@ -23,14 +21,16 @@ private Constants(){}
public static final String INJECT_DIR_BOOTSTRAP = "bootstrap";
public static final String INTERCEPT_METHOD_NAME = "intercept";

public static String concat(String source, String... appends) {
Objects.requireNonNull(source);
StringBuilder sb = new StringBuilder(appends.length)
.append(source);
for (String append : appends) {
sb.append(append);
}

return sb.toString();
}
/* --------------- logging support --------------- */
public static final String SPACE = " ";
public static final String LEFT_MIDDLE_BRACKET = "[";
public static final String RIGHT_MIDDLE_BRACKET = "]";
public static final String CAUSED_BY = "Caused by: ";
public static final String SYSTEM_PROPERTY_LOG_ENABLE = "autotrace4j.log.enable";
public static final String SYSTEM_PROPERTY_LOG_DIR = "autotrace4j.log.dir";
public static final String SYSTEM_PROPERTY_LOG_LEVEL = "autotrace4j.log.level";
public static final String SYSTEM_PROPERTY_LOG_FILE_RETENTION = "autotrace4j.log.file.retention";
public static final String SYSTEM_PROPERTY_LOG_FILE_SIZE = "autotrace4j.log.file.size";
public static final int DEFAULT_LOG_FILE_RETENTION = 7;
public static final int DEFAULT_LOG_FILE_SIZE = 0;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.artlibs.autotrace4j.support;

import io.github.artlibs.autotrace4j.support.exception.UnlockMethodException;
import io.github.artlibs.autotrace4j.exception.UnlockMethodException;
import net.bytebuddy.utility.JavaModule;

import java.lang.reflect.Field;
Expand Down
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 io.github.artlibs.autotrace4j.logger.LogConstants.CAUSED_BY;
import static io.github.artlibs.autotrace4j.support.Constants.CAUSED_BY;

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

0 comments on commit 430f691

Please sign in to comment.