Is it possible for Log4j to change the text color in the System.out console for individual levels? For example, by the principle of LogCat (info = blue, warning = yellow, error = red)?
2 answers
There are 2 ways:
- If the console is an ANSI compatible terminal then you can encode ESC sequences of message color - for example, see here
- If the console is not as such (as under Windows), then you can direct the output to ChainSaw and there it’s ugly to distort with colors.
|
... System.out.println(colorText("Any text", "BLUE")); ... private static String colorText(String text, String color) { int x = color.equals("RED") ? 31 : color.equals("GREEN") ? 32 : color.equals("YELLOW") ? 33 : color.equals("BLUE") ? 34 : color.equals("MAGENTA") ? 35 : color.equals("CYAN") ? 36 : color.equals("WHITE") ? 37 : color.equals("BLACK") ? 30 : color.equals("BRIGHT") ? 1 : 0; return (char) 27 + "[" + x + "m" + text + (char) 27 + "[0m"; }
- First, you forgot to check the TERM environment variable before displaying ESC sequences. And this is what led to it: i.stack.imgur.com/d6qLh.png - Pavel Mayorov
- Secondly, I don’t see any mention of log4j in your answer ... - Pavel Mayorov
- I took the answer from here toster.ru/q/22091 - Mikhail Kolomiets
- Does it somehow justify the provision of incorrect information? .. - Pavel Mayorov
- Please try to write more detailed answers. I am sure the author of the question would be grateful for your expert commentary on the code above. - Nicolas Chabanovsky ♦
|