Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java logger with different colors

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;

public class LogFormatter extends Formatter
{
    // ANSI escape code
    public static final String ANSI_RESET = "u001B[0m";
    public static final String ANSI_BLACK = "u001B[30m";
    public static final String ANSI_RED = "u001B[31m";
    public static final String ANSI_GREEN = "u001B[32m";
    public static final String ANSI_YELLOW = "u001B[33m";
    public static final String ANSI_BLUE = "u001B[34m";
    public static final String ANSI_PURPLE = "u001B[35m";
    public static final String ANSI_CYAN = "u001B[36m";
    public static final String ANSI_WHITE = "u001B[37m";

    // Here you can configure the format of the output and 
    // its color by using the ANSI escape codes defined above.

    // format is called for every console log message
    @Override
    public String format(LogRecord record)
    {
        // This example will print date/time, class, and log level in yellow,
        // followed by the log message and it's parameters in white .
        StringBuilder builder = new StringBuilder();
        builder.append(ANSI_YELLOW);

        builder.append("[");
        builder.append(calcDate(record.getMillis()));
        builder.append("]");

        builder.append(" [");
        builder.append(record.getSourceClassName());
        builder.append("]");

        builder.append(" [");
        builder.append(record.getLevel().getName());
        builder.append("]");

        builder.append(ANSI_WHITE);
        builder.append(" - ");
        builder.append(record.getMessage());

        Object[] params = record.getParameters();

        if (params != null)
        {
            builder.append("	");
            for (int i = 0; i < params.length; i++)
            {
                builder.append(params[i]);
                if (i < params.length - 1)
                    builder.append(", ");
            }
        }

        builder.append(ANSI_RESET);
        builder.append("
");
        return builder.toString();
    }

    private String calcDate(long millisecs) {
        SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date resultdate = new Date(millisecs);
        return date_format.format(resultdate);
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java repository sql find not in list 
Java :: how to reverse a string in java without function 
Java :: getcolor deprecated android 
Java :: how to clear text fields in java 
Java :: java regex case insensitive 
Java :: linear search java 
Java :: java get attributes from class 
Java :: Java How to use SortedSet? 
Java :: how to find palindrome numbers in java 
Java :: Binary tree using linked list in Java 
Java :: how to save a string to a text file 
Java :: reviews button ade android studio 
Java :: center textview programmatically android 
Java :: arrays.copy 2d array 
Java :: esponente in java 
Java :: Add month to date in java 8 
Java :: System.out.println("Hello world") 
Java :: read stream java 
Java :: rock paper scissor in java 
Java :: richest customer wealth leetcode 
Java :: do...while loop Java 
Java :: java explicit array declaration 
Java :: current time stamp android java 
Java :: button onclick java 
Java :: java string from byte array 
Java :: java math ceil 
Java :: android studio copy file 
Java :: spring 5 jdbctemplate query for a single value 
Java :: how to read text file as variable in java 
Java :: uninstall java 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =