Skip to content

Commit

Permalink
[JENKINS-30117] Support bundle blocks with lots of data
Browse files Browse the repository at this point in the history
The format method should be thread safe as it could be invoked from
multiple threads. Instead of using a MessageFormatter which is not thread safe we
use the FastDateFormatter, and a StringBuffer both of which are thread
safe.
  • Loading branch information
christ66 committed Nov 11, 2015
1 parent 5e5cfff commit 666dd1d
Showing 1 changed file with 36 additions and 19 deletions.
Expand Up @@ -24,6 +24,8 @@

package com.cloudbees.jenkins.support;

import org.apache.commons.lang.time.FastDateFormat;

This comment has been minimized.

Copy link
@jtnord

jtnord Jan 26, 2016

Member

causes a deadlock with cli due to remote class loading during logging.


import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.MessageFormat;
Expand All @@ -37,42 +39,57 @@
* @author Stephen Connolly
*/
public class SupportLogFormatter extends Formatter {
private final Date date = new Date();
private final MessageFormat formatter =
new MessageFormat("{0,date,yyyy-MM-dd} {0,time,HH:mm:ss.SSSZ} {1}\t{2}\t{3}: {4}\n");
FastDateFormat fdf = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.SSSZ");
private final Object[] args = new Object[6];

@Override
@edu.umd.cs.findbugs.annotations.SuppressWarnings(
value = {"DE_MIGHT_IGNORE"},
justification = "The exception wasn't thrown on our stack frame"
)
public synchronized String format(LogRecord record) {
date.setTime(record.getMillis());
args[0] = date;
args[1] = "[id=" + record.getThreadID() + "]";
args[2] = record.getLevel().getName();
args[3] = record.getSourceMethodName() != null
? abbreviateClassName(
record.getSourceClassName() == null ? record.getLoggerName() : record.getSourceClassName(), 32)
+ "#" + record.getSourceMethodName()
: abbreviateClassName(
record.getSourceClassName() == null ? record.getLoggerName() : record.getSourceClassName(),
40);
args[4] = formatMessage(record);
StringBuffer buf = formatter.format(args, new StringBuffer(), null);
public String format(LogRecord record) {
StringBuffer buffer = new StringBuffer();
fdf.format(new Date(record.getMillis()), buffer);

buffer.append(" [id=").append(record.getThreadID()).append("]");

buffer.append("\t").append(record.getLevel().getName()).append("\t");

if (record.getSourceMethodName() != null) {
String sourceClass;
if (record.getSourceClassName() == null) {
sourceClass = record.getLoggerName();
} else {
sourceClass = record.getSourceClassName();
}

buffer.append(abbreviateClassName(sourceClass, 32)).append("#").append(record.getSourceMethodName());
} else {
String sourceClass;
if (record.getSourceClassName() == null) {
sourceClass = record.getLoggerName();
} else {
sourceClass = record.getSourceClassName();
}
buffer.append(abbreviateClassName(sourceClass, 40));
}

buffer.append(": ").append(formatMessage(record));

if (record.getThrown() != null) {
try {
StringWriter writer = new StringWriter();
PrintWriter out = new PrintWriter(writer);
record.getThrown().printStackTrace(out);
out.close();
buf.append(writer.toString());
buffer.append(writer.toString());
} catch (Exception e) {
// ignore
}
}
return buf.toString();

buffer.append("\n");
return buffer.toString();
}

public String abbreviateClassName(String fqcn, int targetLength) {
Expand Down

0 comments on commit 666dd1d

Please sign in to comment.