'How to write a propper console handler in Java?

I'm trying to write a propper console handler for my Java application so that I can handle commands while the application is running in the background. My problem is, that I don't know how to put what the user wrote on the end of the console like you see in every other console application.

Imagine we have something like this

[24.02.2022 00:11:08 FINE]: Logger initialized!
[24.02.2022 00:11:08 INFO]: Logger initialized!
[24.02.2022 00:11:08 INFO]: Logger initialized! AGAIN
> 

Now I write for example "help" to get the menu from the console but in the same moment the console prints out something new and just puts the message behind what I just wrote, when I continue writing it looks all really messed up.

[24.02.2022 00:11:08 FINE]: Logger initialized!
[24.02.2022 00:11:08 INFO]: Logger initialized!
[24.02.2022 00:11:08 INFO]: Logger initialized! AGAIN
> he
[24.02.2022 00:11:08 INFO]: Logger initialized! AGAIN
> lp

Or I didn't sent the command yet. Of course I'm getting a smililar outcome.

Here my handler:

 @Override
    public void publish(LogRecord record) {
        System.out.print("\b\b");
        System.out.print(this.getFormatter().format(record));
        System.out.print(YukiLogFormatter.ANSI_RESET + "> ");
    }

The "\b\b" is to remove the "> " which should indicate that you can write a command here.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source