Skip to content

Commit

Permalink
optimize: StackTraceLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbxyyx committed Mar 6, 2020
1 parent 917bfc7 commit 3e15b7b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
26 changes: 21 additions & 5 deletions core/src/main/java/io/seata/core/logger/StackTraceLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import io.seata.config.ConfigurationFactory;
import io.seata.core.constants.ConfigurationKeys;
import org.slf4j.Logger;

import static io.seata.core.constants.DefaultValues.DEFAULT_LOG_EXCEPTION_RATE;

/**
Expand All @@ -29,15 +28,32 @@ public final class StackTraceLogger {

private static final Configuration CONFIG = ConfigurationFactory.getInstance();

public static void info(Logger logger, Throwable cause, String format1, Object[] args1, String format2, Object[] args2) {
private static final String STACK_TRACE_LOGGER_PREFIX = "[stacktrace]";

public static void info(Logger logger, Throwable cause, String format, Object[] args) {
if (logger.isInfoEnabled()) {
int rate = CONFIG.getInt(ConfigurationKeys.TRANSACTION_LOG_EXCEPTION_RATE, DEFAULT_LOG_EXCEPTION_RATE);
int rate = getRate();
if (System.currentTimeMillis() % rate == 0) {
logger.info(STACK_TRACE_LOGGER_PREFIX + format, args, cause);
} else {
logger.info(format, args);
}
}
}

public static void warn(Logger logger, Throwable cause, String format, Object[] args) {
if (logger.isWarnEnabled()) {
int rate = getRate();
if (System.currentTimeMillis() % rate == 0) {
logger.info(format1, args1, cause);
logger.warn(STACK_TRACE_LOGGER_PREFIX + format, args, cause);
} else {
logger.info(format2, args2);
logger.warn(format, args);
}
}
}

private static int getRate() {
return CONFIG.getInt(ConfigurationKeys.TRANSACTION_LOG_EXCEPTION_RATE, DEFAULT_LOG_EXCEPTION_RATE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,8 @@ public BranchStatus branchRollback(BranchType branchType, String xid, long branc
UndoLogManagerFactory.getUndoLogManager(dataSourceProxy.getDbType()).undo(dataSourceProxy, xid, branchId);
} catch (TransactionException te) {
StackTraceLogger.info(LOGGER, te,
"[stacktrace]branchRollback failed. branchType:[{}], xid:[{}], branchId:[{}], resourceId:[{}], applicationData:[{}]. stacktrace:[{}]",
new Object[]{branchType, xid, branchId, resourceId, applicationData, te.getMessage()},
"branchRollback failed reason [{}]", new Object[]{te.getMessage()});
"branchRollback failed. branchType:[{}], xid:[{}], branchId:[{}], resourceId:[{}], applicationData:[{}]. reason:[{}]",
new Object[]{branchType, xid, branchId, resourceId, applicationData, te.getMessage()});
if (te.getCode() == TransactionExceptionCode.BranchRollbackFailed_Unretriable) {
return BranchStatus.PhaseTwo_RollbackFailed_Unretryable;
} else {
Expand Down

0 comments on commit 3e15b7b

Please sign in to comment.