springshell自定义Stacktrace命令

springshell工程如在国内环境时需要中文提示堆栈信息,可自定义Stacktrace命令:

import org.jline.terminal.Terminal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.shell.result.ThrowableResultHandler;
import org.springframework.shell.standard.ShellCommandGroup;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.commands.Stacktrace;

@ShellComponent
@ShellCommandGroup(value = "SpringShellDemo Commands") // 自定义分组名
public class StacktraceCommand implements Stacktrace.Command{

    public interface Command {}

    @Autowired
    @Lazy
    private Terminal terminal;

    @Autowired
    private ThrowableResultHandler throwableResultHandler;

    // 自定义提示信息
    @ShellMethod(key = ThrowableResultHandler.DETAILS_COMMAND_NAME, value = "Display the full stacktrace of the last error.")
    public void stacktrace() {
        if (throwableResultHandler.getLastError() != null) {
            throwableResultHandler.getLastError().printStackTrace(terminal.writer());
        }
    }
}

启动类中禁用springshell内置命令:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.util.StringUtils;

@SpringBootApplication
public class SpringShellDemoApplication {

    public static void main(String[] args) {String[] disabledCommands = {"--spring.shell.command.stacktrace.enabled=false"}; 
    String[] fullArgs
= StringUtils.concatenateStringArrays(args, disabledCommands); SpringApplication.run(SpringShellDemoApplication.class, fullArgs);
  }
}

 

posted @ 2022-06-06 01:24  刀霸汉  阅读(66)  评论(0编辑  收藏  举报