Le Touilleur Express

  • Accueil
  • A propos de l’auteur
  • A propos du Touilleur Express
Next Previous

Log4J and ANSI colors in a xterm

4 août, 2005

The product I am currently developing is a unix server for market datas. Log4J is being use for trace and debug. If you also use Log4J in your Java program, and your program generates traces in a term, this might interest you. I wrote a simple Log4J appender that generates ANSI colors for any Log4J trace. If your application outputs a DEBUG message, a green string message is outputed to the xterm. This appender doesn’t work under Windows 2000 and NT4. It might be interesting for any console java application such as JBoss if your xterm has support for colors. Feel free to use it but don’t forget to send me credits if you include-it in your apps.

/**
* Copyright (c) 2004-2008 Nicolas Martignole
* All rights reserved
* http://www.jroller.com/page/Trecollo/Weblog
* http://www.touilleur-express.fr
* Created by : Nicolas Martignole
* Date: 13 oct. 2004 Time: 15:12:23
*/
package com.reuters.pds.common.sdk.util;

import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.WriterAppender;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.ThrowableInformation;

/**
 * ANSIColorAppender is a Log4J Appender that output LoggingEvent messages using ANSI colors.
 *
 * Each level (DEBUG,INFO,WARN) has its own color that you can customize.
 * To use this file, in your log4j.properties or log4j.xml specify ANSIColorAppender
 * instead of a ConsoleAppender.
 *
 * @author Nicolas Martignole (nicolas_at_martignole_dot_net)
 * @version 13 oct. 2004
 */
public class ANSIColorAppender extends WriterAppender {
    /**
     * Method from Log4j AppenderSkeleton that gets call for any Log4J events.
     *
     * @param event
     * @see org.apache.log4j.AppenderSkeleton
     */
    public void append(LoggingEvent event) {
        System.out.print(colorizeToANSI(layout.format(event), event.getLevel(), event.getThrowableInformation()));
    }

    /**
     * Requires a layout
     *
     * @return true
     */
    public boolean requiresLayout() {
        return true;
    }

    /**
     * This method overrides the parent {@link WriterAppender#closeWriter}
     * implementation to do nothing because the console stream is not ours to close.
     */
    protected final void closeWriter() {
    }

    /**
     * @param layout
     */
    public void setLayout(Layout layout) {
        super.setLayout(layout);
    }

    /**
     * Colorizes the specified message for the specified log4j level.
     * To get a list of available colors see url http://pueblo.sourceforge.net/doc/manual/ansi_color_codes.html.
     */
    private String colorizeToANSI(Object message, Level level, ThrowableInformation ti) {
        StringBuffer oBuffer = new StringBuffer();
        switch (level.toInt()) {
            case Level.ALL_INT:
                oBuffer.append("\u001b[1m\u001b[30m");
                break;
            case Level.FATAL_INT:
                oBuffer.append("\u001b[1m\u001b[31m");
                break;
            case Level.ERROR_INT:
                oBuffer.append("\u001b[31m");
                break;
            case Level.WARN_INT:
                oBuffer.append("\u001b[35m");
                break;
            case Level.INFO_INT:
                oBuffer.append("\u001b[34m");
                break;
            case Level.DEBUG_INT:
                oBuffer.append("\u001b[32m");
                break;// dark green
        }
        oBuffer.append(message);
        oBuffer.append("\u001b[0m");

        // Print in red the whole exception stack trace
        if (ti != null) {
            String s[] = ti.getThrowableStrRep();
            for (int i = 0; i < s.length; i++) {
                oBuffer.append("\n\u001b[31m");
                oBuffer.append(s[i]);
                oBuffer.append("\u001b[0m");
            }
        }
        oBuffer.append("\u001b[0m");
        return oBuffer.toString();
    }

}
0 no like

Articles similaires:

Default ThumbnailAdieu System.out.println, vive Log4J Default ThumbnailByteArrayOutputStream: vive les buffers de taille variable Default ThumbnailLog4J et log4j.xml Default ThumbnailLog4J etait capable de recharger un fichier de conf XML automatiquement

Derniers articles

  • Les outils de génération de code, la couche confort du développeur ?

    Il est 9h35, tu sirotes tranquillement ton café devant ton IDE, et

    5 likes

    18 juin, 2025
  • L’instant T où tu poses ta dém…

    Retour d’expérience sur la démission et le moment où vous devez quitter une entreprise.

    7 likes

    24 octobre, 2024
  • The « Robinson » projection – comprendre son système d’information

    Nous sommes en juillet 2022 chez Doctolib. Je travaille sur un projet

    5 likes

    22 octobre, 2024

Mots clés

Apple (32) Architecture (14) Big Data (5) Conference (8) Devoxx (55) Dev Web (37) Doctolib (2) geekevent (1) groovy (2) Innoteria (11) Java (517) Linux (10) Non classé (16) Perso (266) Recrutement (2) Scala (30) scrum (43) Société (3) Staff Engineer (5) Startup (21) Web 2.0 (67)

Le Touilleur Express

Blog par Nicolas Martignole

Contactez-moi : nicolas@touilleur-express.fr

Suivez-moi sur X (Twitter) : @nmartignole

Copyright© 2008 - 2024 Nicolas Martignole | Tous droits réservés
  • A propos de l’auteur
  • A propos du Touilleur Express
  • Reset Password

Le Touilleur Express