You could use org.apache.commons.lang.StringUtils.replace(String text,
String repl, String with) or the following:

   public static String Replace(String original, String from, String to) {
       if(original == null) {
           original = "";
       }
       if(from == null) {
           from = "";
       }
       if(to == null) {
           to = "";
       }
       String replaced = original;
       String start;
                                                           // look
for the next from after the last replacement
       for(int index = replaced.indexOf(from); index >= 0; index =
replaced.indexOf(from, index + to.length())) {

           start = replaced.substring(0, index) + to;
           if(index + from.length() + 1 <= replaced.length()) {//
There is something after replaced piece
               replaced = start + replaced.substring(index + from.length());
           } else {
               replaced = start;
           }
       }
       return replaced;
   }

On 7/18/06, Roberto Giménez <[EMAIL PROTECTED]> wrote:
Yes those are good ideas. Unfortunatelly in jdk 1.3.1 the class String
doesn't have the method "replaceAll" as in 1.4.x and 1.5.x. It only
has replace(char oldChar,
char newChar), so i'll have to use my getFormattedString method anyway.
As for the first 3 sugestions, I don't think using the PatternLayout as an
utilitary for obtaining the prefix and the stacktrace is a bad idea, but I
doubt that it is conceptually correct. I mean, the PatternLayout is meant to
be used as a Layout not as an utilitary class. In my first solution I kinda
used it for that purpose, and I didn't like that much either.


2006/7/18, James Stauffer <[EMAIL PROTECTED]>:
>
> Why can't you just do this:
> Grab the pattern and remove the %m.
> Pass that to PatternLayout to convert to the prefix String.
> Pass the %m to PatternLayout to get the stack trace String.
> Put the pieces together (prefix + stack.replaceAll("\r\n", "\r\n" +
> prefix)
>
> On 7/18/06, Roberto Giménez <[EMAIL PROTECTED]> wrote:
> > Now I have done what I said:
> >
> >     /** Establece el patrón. */
> >     protected String getPrefix()
> >     {
> >         if (indent == -1)
> >             return "";
> >
> >         long currTime = System.currentTimeMillis() / 1000;
> >         if (timePrefixLastTimeSet != currTime)
> >         {
> >             GregorianCalendar calendar = new GregorianCalendar();
> >             int hour = calendar.get(Calendar.HOUR_OF_DAY);
> >             int minutes = calendar.get(Calendar.MINUTE);
> >             int seconds = calendar.get(Calendar.SECOND);
> >             timePrefix = "[" +  (hour < 10 ? "0" : "") + hour + ":" +
> > (minutes < 10 ? "0" : "") + minutes + ":" + (seconds < 10 ? "0" : "") +
> > seconds + "]";
> >
> >             timePrefixLastTimeSet = currTime;
> >         }
> >
> >         String indentString = "";
> >         for (int i = 0; i < indent; i++, indentString += "\t");
> >
> >         return timePrefix + "[" + threadName + "] " + indentString;
> >     }
> >
> >     /* (non-Javadoc)
> >      * @see org.apache.log4j.Layout#format(
> org.apache.log4j.spi.LoggingEvent
> > )
> >      */
> >     public String format(LoggingEvent event)
> >     {
> >         String message = (String)event.getMessage();
> >         String ret = getFormattedString(message);
> >
> >         if (event.getThrowableInformation() != null)
> >         {
> >             Throwable throwable = event.getThrowableInformation
> > ().getThrowable();
> >
> >             ByteArrayOutputStream os = new ByteArrayOutputStream();
> >             PrintStream ps = new PrintStream(os);
> >
> >             throwable.printStackTrace(ps);
> >
> >             ret += getFormattedString(os.toString());
> >         }
> >
> >         return ret;
> >     }
> >
> >     /** Formatea 'message' y retorna la cadena formateada.
> >      * @param message    La cadena a formatear.
> >      * @return La cadena formateada.
> >      */
> >     String getFormattedString(String message)
> >     {
> >         String prefix = getPrefix();
> >         String ret = prefix;
> >
> >         for (int i = 0; i < message.length(); i++)
> >         {
> >             ret += message.charAt(i);
> >             switch(message.charAt(i))
> >             {
> >                 case '\n':
> >                 case '\r':
> >                     ret += prefix;
> >             }
> >         }
> >
> >         return ret + "\n";
> >     }
> >
> > Does the same thing but is far more efficient than my previous solution.
> > What I don't like is that I have to create a GregorianCalendar each time
> I
> > want to update my prefix, since it contains the current time. I do that
> when
> > the current time and the last format time differs by a second. I know
> this
> > is not log4j related, but I'll ask anyway. Is there a way around this?
> >
> > 2006/7/18, Roberto Giménez <[EMAIL PROTECTED]>:
> > >
> > > I am calling an encapsulated layout, it is a PatternLayout. The
> special
> > > case of empty message but throwable information is not covered, i'll
> fix
> > > that, thanks.
> > > The use of a StringTokenizer for every single line is not efficient at
> > > all. Retrieving the Logger probably isn't either, due the table
> lookup.
> > > About the recursion... well... I don't think that the time and memory
> it
> > > consumes is very significative.
> > >
> > > But I think that the creation and use of the StringTokenizer for every
> > > single line, the creation of LoggingEvents for every token line, and
> the
> > > Logger lookup are ineficient methods. I think that what I have to do
> is to
> > > create the output string by begining it with the prefix, and then
> copying
> > > each character of the message and everytime I find a nl/cr character
> I'll
> > > just place the prefix before the next character to be copied. I can
> return
> > > this string and avoid the creation of another logging event since I
> don't
> > > use any of the other members contained in the event. I'll have to
> create the
> > > prefix by hand thou, but the prefix is not so complex anyway. I'm
> using
> > > jdk1.3.1 and here Throwables cant return the stack frames, that's why
> I
> > > have to print the stacktrace into a ByteArrayOutputStream, this is
> also
> > > inneficient but there's nothing I can do about it.
> > >
> > >
> > > 2006/7/18, David Tonhofer <[EMAIL PROTECTED]>:
> > >
> > > > Roberto Giménez wrote:
> > > > > I already have a custom layout that contains a PatternLayout
> inside
> > > > that
> > > > > does the formatting. But now I have modified the format method as
> you
> > > > > told
> > > > > me so it will do the line breakup instead of writting a custom
> logger.
> > > >
> > > > > Here's the code.
> > > > >
> > > >
> > > > <...>
> > > > > public String format(LoggingEvent event)
> > > > > {
> > > > > String message = (String)event.getMessage();
> > > > > String ret = "";
> > > > >
> > > > > if (message.length () == 0)
> > > > // CALL ENCAPSULATED LAYOUT
> > > > > return layout.format(event);
> > > > >
> > > > > StringTokenizer tokenizer = new StringTokenizer(message, "\n\r");
> > > > > while(tokenizer.hasMoreElements())
> > > > > {
> > > > > LoggingEvent e = new LoggingEvent
> > > > > (
> > > > > event.fqnOfCategoryClass,
> > > > > Logger.getLogger(event.getLoggerName()),
> > > > > event.timeStamp,
> > > > > event.getLevel(),
> > > > > tokenizer.nextToken(),
> > > > > null
> > > > > );
> > > > >
> > > > // CALL ENCAPSULATED LAYOUT
> > > > > ret += layout.format(e);
> > > > > }
> > > > >
> > > > > if (event.getThrowableInformation() != null)
> > > > > {
> > > > > Throwable throwable = event.getThrowableInformation
> > > > > ().getThrowable();
> > > > >
> > > > > ByteArrayOutputStream os = new ByteArrayOutputStream();
> > > > > PrintStream ps = new PrintStream(os);
> > > > >
> > > > > throwable.printStackTrace(ps);
> > > > >
> > > > // RECURSIVE CALL
> > > > > ret += format(new LoggingEvent
> > > > > (
> > > > > event.fqnOfCategoryClass,
> > > > > Logger.getLogger(event.getLoggerName()),
> > > > > event.timeStamp,
> > > > > event.getLevel(),
> > > > > os.toString(),
> > > > > null
> > > > > ));
> > > > > }
> > > > >
> > > > > return ret;
> > > > > }
> > > > >
> > > > > /* (non-Javadoc)
> > > > > * @see org.apache.log4j.Layout#ignoresThrowable()
> > > > > */
> > > > > public boolean ignoresThrowable()
> > > > > {
> > > > > return false;
> > > > > }
> > > > >
> > > > > This code works fine so far, it performs the line breakup and
> formats
> > > > > each
> > > > > line using my pattern layout, both for normal text as for the
> > > > > stacktrace of
> > > > > throwables. I have doubts thou on whether this is the correct way
> to
> > > > > do it.
> > > > >
> > > >
> > > > Looks ok, you are calling an encapsulated layout, right? The special
> > > > case of
> > > > empyt message but throwable information is not correctly covered
> though.
> > > >
> > > > One could maybe nitpick about the efficiency of using a
> StringTokenizer
> > > > every time
> > > > and retrieving the Logger at each line and the fact that you call
> > > > recursively
> > > > instead of just processing right there .. :-=
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > > For additional commands, e-mail: [EMAIL PROTECTED]
> > > >
> > > >
> > >
> >
> >
>
>
> --
> James Stauffer
> Are you good? Take the test at http://www.livingwaters.com/good/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>




--
James Stauffer
Are you good? Take the test at http://www.livingwaters.com/good/

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to