9. Customization
This section describes how you can customize the shell.
9.1. Theming
Current terminal implementations are rich in features and can usually show something else that just plain text. For example a text can be styled to be bold or have different colors. It’s also common for terminals to be able to show various characters from an unicode table like emoji’s which are usually used to make shell output more pretty.
Spring Shell supports these via it’s theming framework which contains two parts, firstly styling can be used to change text type and secondly figures how some characters are shown. These two are then combined together as a theme.
More about theming internals, see Theming.
Default theme is named default but can be change using property
spring.shell.theme.name . Other built-in theme named dump uses
no styling for colors and tries to not use any special figures.
|
Modify existing style by overriding settings.
static class MyStyleSettings extends StyleSettings {
@Override
public String highlight() {
return super.highlight();
}
}
Modify existing figures by overriding settings.
static class MyFigureSettings extends FigureSettings {
@Override
public String error() {
return super.error();
}
}
To create a new theme, create a ThemeSettings
and provide your own style
and figure implementations.
static class MyThemeSettings extends ThemeSettings {
@Override
public StyleSettings styles() {
return new MyStyleSettings();
}
@Override
public FigureSettings figures() {
return new MyFigureSettings();
}
}
Register a new bean Theme
where you can return your custom ThemeSettings
and a theme name.
@Configuration
static class CustomThemeConfig {
@Bean
Theme myTheme() {
return new Theme() {
@Override
public String getName() {
return "mytheme";
}
@Override
public ThemeSettings getSettings() {
return new MyThemeSettings();
}
};
}
}
You can use ThemeResolver
to resolve styles if you want to create
JLine-styled strings programmatically and figures if you want to
theme characters for being more pretty.
@Autowired
private ThemeResolver resolver;
void resolve() {
String resolvedStyle = resolver.resolveStyleTag(StyleSettings.TAG_TITLE);
// bold,fg:bright-white
AttributedStyle style = resolver.resolveStyle(resolvedStyle);
// jline attributed style from expression above
String resolvedFigure = resolver.resolveFigureTag(FigureSettings.TAG_ERROR);
// character i.e. U+2716 Heavy Multiplication X Emoji, cross
}
9.2. Logging
On default a Spring Boot application will log messages into a console which at minimum is annoying and may also mix output from a shell commands. Fortunately there is a simple way to instruct logging changes via boot properties.
Completely silence console logging by defining its pattern as an empty value.
logging:
pattern:
console:
If you need log from a shell then write those into a file.
logging:
file:
name: shell.log
If you need different log levels.
logging:
level:
org:
springframework:
shell: debug
Passing contiguration properties as command line options is not supported but you can use any other ways supported by boot, for example.
$ java -Dlogging.level.root=debug -jar demo.jar
$ LOGGING_LEVEL_ROOT=debug java -jar demo.jar
In a GraalVM image settings are locked during compilation which means you can’t change log levels at runtime. |