标签:
核心代码:
public void setLogLevel(String category, String level) {
org.apache.log4j.Logger log;
if(LoggerInfo.ROOT_NAME.equals(category)) {
log = org.apache.log4j.LogManager.getRootLogger();
} else {
log = org.apache.log4j.Logger.getLogger(category);
}
if(level==null||"unset".equals(level)||"null".equals(level)) {
log.setLevel(null);
}
else {
log.setLevel(org.apache.log4j.Level.toLevel(level));
}
}
public List<String> getAllLevels() {
return Arrays.asList(
org.apache.log4j.Level.ALL.toString(),
org.apache.log4j.Level.TRACE.toString(),
org.apache.log4j.Level.DEBUG.toString(),
org.apache.log4j.Level.INFO.toString(),
org.apache.log4j.Level.WARN.toString(),
org.apache.log4j.Level.ERROR.toString(),
org.apache.log4j.Level.FATAL.toString(),
org.apache.log4j.Level.OFF.toString());
}
public Collection<LoggerInfo> getAllLoggers() {
org.apache.log4j.Logger root = org.apache.log4j.LogManager.getRootLogger();
Map<String,LoggerInfo> map = new HashMap<String,LoggerInfo>();
Enumeration<?> loggers = org.apache.log4j.LogManager.getCurrentLoggers();
while (loggers.hasMoreElements()) {
org.apache.log4j.Logger logger = (org.apache.log4j.Logger)loggers.nextElement();
String name = logger.getName();
if( logger == root) {
continue;
}
map.put(name, new Log4jInfo(name, logger));
while (true) {
int dot = name.lastIndexOf(".");
if (dot < 0)
break;
name = name.substring(0, dot);
if(!map.containsKey(name)) {
map.put(name, new Log4jInfo(name, null));
}
}
}
map.put(LoggerInfo.ROOT_NAME, new Log4jInfo(LoggerInfo.ROOT_NAME, root));
return map.values();
}
依赖类LoggerInfo
/*
* 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.
*/
/**
* Wrapper class for Logger implementaions
*/
public abstract class LoggerInfo implements Comparable<LoggerInfo> {
public static final String ROOT_NAME = "root";
protected final String name;
protected String level;
public LoggerInfo(String name) {
this.name = name;
}
public String getLevel() {
return level;
}
public String getName() {
return name;
}
public abstract boolean isSet();
// public Map getInfo() {
// Map info = new HashMap();
// info.put("name", getName());
// info.put("level", getLevel());
// info.put("set", isSet());
// return info;
// }
@Override
public int compareTo(LoggerInfo other) {
if (this.equals(other))
return 0;
String tN = this.getName();
String oN = other.getName();
if(ROOT_NAME.equals(tN))
return -1;
if(ROOT_NAME.equals(oN))
return 1;
return tN.compareTo(oN);
}
}
依赖类Log4jInfo
/**
* 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.
*/
public class Log4jInfo extends LoggerInfo {
final org.apache.log4j.Logger logger;
public Log4jInfo(String name, org.apache.log4j.Logger logger) {
super(name);
this.logger = logger;
}
@Override
public String getLevel() {
if(logger==null) {
return null;
}
Object level = logger.getLevel();
if(level==null) {
return null;
}
return level.toString();
}
@Override
public String getName() {
return name;
}
@Override
public boolean isSet() {
return (logger!=null && logger.getLevel()!=null);
}
}
用easyui做了一个简单的界面和修改
标签:
原文地址:http://my.oschina.net/stevenliuit/blog/511112