You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by Thomas NGUYEN <th...@openwide.fr> on 2003/05/14 16:31:08 UTC

Colored console output

I modified src/java/org/apache/log4j/helpers/PatternParser.java to add a
little feature: the priority name appears colored in the console.

I added a new conversion character named 'P' ('p' prints the priority,
so 'P' printing an enhanced priority seemed to make sense)

I made this because console logs are rather hard to read. I fill that
some color is welcome.

I haven't tested it extensively, but it looks like it works well.

Any feedback welcome.

-- Thomas NGUYEN


PS: If you don't find it usefull, at least have been having some fun
playing with log4j. ;^)


Re: Colored console output

Posted by Ceki Gülcü <ce...@qos.ch>.
This is a great idea.

One of the stated goals for log4j 1.3 is to allow PatternLayout to be 
extensible with new conversion specifiers (as in P) on the fly. For the 
time being, I would recommend that you enter a log4j enhancement request 
into bugzilla http://nagoya.apache.org/bugzilla/ so that your patch is not 
forgotten.

At 04:31 PM 5/14/2003 +0200, you wrote:

>I modified src/java/org/apache/log4j/helpers/PatternParser.java to add a
>little feature: the priority name appears colored in the console.
>
>I added a new conversion character named 'P' ('p' prints the priority,
>so 'P' printing an enhanced priority seemed to make sense)
>
>I made this because console logs are rather hard to read. I fill that
>some color is welcome.
>
>I haven't tested it extensively, but it looks like it works well.
>
>Any feedback welcome.
>
>-- Thomas NGUYEN
>
>
>PS: If you don't find it usefull, at least have been having some fun
>playing with log4j. ;^)
>
>Index: src/java/org/apache/log4j/helpers/PatternParser.java
>===================================================================
>RCS file: 
>/home/cvspublic/jakarta-log4j/src/java/org/apache/log4j/helpers/PatternParser.java,v
>retrieving revision 1.19
>diff -r1.19 PatternParser.java
>13a14
> > import org.apache.log4j.Level;
>58a60
> >   static final int LEVEL_CONVERTER_COLORED = 2005;
>314a317,322
> >     case 'P':
> >       pc = new ColoredLevelPatternConverter(formattingInfo, 
> LEVEL_CONVERTER_COLORED);
> >       //LogLog.debug("COLORED_LEVEL converter.");
> >       //formattingInfo.dump();
> >       currentLiteral.setLength(0);
> >       break;
>403a412,454
> >   private static class ColoredLevelPatternConverter extends 
> PatternConverter {
> >       private static final String RED = "\033[31m";
> >       private static final String GREEN = "\033[32m";
> >       private static final String CYAN = "\033[36m";
> >       private static final String YELLOW = "\033[33m";
> >       private static final String WHITE = "\033[37m";
> >       private static final String NORMAL = "\033[0m";
> >
> >       ColoredLevelPatternConverter(FormattingInfo formattinginfo, int 
> type) {
> >         super(formattinginfo);
> >       }
> >
> >       public String convert(LoggingEvent event)
> >       {
> >         StringBuffer sbuf = new StringBuffer();
> >         Level lev = event.getLevel();
> >
> >         sbuf.append(getColor(lev));
> >         sbuf.append(lev.toString());
> >         sbuf.append(NORMAL);
> >
> >         return sbuf.toString();
> >       }
> >
> >       private String getColor(Level level)
> >       {
> >         if (level.isGreaterOrEqual(Level.FATAL))
> >             return RED;
> >         if (level.isGreaterOrEqual(Level.ERROR))
> >             return RED;
> >         if (level.isGreaterOrEqual(Level.WARN))
> >             return YELLOW;
> >         if (level.isGreaterOrEqual(Level.INFO))
> >             return GREEN;
> >         if (level.isGreaterOrEqual(Level.DEBUG))
> >             return CYAN;
> >         else
> >             return WHITE;
> >         //DEBUG, INFO, WARN, ERROR and FATAL
> >       }
> >
> >   }
> >
>566a618
> >
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: log4j-dev-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: log4j-dev-help@jakarta.apache.org

--
Ceki  For log4j documentation consider "The complete log4j manual"
       http://www.qos.ch/shop/products/clm_t.jsp 


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-dev-help@jakarta.apache.org