Skip to content

Question: How can we use the connection.write using Picocli ? #87

@cmoulliard

Description

@cmoulliard

Question

How can we use the Terminal connection.write using Picocli to log messages ?

As you can see hereafter, Picocli can log easily the messages using the following stdout and printwriter method: spec.commandLine().getOut().println().

They also offer an Ansi() builder method able from a colorScheme to colorize the text colorScheme.ansi().new Text("#" + message,colorScheme) BUT their colorScheme has been designed around commands and options colors as you can see hereafter : https://github.com/remkop/picocli/blob/main/src/main/java/picocli/CommandLine.java#L17540-L17568 and then is not well suited to format/colorize logging message

package dev.snowdrop.logging;

import dev.snowdrop.logging.util.AnsiBuilder;
import dev.snowdrop.logging.util.LEVEL;
import dev.snowdrop.service.MessageService;
import jakarta.enterprise.context.ApplicationScoped;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger;
import picocli.CommandLine;

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

@ApplicationScoped
public class LoggingService {
    private static final Logger logger = Logger.getLogger(MessageService.class);

    private CommandLine.Model.CommandSpec spec;

    @ConfigProperty(name = "cli.mode", defaultValue = "false")
    boolean isCliMode;

    @ConfigProperty(name = "cli.logging.verbose", defaultValue = "false")
    boolean isVerbose;

    @ConfigProperty(name = "cli.logging.colored", defaultValue = "true")
    boolean useAnsiColoredMsg;
    
    private static String TIMESTAMP_COLOR = "TIMESTAMP";
    private static String MESSAGE_COLOR = "MESSAGE";

    CommandLine.Help.ColorScheme colorScheme;

    public LoggingService() {
    }

    public void info(String message) {
        // TODO: To be investigate to see if this is easier to use picocli Ansi and the colorScheme = spec.commandLine().getColorScheme();
        if (isCliMode) {
            //spec.commandLine().getOut().println(colorScheme.ansi().new Text("#" + message,colorScheme));
            spec.commandLine().getOut().println(formatedMessage(LEVEL.INFO, message));
        } else {
            logger.info(message);
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions