[jboss-svn-commits] JBoss Common SVN: r3223 - in jboss-logmanager/trunk/src/main/java/org/jboss/logmanager: formatters and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Jun 4 20:41:54 EDT 2009


Author: david.lloyd at jboss.com
Date: 2009-06-04 20:41:54 -0400 (Thu, 04 Jun 2009)
New Revision: 3223

Added:
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/formatters/
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/formatters/ExtFormatter.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/formatters/FormatStep.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/formatters/FormatStringParser.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/formatters/Formatters.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/formatters/MultistepFormatter.java
Log:
Some formatters and stuff

Added: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/formatters/ExtFormatter.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/formatters/ExtFormatter.java	                        (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/formatters/ExtFormatter.java	2009-06-05 00:41:54 UTC (rev 3223)
@@ -0,0 +1,49 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.logmanager.formatters;
+
+import org.jboss.logmanager.ExtLogRecord;
+
+import java.util.logging.Formatter;
+import java.util.logging.LogRecord;
+
+/**
+ * A formatter which handles {@link org.jboss.logmanager.ExtLogRecord ExtLogRecord} instances.
+ */
+public abstract class ExtFormatter extends Formatter {
+
+    private static final String LOGGER_CLASS_NAME = org.jboss.logmanager.Logger.class.getName();
+
+    /** {@inheritDoc} */
+    public final String format(final LogRecord record) {
+        return format((record instanceof ExtLogRecord) ? (ExtLogRecord) record : new ExtLogRecord(record, LOGGER_CLASS_NAME));
+    }
+
+    /**
+     * Format a message using an extended log record.
+     *
+     * @param extLogRecord the log record
+     * @return the formatted message
+     */
+    public abstract String format(final ExtLogRecord extLogRecord);
+}

Added: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/formatters/FormatStep.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/formatters/FormatStep.java	                        (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/formatters/FormatStep.java	2009-06-05 00:41:54 UTC (rev 3223)
@@ -0,0 +1,47 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.logmanager.formatters;
+
+import org.jboss.logmanager.ExtLogRecord;
+
+/**
+ * A single format step which handles some part of rendering a log record.
+ */
+public interface FormatStep {
+
+    /**
+     * Render a part of the log record.
+     *
+     * @param builder the string builder to append to
+     * @param record the record being rendered
+     */
+    void render(StringBuilder builder, ExtLogRecord record);
+
+    /**
+     * Emit an estimate of the length of data which this step will produce.  The more accurate the estimate, the
+     * more likely the format operation will be performant.
+     *
+     * @return an estimate
+     */
+    int estimateLength();
+}

Added: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/formatters/FormatStringParser.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/formatters/FormatStringParser.java	                        (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/formatters/FormatStringParser.java	2009-06-05 00:41:54 UTC (rev 3223)
@@ -0,0 +1,144 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.logmanager.formatters;
+
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+import java.util.ArrayList;
+
+/**
+ * A parser which can translate a log4j-style format string into a series of {@code FormatStep} instances.
+ */
+public final class FormatStringParser {
+
+    /**
+     * The regular expression for format strings.  Ain't regex grand?
+     */
+    private static final Pattern pattern = Pattern.compile(
+                // greedily match all non-format characters
+                "([^%]+)" +
+                // match a format string...
+                "|(?:%" +
+                    // optional minimum width plus justify flag
+                    "((-)?\\d+)?" +
+                    // optional maximum width
+                    "(?:\\.(\\d+))?" +
+                    // the actual format character
+                    "(.)" +
+                    // an optional argument string
+                    "(?:\\{([^}]*)\\})?" +
+                // end format string
+                ")"
+    );
+
+    private FormatStringParser() {
+    }
+
+    /**
+     * Compile a format string into a series of format steps.
+     *
+     * @param formatString the format string
+     * @return the format steps
+     */
+    public static FormatStep[] getSteps(final String formatString) {
+        final long time = System.currentTimeMillis();
+        final ArrayList<FormatStep> stepList = new ArrayList<FormatStep>();
+        final Matcher matcher = pattern.matcher(formatString);
+        while (matcher.find()) {
+            final String otherText = matcher.group(1);
+            if (otherText != null) {
+                stepList.add(Formatters.textFormatStep(otherText));
+            } else {
+                final String minWidthString = matcher.group(2);
+                final String hypen = matcher.group(3);
+                final String maxWidthString = matcher.group(4);
+                final String formatCharString = matcher.group(5);
+                final String argument = matcher.group(6);
+                final int minimumWidth = minWidthString == null ? 0 : Integer.parseInt(minWidthString);
+                final boolean leftJustify = hypen != null;
+                final int maximumWidth = maxWidthString == null ? 0 : Integer.parseInt(maxWidthString);
+                final char formatChar = formatCharString.charAt(0);
+                switch (formatChar) {
+                    case 'c': {
+                        final int count = argument == null ? 0 : Integer.parseInt(argument);
+                        stepList.add(Formatters.loggerNameFormatStep(leftJustify, minimumWidth, maximumWidth, count));
+                        break;
+                    }
+                    case 'C': {
+                        final int count = argument == null ? 0 : Integer.parseInt(argument);
+                        stepList.add(Formatters.classNameFormatStep(leftJustify, minimumWidth, maximumWidth, count));
+                        break;
+                    }
+                    case 'd': {
+                        stepList.add(Formatters.dateFormatStep(argument, leftJustify, minimumWidth, maximumWidth));
+                        break;
+                    }
+                    case 'F': {
+                        stepList.add(Formatters.fileNameFormatStep(leftJustify, minimumWidth, maximumWidth));
+                        break;
+                    }
+                    case 'l': {
+                        stepList.add(Formatters.locationInformationFormatStep(leftJustify, minimumWidth, maximumWidth));
+                        break;
+                    }
+                    case 'm': {
+                        stepList.add(Formatters.messageFormatStep(leftJustify, minimumWidth, maximumWidth));
+                        break;
+                    }
+                    case 'n': {
+                        stepList.add(Formatters.lineSeparatorFormatStep(leftJustify, minimumWidth, maximumWidth));
+                        break;
+                    }
+                    case 'p': {
+                        stepList.add(Formatters.levelFormatStep(leftJustify, minimumWidth, maximumWidth));
+                        break;
+                    }
+                    case 'r': {
+                        stepList.add(Formatters.relativeTimeFormatStep(time, leftJustify, minimumWidth, maximumWidth));
+                        break;
+                    }
+                    case 't': {
+                        stepList.add(Formatters.threadNameFormatStep(leftJustify, minimumWidth, maximumWidth));
+                        break;
+                    }
+                    case 'x': {
+                        stepList.add(Formatters.ndcFormatStep(leftJustify, minimumWidth, maximumWidth));
+                        break;
+                    }
+                    case 'X': {
+                        stepList.add(Formatters.mdcFormatStep(argument, leftJustify, minimumWidth, maximumWidth));
+                        break;
+                    }
+                    case '%': {
+                        stepList.add(Formatters.textFormatStep("%"));
+                        break;
+                    }
+                    default: {
+                        throw new IllegalArgumentException("Encountered an unknown format character");
+                    }
+                }
+            }
+        }
+        return stepList.toArray(new FormatStep[stepList.size()]);
+    }
+}

Added: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/formatters/Formatters.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/formatters/Formatters.java	                        (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/formatters/Formatters.java	2009-06-05 00:41:54 UTC (rev 3223)
@@ -0,0 +1,412 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.logmanager.formatters;
+
+import org.jboss.logmanager.ExtLogRecord;
+
+import java.util.logging.Level;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import static java.lang.Math.min;
+import static java.lang.Math.max;
+
+/**
+ * Formatter utility methods.
+ */
+public final class Formatters {
+
+    private Formatters() {
+    }
+
+    /**
+     * Create a format step which simply emits the given string.
+     *
+     * @param string the string to emit
+     * @return a format step
+     */
+    public static FormatStep textFormatStep(final String string) {
+        return new FormatStep() {
+            public void render(final StringBuilder builder, final ExtLogRecord record) {
+                builder.append(string);
+            }
+
+            public int estimateLength() {
+                return string.length();
+            }
+        };
+    }
+
+    /**
+     * Apply up to {@code count} trailing segments of the given string to the given {@code builder}.
+     *
+     * @param count the maximum number of segments to include
+     * @param subject the subject string
+     * @return the substring
+     */
+    private static String applySegments(final int count, final String subject) {
+        if (count == 0) {
+            return subject;
+        }
+        int idx = subject.length() + 1;
+        for (int i = 0; i < count; i ++) {
+            idx = subject.lastIndexOf('.', idx - 1);
+            if (idx == -1) {
+                return subject;
+            }
+        }
+        return subject.substring(idx + 1);
+    }
+
+    private static abstract class JustifyingFormatStep implements FormatStep {
+        private final boolean leftJustify;
+        private final int minimumWidth;
+        private final int maximumWidth;
+
+        protected JustifyingFormatStep(final boolean leftJustify, final int minimumWidth, final int maximumWidth) {
+            if (maximumWidth != 0 && minimumWidth > maximumWidth) {
+                throw new IllegalArgumentException("Specified minimum width may not be greater than the specified maximum width");
+            }
+            if (maximumWidth < 0 || minimumWidth < 0) {
+                throw new IllegalArgumentException("Minimum and maximum widths must not be less than zero");
+            }
+            this.leftJustify = leftJustify;
+            this.minimumWidth = minimumWidth;
+            this.maximumWidth = maximumWidth == 0 ? Integer.MAX_VALUE : maximumWidth;
+        }
+
+        public void render(final StringBuilder builder, final ExtLogRecord record) {
+            final String subject = getSubject(record);
+            final int len = subject.length();
+            final int minimumWidth = this.minimumWidth;
+            final int maximumWidth = this.maximumWidth;
+            if (len > maximumWidth) {
+                builder.append(subject.substring(len - maximumWidth));
+                return;
+            } else if (len < minimumWidth) {
+                if (leftJustify) {
+                    builder.append(subject);
+                    int spaces = minimumWidth - len;
+                    for (int i = 0; i < spaces; i ++) {
+                        builder.append(' ');
+                    }
+                } else {
+                    // right justify
+                    int spaces = minimumWidth - len;
+                    for (int i = 0; i < spaces; i ++) {
+                        builder.append(' ');
+                    }
+                    builder.append(subject);
+                }
+            }
+        }
+
+        public int estimateLength() {
+            if (maximumWidth != 0) {
+                return min(maximumWidth, minimumWidth * 3);
+            } else {
+                return max(32, minimumWidth);
+            }
+        }
+
+        public abstract String getSubject(final ExtLogRecord record);
+    }
+
+    private static abstract class SegmentedFormatStep extends JustifyingFormatStep {
+        private final int count;
+
+        protected SegmentedFormatStep(final boolean leftJustify, final int minimumWidth, final int maximumWidth, final int count) {
+            super(leftJustify, minimumWidth, maximumWidth);
+            this.count = count;
+        }
+
+        public final String getSubject(final ExtLogRecord record) {
+            return applySegments(count, getSegmentedSubject(record));
+        }
+
+        public abstract String getSegmentedSubject(final ExtLogRecord record);
+    }
+
+    /**
+     * Create a format step which emits the logger name with the given justification rules.
+     *
+     * @param leftJustify {@code true} to left justify, {@code false} to right justify
+     * @param minimumWidth the minimum field width, or 0 for none
+     * @param maximumWidth the maximum field width (must be greater than {@code minimumFieldWidth}), or 0 for none
+     * @param count the maximum number of logger name segments to emit (counting from the right)
+     * @return the format step
+     */
+    public static FormatStep loggerNameFormatStep(final boolean leftJustify, final int minimumWidth, final int maximumWidth, final int count) {
+        return new SegmentedFormatStep(leftJustify, minimumWidth, maximumWidth, count) {
+            public String getSegmentedSubject(final ExtLogRecord record) {
+                return record.getLoggerName();
+            }
+        };
+    }
+
+    /**
+     * Create a format step which emits the source class name with the given justification rules (NOTE: call stack
+     * introspection introduces a significant performance penalty).
+     *
+     * @param leftJustify {@code true} to left justify, {@code false} to right justify
+     * @param minimumWidth the minimum field width, or 0 for none
+     * @param maximumWidth the maximum field width (must be greater than {@code minimumFieldWidth}), or 0 for none
+     * @param count the maximum number of class name segments to emit (counting from the right)
+     * @return the format step
+     */
+    public static FormatStep classNameFormatStep(final boolean leftJustify, final int minimumWidth, final int maximumWidth, final int count) {
+        return new SegmentedFormatStep(leftJustify, minimumWidth, maximumWidth, count) {
+            public String getSegmentedSubject(final ExtLogRecord record) {
+                return record.getSourceClassName();
+            }
+        };
+    }
+
+    /**
+     * Create a format step which emits the date of the log record with the given justificaiton rules.
+     *
+     * @param formatString the date format string
+     * @param leftJustify {@code true} to left justify, {@code false} to right justify
+     * @param minimumWidth the minimum field width, or 0 for none
+     * @param maximumWidth the maximum field width (must be greater than {@code minimumFieldWidth}), or 0 for none
+     * @return the format step
+     */
+    public static FormatStep dateFormatStep(final String formatString, final boolean leftJustify, final int minimumWidth, final int maximumWidth) {
+        final SimpleDateFormat dateFormatMaster = new SimpleDateFormat(formatString);
+        return new JustifyingFormatStep(leftJustify, minimumWidth, maximumWidth) {
+            public String getSubject(final ExtLogRecord record) {
+                final SimpleDateFormat dateFormat = dateFormatMaster;
+                synchronized (dateFormat) {
+                    return dateFormat.format(new Date(record.getMillis()));
+                }
+            }
+        };
+    }
+
+    /**
+     * Create a format step which emits the source file name with the given justification rules (NOTE: call stack
+     * introspection introduces a significant performance penalty).
+     *
+     * @param leftJustify {@code true} to left justify, {@code false} to right justify
+     * @param minimumWidth the minimum field width, or 0 for none
+     * @param maximumWidth the maximum field width (must be greater than {@code minimumFieldWidth}), or 0 for none
+     * @return the format step
+     */
+    public static FormatStep fileNameFormatStep(final boolean leftJustify, final int minimumWidth, final int maximumWidth) {
+        return new JustifyingFormatStep(leftJustify, minimumWidth, maximumWidth) {
+            public String getSubject(final ExtLogRecord record) {
+                return record.getSourceFileName();
+            }
+        };
+    }
+
+    /**
+     * Create a format step which emits the complete source location information with the given justification rules
+     * (NOTE: call stack introspection introduces a significant performance penalty).
+     *
+     * @param leftJustify {@code true} to left justify, {@code false} to right justify
+     * @param minimumWidth the minimum field width, or 0 for none
+     * @param maximumWidth the maximum field width (must be greater than {@code minimumFieldWidth}), or 0 for none
+     * @return the format step
+     */
+    public static FormatStep locationInformationFormatStep(final boolean leftJustify, final int minimumWidth, final int maximumWidth) {
+        return new JustifyingFormatStep(leftJustify, minimumWidth, maximumWidth) {
+            public String getSubject(final ExtLogRecord record) {
+                final String fileName = record.getSourceFileName();
+                final int lineNumber = record.getSourceLineNumber();
+                final String className = record.getSourceClassName();
+                final String methodName = record.getSourceMethodName();
+                final StringBuilder builder = new StringBuilder(fileName.length() + className.length() + methodName.length() + 16);
+                builder.append(className).append('.').append(methodName);
+                builder.append('(').append(fileName);
+                if (lineNumber != -1) {
+                    builder.append(':').append(lineNumber);
+                }
+                builder.append(')');
+                return builder.toString();
+            }
+        };
+    }
+
+    /**
+     * Create a format step which emits the source file line number with the given justification rules (NOTE: call stack
+     * introspection introduces a significant performance penalty).
+     *
+     * @param leftJustify {@code true} to left justify, {@code false} to right justify
+     * @param minimumWidth the minimum field width, or 0 for none
+     * @param maximumWidth the maximum field width (must be greater than {@code minimumFieldWidth}), or 0 for none
+     * @return the format step
+     */
+    public static FormatStep lineNumberFormatStep(final boolean leftJustify, final int minimumWidth, final int maximumWidth) {
+        return new JustifyingFormatStep(leftJustify, minimumWidth, maximumWidth) {
+            public String getSubject(final ExtLogRecord record) {
+                return Integer.toString(record.getSourceLineNumber());
+            }
+        };
+    }
+
+    /**
+     * Create a format step which emits the formatted log message text with the given justification rules.
+     *
+     * @param leftJustify {@code true} to left justify, {@code false} to right justify
+     * @param minimumWidth the minimum field width, or 0 for none
+     * @param maximumWidth the maximum field width (must be greater than {@code minimumFieldWidth}), or 0 for none
+     * @return the format step
+     */
+    public static FormatStep messageFormatStep(final boolean leftJustify, final int minimumWidth, final int maximumWidth) {
+        return new JustifyingFormatStep(leftJustify, minimumWidth, maximumWidth) {
+            public String getSubject(final ExtLogRecord record) {
+                return record.getFormattedMessage();
+            }
+        };
+    }
+
+    /**
+     * Create a format step which emits the source method name with the given justification rules (NOTE: call stack
+     * introspection introduces a significant performance penalty).
+     *
+     * @param leftJustify {@code true} to left justify, {@code false} to right justify
+     * @param minimumWidth the minimum field width, or 0 for none
+     * @param maximumWidth the maximum field width (must be greater than {@code minimumFieldWidth}), or 0 for none
+     * @return the format step
+     */
+    public static FormatStep methodNameFormatStep(final boolean leftJustify, final int minimumWidth, final int maximumWidth) {
+        return new JustifyingFormatStep(leftJustify, minimumWidth, maximumWidth) {
+            public String getSubject(final ExtLogRecord record) {
+                return record.getSourceMethodName();
+            }
+        };
+    }
+
+    private static final String separatorString;
+
+    static {
+        separatorString = AccessController.doPrivileged(new PrivilegedAction<String>() {
+            public String run() {
+                return System.getProperty("line.separator");
+            }
+        });
+    }
+
+    /**
+     * Create a format step which emits the platform line separator.
+     *
+     * @param leftJustify {@code true} to left justify, {@code false} to right justify
+     * @param minimumWidth the minimum field width, or 0 for none
+     * @param maximumWidth the maximum field width (must be greater than {@code minimumFieldWidth}), or 0 for none
+     * @return the format step
+     */
+    public static FormatStep lineSeparatorFormatStep(final boolean leftJustify, final int minimumWidth, final int maximumWidth) {
+        return new JustifyingFormatStep(leftJustify, minimumWidth, maximumWidth) {
+            public String getSubject(final ExtLogRecord record) {
+                return separatorString;
+            }
+        };
+    }
+
+    /**
+     * Create a format step which emits the localized log level name.
+     *
+     * @param leftJustify {@code true} to left justify, {@code false} to right justify
+     * @param minimumWidth the minimum field width, or 0 for none
+     * @param maximumWidth the maximum field width (must be greater than {@code minimumFieldWidth}), or 0 for none
+     * @return the format step
+     */
+    public static FormatStep levelFormatStep(final boolean leftJustify, final int minimumWidth, final int maximumWidth) {
+        return new JustifyingFormatStep(leftJustify, minimumWidth, maximumWidth) {
+            public String getSubject(final ExtLogRecord record) {
+                final Level level = record.getLevel();
+                return (level.getResourceBundleName() != null) ? level.getLocalizedName() : level.getName();
+            }
+        };
+    }
+
+    /**
+     * Create a format step which emits the number of milliseconds since the given base time.
+     *
+     * @param baseTime the base time as milliseconds as per {@link System#currentTimeMillis()}
+     * @param leftJustify {@code true} to left justify, {@code false} to right justify
+     * @param minimumWidth the minimum field width, or 0 for none
+     * @param maximumWidth the maximum field width (must be greater than {@code minimumFieldWidth}), or 0 for none
+     * @return the format step
+     */
+    public static FormatStep relativeTimeFormatStep(final long baseTime, final boolean leftJustify, final int minimumWidth, final int maximumWidth) {
+        return new JustifyingFormatStep(leftJustify, minimumWidth, maximumWidth) {
+            public String getSubject(final ExtLogRecord record) {
+                return Long.toString(record.getMillis() - baseTime);
+            }
+        };
+    }
+
+    /**
+     * Create a format step which emits the name of the thread which originated the log record.
+     *
+     * @param leftJustify {@code true} to left justify, {@code false} to right justify
+     * @param minimumWidth the minimum field width, or 0 for none
+     * @param maximumWidth the maximum field width (must be greater than {@code minimumFieldWidth}), or 0 for none
+     * @return the format step
+     */
+    public static FormatStep threadNameFormatStep(final boolean leftJustify, final int minimumWidth, final int maximumWidth) {
+        return new JustifyingFormatStep(leftJustify, minimumWidth, maximumWidth) {
+            public String getSubject(final ExtLogRecord record) {
+                return record.getThreadName();
+            }
+        };
+    }
+
+    /**
+     * Create a format step which emits the NDC value of the log record.
+     *
+     * @param leftJustify {@code true} to left justify, {@code false} to right justify
+     * @param minimumWidth the minimum field width, or 0 for none
+     * @param maximumWidth the maximum field width (must be greater than {@code minimumFieldWidth}), or 0 for none
+     * @return the format step
+     */
+    public static FormatStep ndcFormatStep(final boolean leftJustify, final int minimumWidth, final int maximumWidth) {
+        return new JustifyingFormatStep(leftJustify, minimumWidth, maximumWidth) {
+            public String getSubject(final ExtLogRecord record) {
+                final String value = record.getNdc();
+                return value == null ? "" : value;
+            }
+        };
+    }
+
+    /**
+     * Create a format step which emits the MDC value associated with the given key of the log record.
+     *
+     * @param leftJustify {@code true} to left justify, {@code false} to right justify
+     * @param minimumWidth the minimum field width, or 0 for none
+     * @param maximumWidth the maximum field width (must be greater than {@code minimumFieldWidth}), or 0 for none
+     * @return the format step
+     */
+    public static FormatStep mdcFormatStep(final String key, final boolean leftJustify, final int minimumWidth, final int maximumWidth) {
+        return new JustifyingFormatStep(leftJustify, minimumWidth, maximumWidth) {
+            public String getSubject(final ExtLogRecord record) {
+                final String value = record.getMdc(key);
+                return value == null ? "" : value;
+            }
+        };
+    }
+}

Added: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/formatters/MultistepFormatter.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/formatters/MultistepFormatter.java	                        (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/formatters/MultistepFormatter.java	2009-06-05 00:41:54 UTC (rev 3223)
@@ -0,0 +1,57 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.logmanager.formatters;
+
+import org.jboss.logmanager.ExtLogRecord;
+import static java.lang.Math.max;
+
+/**
+ * A formatter which formats a record in a series of steps.
+ */
+public final class MultistepFormatter extends ExtFormatter {
+    private final FormatStep[] steps;
+    private final int builderLength;
+
+    /**
+     * Construct a new instance.
+     *
+     * @param steps the steps to execute to format the record
+     */
+    public MultistepFormatter(final FormatStep[] steps) {
+        this.steps = steps;
+        int builderLength = 0;
+        for (FormatStep step : steps) {
+            builderLength += step.estimateLength();
+        }
+        this.builderLength = max(32, builderLength);
+    }
+
+    /** {@inheritDoc} */
+    public String format(final ExtLogRecord record) {
+        final StringBuilder builder = new StringBuilder(builderLength);
+        for (FormatStep step : steps) {
+            step.render(builder, record);
+        }
+        return builder.toString();
+    }
+}




More information about the jboss-svn-commits mailing list