系统相关
首页 > 系统相关> > springshell自定义Stacktrace命令

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);
  }
}

 

标签:Stacktrace,shell,自定义,springframework,import,springshell,org,public
来源: https://www.cnblogs.com/chencoolandclear/p/16345782.html