/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.log4j;
import org.apache.log4j.spi.LoggingEvent;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.ResourceBundle;
import java.util.Locale;
/**
* This class provides parameterized logging services
* using the pattern syntax of java.text.MessageFormat.
* Message formatting is only performed when the
* request exceeds the threshold level of the logger.
* When the pattern only contains literal text and
* default conversion patterns (that is "{0}" and similar)
* a simple fast compatible formatter is used.
* If the pattern contains more complex conversion patterns,
* formatting will be delegated to java.text.MessageFormatter
* which can be substantially slower.
*
* @see org.apache.log4j.LogSF
* @since 1.2.16
*
*/
public final class LogMF extends LogXF {
/**
* private constructor.
*
*/
private LogMF() {
}
/**
* Number format.
*/
private static NumberFormat numberFormat = null;
/**
* Locale at time of last number format request.
*/
private static Locale numberLocale = null;
/**
* Date format.
*/
private static DateFormat dateFormat = null;
/**
* Locale at time of last date format request.
*/
private static Locale dateLocale = null;
/**
* Format number.
* @param n number to format, may not be null.
* @return formatted value.
*/
private static synchronized String formatNumber(final Object n) {
Locale currentLocale = Locale.getDefault();
if (currentLocale != numberLocale || numberFormat == null) {
numberLocale = currentLocale;
numberFormat = NumberFormat.getInstance(currentLocale);
}
return numberFormat.format(n);
}
/**
* Format date.
* @param d date, may not be null.
* @return formatted value.
*/
private static synchronized String formatDate(final Object d) {
Locale currentLocale = Locale.getDefault();
if (currentLocale != dateLocale || dateFormat == null) {
dateLocale = currentLocale;
dateFormat = DateFormat.getDateTimeInstance(
DateFormat.SHORT,
DateFormat.SHORT,
currentLocale);
}
return dateFormat.format(d);
}
/**
* Format a single parameter like a "{0}" formatting specifier.
*
* @param arg0 parameter, may be null.
* @return string representation of arg0.
*/
private static String formatObject(final Object arg0) {
if (arg0 instanceof String) {
return arg0.toString();
} else if (arg0 instanceof Double ||
arg0 instanceof Float) {
return formatNumber(arg0);
} else if (arg0 instanceof Date) {
return formatDate(arg0);
}
return String.valueOf(arg0);
}
/**
* Determines if pattern contains only {n} format elements
* and not apostrophes.
*
* @param pattern pattern, may not be null.
* @return true if pattern only contains {n} format elements.
*/
private static boolean isSimple(final String pattern) {
if (pattern.indexOf('\'') != -1) {
return false;
}
for(int pos = pattern.indexOf('{');
pos != -1;
pos = pattern.indexOf('{', pos + 1)) {
if (pos + 2 >= pattern.length() ||
pattern.charAt(pos+2) != '}' ||
pattern.charAt(pos+1) < '0' ||
pattern.charAt(pos+1) > '9') {
return false;
}
}
return true;
}
/**
* Formats arguments using MessageFormat.
* @param pattern pattern, may be malformed or null.
* @param arguments arguments, may be null or mismatched.
* @return Message string or null
*/
private static String format(final String pattern,
final Object[] arguments) {
if (pattern == null) {
return null;
} else if(isSimple(pattern)) {
String formatted[] = new String[10];
int prev = 0;
String retval = "";
int pos = pattern.indexOf('{');
while(pos >= 0) {
if(pos + 2 < pattern.length() &&
pattern.charAt(pos+2) == '}' &&
pattern.charAt(pos+1) >= '0' &&
pattern.charAt(pos+1) <= '9') {
int index = pattern.charAt(pos+1) - '0';
retval += pattern.substring(prev, pos);
if (formatted[index] == null) {
if (arguments == null || index >= arguments.length) {
formatted[index] = pattern.substring(pos, pos+3);
} else {
formatted[index] = formatObject(arguments[index]);
}
}
retval += formatted[index];
prev = pos + 3;
pos = pattern.indexOf('{', prev);
} else {
pos = pattern.indexOf('{', pos + 1);
}
}
retval += pattern.substring(prev);
return retval;
}
try {
return MessageFormat.format(pattern, arguments);
} catch (IllegalArgumentException ex) {
return pattern;
}
}
/**
* Formats a single argument using MessageFormat.
* @param pattern pattern, may be malformed or null.
* @param arguments arguments, may be null or mismatched.
* @return Message string or null
*/
private static String format(final String pattern,
final Object arg0) {
if (pattern == null) {
return null;
} else if(isSimple(pattern)) {
String formatted = null;
int prev = 0;
String retval = "";
int pos = pattern.indexOf('{');
while(pos >= 0) {
if(pos + 2 < pattern.length() &&
pattern.charAt(pos+2) == '}' &&
pattern.charAt(pos+1) >= '0' &&
pattern.charAt(pos+1) <= '9') {
int index = pattern.charAt(pos+1) - '0';
retval += pattern.substring(prev, pos);
if (index != 0) {
retval += pattern.substring(pos, pos+3);
} else {
if (formatted == null) {
formatted = formatObject(arg0);
}
retval += formatted;
}
prev = pos + 3;
pos = pattern.indexOf('{', prev);
} else {
pos = pattern.indexOf('{', pos + 1);
}
}
retval += pattern.substring(prev);
return retval;
}
try {
return MessageFor
没有合适的资源?快使用搜索试试~ 我知道了~
apache-log4j-1.2.16.zip
共1567个文件
html:883个
java:354个
xml:88个
5星 · 超过95%的资源 需积分: 43 15 下载量 161 浏览量
2010-12-04
15:17:16
上传
评论
收藏 4.4MB ZIP 举报
温馨提示
og4j-1.2.16jar 相关jar与源代码 . 使用myeclipse 关联源代码的时候选择External Floder,然后找到解压的apache-log4j-1.2.16 这个文件夹即可
资源推荐
资源详情
资源评论
收起资源包目录
apache-log4j-1.2.16.zip (1567个子文件)
xmlLayout.1 5KB
dom.A1.1 4KB
patternLayout.1 4KB
dom.A2.1 3KB
enhancedPatternLayout.1 3KB
socketServer.1 2KB
customLogger.1 941B
xmlLayout.mdc.1 296B
customLevel.1 210B
patternLayout.mdc.1 44B
enhancedPatternLayout.mdc.1 44B
hierarchyThreshold.1 0B
patternLayout.10 6KB
enhancedPatternLayout.10 4KB
patternLayout.11 5KB
enhancedPatternLayout.11 3KB
patternLayout.12 6KB
enhancedPatternLayout.12 4KB
patternLayout.13 6KB
enhancedPatternLayout.13 4KB
patternLayout.14 4KB
enhancedPatternLayout.14 3KB
enhancedPatternLayout.15 3KB
xmlLayout.2 7KB
patternLayout.2 5KB
enhancedPatternLayout.2 3KB
socketServer.2 2KB
enhancedPatternLayout.mdc.2 483B
xmlLayout.mdc.2 356B
customLevel.2 210B
hierarchyThreshold.2 51B
customLogger.2 47B
patternLayout.3 5KB
enhancedPatternLayout.3 3KB
socketServer.3 2KB
xmlLayout.3 720B
hierarchyThreshold.3 102B
customLevel.3 42B
patternLayout.4 5KB
dom.A1.4 4KB
enhancedPatternLayout.4 3KB
dom.A2.4 3KB
socketServer.4 2KB
customLevel.4 210B
hierarchyThreshold.4 152B
patternLayout.5 5KB
enhancedPatternLayout.5 3KB
socketServer.5 2KB
hierarchyThreshold.5 202B
patternLayout.6 5KB
enhancedPatternLayout.6 3KB
socketServer.6 2KB
hierarchyThreshold.6 253B
patternLayout.7 5KB
enhancedPatternLayout.7 3KB
socketServer.7 2KB
hierarchyThreshold.7 304B
patternLayout.8 5KB
enhancedPatternLayout.8 3KB
socketServer.8 2KB
hierarchyThreshold.8 304B
patternLayout.9 5KB
enhancedPatternLayout.9 3KB
publications.apt 4KB
download.apt 3KB
index.apt 3KB
roadmap.apt 1KB
mycat.bad 1KB
run-tests.bat 7KB
exception.bin 2KB
mdc.bin 508B
location.bin 465B
ndc.bin 409B
simple.bin 399B
MSG00001.bin 164B
info.bin 60B
patternLayout.mdc.clear 67B
CONTENTS 3KB
nteventlog.cpp 10KB
maven-theme.css 3KB
maven-base.css 2KB
stylesheet.css 2KB
stylesheet.css 2KB
stylesheet.css 1KB
site.css 775B
site.css 775B
print.css 222B
NTEventLogAppender.def 912B
NTEventLogAppender.amd64.dll 45KB
NTEventLogAppender.dll 10KB
log4j.dtd 7KB
log4j.dtd 7KB
log4j.dtd 7KB
fallback1 3KB
faq.fml 35KB
od.gif 6KB
od.gif 6KB
lf5_small_icon.gif 1KB
icon_help_sml.gif 1019B
icon_error_sml.gif 1010B
共 1567 条
- 1
- 2
- 3
- 4
- 5
- 6
- 16
资源评论
- diego10wl2013-11-21可以使用,解决燃眉之急
steely_chen
- 粉丝: 48
- 资源: 50
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功