BoxLogger Tutorial
This tutorial provides an overview of the BoxLogger
class, which is part of the lightweight-component/aj-util
library. The BoxLogger
class provides utilities for creating boxed log messages with proper formatting and alignment.
Introduction
The BoxLogger
class contains static methods for creating visually formatted log messages in boxes, with support for ANSI colors and proper handling of wide characters (like CJK).
Main Features
- Boxed log message formatting
- ANSI color support
- String truncation with ellipsis
- Display width calculation (handling wide characters)
- String repetition utility
Constants
ANSI_GREEN
- ANSI green color codeANSI_YELLOW
- ANSI yellow color codeANSI_BLUE
- ANSI blue color codeANSI_RED
- ANSI red color codeANSI_RESET
- ANSI reset codeNONE
- Constant "none"TRACE_KEY
- Default trace key nameBIZ_ACTION
- Default business action key name
Methods
1. Box Formatting
boxLine(char left, char fill, char right, String title)
- Create a box border lineboxContent(String key, String value)
- Create a content line within box
2. String Utilities
repeat(char c, int n)
- Repeat character n timestruncate(String s, int maxDisplayLen)
- Truncate string with ellipsisgetDisplayWidth(String s)
- Calculate display width (wide chars = 2)isWideChar(char ch)
- Check if character is wide (CJK)
Usage Examples
Create Boxed Log
System.out.println(BoxLogger.boxLine('┌', '─', '┐', " LOG START "));
System.out.println(BoxLogger.boxContent("User: ", "张三"));
System.out.println(BoxLogger.boxLine('└', '─', '┘', " LOG END "));
ANSI Colors
System.out.println(BoxLogger.ANSI_RED + "Error!" + BoxLogger.ANSI_RESET);
String Utilities
String repeated = BoxLogger.repeat('-', 10); // "----------"
String truncated = BoxLogger.truncate("This is a long string", 10); // "This is..."
int width = BoxLogger.getDisplayWidth("中文"); // 4
Implementation Notes
- Box width is fixed at 97 characters
- Properly handles CJK and fullwidth characters
- Includes basic ANSI color support
- Provides clean truncation with ellipsis
Conclusion
The BoxLogger
class provides useful utilities for creating visually formatted log messages, especially useful for CLI applications or debugging output.