[logback-user] patternlayout question
David Tkaczyk
djt69 at comcast.net
Thu Mar 10 22:45:15 CET 2011
Thanks for the information...
I wrote a StatusListener that I thought I would share, but it isn't very
elegant and it wasn't as friendly/easy as I think it could be given that I
had to copy a bunch of code to do it. All just to change the date formatter
that was used. I'll post the code below if anyone is interested or has any
other ideas about how to make this a bit nicer.
Also, while reading through the code I noticed that the use of
java.text.SimpleDateFormat in ch.qos.logback.core.util.StatusPrinter does
not appear to be thread-safe. The javadoc indicates that external sync is
necessary. ch.qos.logback.core.status.OnConsoleListener certainly doesn't
sync before calling it. Does this synchronization happen elsewhere or is
this a bug?
Thanks,
Dave
package camiant.util.slf;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.helpers.ThrowableToStringArray;
import ch.qos.logback.core.status.Status;
import ch.qos.logback.core.status.StatusListener;
public class TestMyListener implements StatusListener {
private static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd
HH:mm:ss.SSS";
@Override
public void addStatusEvent(Status status) {
StringBuilder builder = new StringBuilder();
buildStr(builder, "", status);
System.out.print(builder.toString());
}
private static void buildStr(StringBuilder sb, String indentation, Status
s) {
String prefix;
if (s.hasChildren()) {
prefix = indentation + "+ ";
} else {
prefix = indentation + "|-";
}
SimpleDateFormat simpleDateFormat = new
SimpleDateFormat(DATE_FORMAT_PATTERN);
if (simpleDateFormat != null) {
Date date = new Date(s.getDate());
String dateStr = simpleDateFormat.format(date);
sb.append(dateStr).append(" ");
}
sb.append(prefix).append(s).append(CoreConstants.LINE_SEPARATOR);
if (s.getThrowable() != null) {
appendThrowable(sb, s.getThrowable());
}
if (s.hasChildren()) {
Iterator<Status> ite = s.iterator();
while (ite.hasNext()) {
Status child = ite.next();
buildStr(sb, indentation + " ", child);
}
}
}
private static void appendThrowable(StringBuilder sb, Throwable t) {
String[] stringRep = ThrowableToStringArray.convert(t);
for (String s : stringRep) {
if (s.startsWith(CoreConstants.CAUSED_BY)) {
// nothing
} else if (Character.isDigit(s.charAt(0))) {
// if line resembles "48 common frames omitted"
sb.append("\t... ");
} else {
// most of the time. just add a tab+"at"
sb.append("\tat ");
}
sb.append(s).append(CoreConstants.LINE_SEPARATOR);
}
}
}
-----Original Message-----
From: logback-user-bounces at qos.ch [mailto:logback-user-bounces at qos.ch] On
Behalf Of Ceki Gülcü
Sent: Thursday, March 10, 2011 2:41 PM
To: logback users list
Subject: Re: [logback-user] patternlayout question
On 10/03/2011 8:29 PM, David Tkaczyk wrote:
> Is there a way to modify the pattern that is used by the logback runtime
> when it is logging messages during startup or configuration change. i.e.
> the messages that show up on the console when you include
> <statusListener
> class=ch.qos.logback.core.status.OnConsoleStatusListener /> in your
> configuration file. Id like to have them match the pattern that I use
> for every other log message.
>
> Without a DTD or XSD it is hard to tell if I can do this via
> configuration XML. It is also not evident if I need to extend a class or
> implement a listener to implement this behavior on my own.
>
> Im using 0.9.26
..
>
> Thanks,
> Dave
Hello Dave,
The output of OnConsoleStatusListener cannot be changed. You need to
write your own implementation and register it instead of
OnConsoleStatusListener.
HTH,
--
QOS.ch, main sponsor of cal10n, logback and slf4j open source projects,
is looking to hire talented software developers. For further details,
see http://logback.qos.ch/job.html
_______________________________________________
Logback-user mailing list
Logback-user at qos.ch
http://qos.ch/mailman/listinfo/logback-user
More information about the Logback-user
mailing list