📄 patternformatter.java
字号:
if (rightJustify) { sb.append(output.substring(size - maxSize)); } else { sb.append(output.substring(0, maxSize)); } } else { sb.append(output); } } /** * Append a certain number of whitespace characters to a StringBuffer. * * @param sb the StringBuffer * @param length the number of spaces to append */ private void appendWhiteSpace(final StringBuffer sb, int length) { while (length >= 16) { sb.append(SPACE_16); length -= 16; } if (length >= 8) { sb.append(SPACE_8); length -= 8; } if (length >= 4) { sb.append(SPACE_4); length -= 4; } if (length >= 2) { sb.append(SPACE_2); length -= 2; } if (length >= 1) { sb.append(SPACE_1); length -= 1; } } /** * Format the event according to the pattern. * * @param event the event * @return the formatted output */ public String format(final LogEvent event) { final StringBuffer sb = new StringBuffer(); for (int i = 0; i < m_formatSpecification.length; i++) { final PatternRun run = m_formatSpecification[i]; //treat text differently as it doesn't need min/max padding if (run.m_type == TYPE_TEXT) { sb.append(run.m_data); } else { final String data = formatPatternRun(event, run); if (null != data) { append(sb, run.m_minSize, run.m_maxSize, run.m_rightJustify, data); } } } return sb.toString(); } /** * Formats a single pattern run (can be extended in subclasses). * * @param run the pattern run to format. * @return the formatted result. */ protected String formatPatternRun(final LogEvent event, final PatternRun run) { switch (run.m_type) { case TYPE_RELATIVE_TIME: return getRTime(event.getRelativeTime(), run.m_format); case TYPE_TIME: return getTime(event.getTime(), run.m_format); case TYPE_THROWABLE: return getStackTrace(event.getThrowable(), run.m_format); case TYPE_MESSAGE: return getMessage(event.getMessage(), run.m_format); case TYPE_CATEGORY: return getCategory(event.getCategory(), run.m_format); case TYPE_PRIORITY: return getPriority(event.getPriority(), run.m_format); case TYPE_CONTEXT:// if( null == run.m_format ||// run.m_format.startsWith( "stack" ) )// {// //Print a warning out to stderr here// //to indicate you are using a deprecated feature?// return getContext( event.getContextStack(), run.m_format );// }// else// { return getContextMap(event.getContextMap(), run.m_format);// } default: throw new IllegalStateException("Unknown Pattern specification." + run.m_type); } } /** * Utility method to format category. * * @param category the category string * @param format ancilliary format parameter - allowed to be null * @return the formatted string */ protected String getCategory(final String category, final String format) { return category; } /** * Get formatted priority string. */ protected String getPriority(final Priority priority, final String format) { return priority.getName(); }// /**// * Utility method to format context.// *// * @param context the context string// * @param format ancilliary format parameter - allowed to be null// * @return the formatted string// * @deprecated Use getContextStack rather than this method// */// protected String getContext( final ContextStack stack, final String format )// {// return getContextStack( stack, format );// }// /**// * Utility method to format context.// *// * @param context the context string// * @param format ancilliary format parameter - allowed to be null// * @return the formatted string// */// protected String getContextStack( final ContextStack stack, final String format )// {// if( null == stack ) return "";// return stack.toString( Integer.MAX_VALUE );// } /** * Utility method to format context map. * * @param map the context map * @param format ancilliary format parameter - allowed to be null * @return the formatted string */ protected String getContextMap(final ContextMap map, final String format) { if (null == map) return ""; return map.get(format, "").toString(); } /** * Utility method to format message. * * @param message the message string * @param format ancilliary format parameter - allowed to be null * @return the formatted string */ protected String getMessage(final String message, final String format) { return message; } /** * Utility method to format stack trace. * * @param throwable the throwable instance * @param format ancilliary format parameter - allowed to be null * @return the formatted string */ protected String getStackTrace(final Throwable throwable, final String format) { if (null == throwable) return ""; final StringWriter sw = new StringWriter(); throwable.printStackTrace(new java.io.PrintWriter(sw)); return sw.toString(); } /** * Utility method to format relative time. * * @param time the time * @param format ancilliary format parameter - allowed to be null * @return the formatted string */ protected String getRTime(final long time, final String format) { return getTime(time, format); } /** * Utility method to format time. * * @param time the time * @param format ancilliary format parameter - allowed to be null * @return the formatted string */ protected String getTime(final long time, final String format) { if (null == format) { return Long.toString(time); } else { synchronized (m_date) { if (null == m_simpleDateFormat) { m_simpleDateFormat = FastDateFormat.getInstance(format); } m_date.setTime(time); return m_simpleDateFormat.format(m_date); } } } /** * Retrieve the type-id for a particular string. * * @param type the string * @return the type-id */ protected int getTypeIdFor(final String type) { if (type.equalsIgnoreCase(TYPE_CATEGORY_STR)) return TYPE_CATEGORY; else if (type.equalsIgnoreCase(TYPE_CONTEXT_STR)) return TYPE_CONTEXT; else if (type.equalsIgnoreCase(TYPE_MESSAGE_STR)) return TYPE_MESSAGE; else if (type.equalsIgnoreCase(TYPE_PRIORITY_STR)) return TYPE_PRIORITY; else if (type.equalsIgnoreCase(TYPE_TIME_STR)) return TYPE_TIME; else if (type.equalsIgnoreCase(TYPE_RELATIVE_TIME_STR)) return TYPE_RELATIVE_TIME; else if (type.equalsIgnoreCase(TYPE_THROWABLE_STR)) { return TYPE_THROWABLE; } else { throw new IllegalArgumentException("Unknown Type in pattern - " + type); } } /** * Parse the input pattern and build internal data structures. * * @param patternString the pattern */ protected final void parse(final String patternString) { final Stack stack = new Stack(); final int size = patternString.length(); final char pattern[] = new char[size]; int index = 0; patternString.getChars(0, size, pattern, 0); while (index < size) { if (pattern[index] == '%' && !(index != size - 1 && pattern[index + 1] == '%')) { index += addPatternRun(stack, pattern, index); } else { index += addTextRun(stack, pattern, index); } } final int elementCount = stack.size(); m_formatSpecification = new PatternRun[elementCount]; for (int i = 0; i < elementCount; i++) { m_formatSpecification[i] = (PatternRun)stack.elementAt(i); } } /** * Set the string description that the format is extracted from. * * @param format the string format * @deprecated Parse format in via constructor rather than use this method */ public void setFormat(final String format) { parse(format); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -