/*
* 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
没有合适的资源?快使用搜索试试~ 我知道了~
log4j-1.2.17(源码+jar包)

共1610个文件
html:913个
java:363个
xml:92个

需积分: 32 56 下载量 30 浏览量
2017-09-21
09:39:00
上传
评论 1
收藏 4.63MB ZIP 举报
温馨提示
内附log4j-1.2.17版本源码和jar包 适合初学者及需要的人群使用与学习 关于如何配置,在这里不做讲解, 具体可以谷歌一下。
资源推荐
资源详情
资源评论















收起资源包目录





































































































共 1610 条
- 1
- 2
- 3
- 4
- 5
- 6
- 17
资源评论


卜老怪
- 粉丝: 1
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 移动通信基站综合防雷设计方案.doc
- 第9章-MATLAB在风力发电技术中的应用仿真.ppt
- 通识计算机程式设计教育理念与教学计画市公开课金奖市赛课一等奖课件.pptx
- 实现电脑开机小键盘自动打开的功能,不用每次都手动开启小键盘,双击运行一次即可
- 自动化考研专业课所考科目.doc
- 系统内置的五种网络测试工具(强烈推荐).pdf
- 创维网络营销策划书.docx
- 论项目管理中施工进度的管理.doc
- 石油化工自动化技术发展趋势.doc
- Swift-Swift资源
- 图解项目管理(2008版)(V3)(下).ppt
- 矿井电气系统安全检查.pptx
- MATLAB-Matlab资源
- 基于 Java 的 OpenCV 库实现简单实用的图像识别
- 计算机软件著作权合作开发协议简洁版.pdf
- 基于HTML、CSS和JavaScript的简易签到功能实现
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
