log4j对象改变日志级别

log4j对象改变日志级别可批量的改变所有级别,或是根据条件改变日志级别。

log4j配置文件:


log4j.rootLogger=ERROR,FILE,CONSOLE,EXECPTION

#log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.File=${catalina.home}/logs/boss.log
log4j.appender.CONSOLE.Threshold=DEBUG
log4j.appender.FILE.Encoding=utf-8
#log4j.appender.FILE.MaxFileSize=10MB
#log4j.appender.FILE.MaxBackupIndex=100
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss,SSS}][%t][%l][%p]: %m%n

log4j.appender.EXECPTION=org.apache.log4j.DailyRollingFileAppender
log4j.appender.EXECPTION.File=${catalina.home}/logs/error.log
log4j.appender.EXECPTION.Threshold=ERROR
log4j.appender.EXECPTION.Encoding=utf-8
#log4j.appender.EXECPTION.MaxFileSize=10MB
#log4j.appender.EXECPTION.MaxBackupIndex=100
log4j.appender.EXECPTION.layout=org.apache.log4j.PatternLayout
log4j.appender.EXECPTION.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss,SSS}][%t][%l][%p]: %m%n

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%d{HH:mm:ss,SSS}][%-5p][%t][%M] (%F:%L) - %m%n

log4j.appender.Requests=org.apache.log4j.RollingFileAppender
log4j.appender.Requests.File=${catalina.home}/logs/jotm.log
log4j.appender.Requests.MaxFileSize=100MB
log4j.appender.Requests.MaxBackupIndex=5
log4j.appender.Requests.Encoding=utf-8
log4j.appender.Requests.layout=org.apache.log4j.PatternLayout
log4j.appender.Requests.layout.ConversionPattern=%10r [%-5c{1}] %7p - %C{1}.%M(%L) : %m\n

#c3p0
log4j.logger.com.mchange.v2=ERROR
log4j.logger.com.opensymphony=ERROR
log4j.logger.org.apache=ERROR
log4j.logger.org.springframework=ERROR
log4j.logger.freemarker.cache=ERROR

#SqlMap logging configuration...
#DEBUG < INFO < WARN < ERROR < FATAL#
log4j.logger.com.miri.boss=DEBUG

log4j.logger.com.ibatis=ERROR
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=ERROR
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=ERROR
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=ERROR

#log4j.logger.java.sql=INFO
log4j.logger.java.sql.Statement=ERROR
log4j.logger.java.sql.ResultSet=ERROR
log4j.logger.java.sql.PreparedStatement=ERROR

#json
log4j.org.apache.struts2=ERROR
log4j.logger.org.apache.struts2.json=ERROR

#ecside
log4j.logger.org.ecside.core.TableModelUtils=ERROR

# JOTM #
log4j.logger.org.objectweb.jotm=ERROR, Requests
log4j.additivity.org.objectweb.jotm=false

log4j.logger.org.objectweb.jotm.jta=ERROR, Requests
log4j.additivity.org.objectweb.jotm.jta=false

log4j.logger.org.objectweb.jotm.recovery=ERROR, Requests
log4j.additivity.org.objectweb.jotm.recovery=false

log4j.logger.org.objectweb.jotm.tests=ERROR, Requests
log4j.additivity.org.objectweb.jotm.tests=false

# CAROL #
log4j.logger.org.objectweb.carol=ERROR, Requests
log4j.additivity.org.objectweb.carol=false

log4j.logger.org.objectweb.carol.rmi=ERROR, Requests
log4j.additivity.org.objectweb.carol.rmi=false

log4j.logger.org.objectweb.carol.jndi=ERROR, Requests
log4j.additivity.org.objectweb.carol.jndi=false


1、批量的改变所有日志对象的日志级别:


//value日志级别eroor、info、debug等
public void test(String value){
final String sArg = LogManager.getRootLogger().getLevel().toString();
if (!sArg.equals(value))
{
//设置日志级别
final Level level = Level.toLevel(value);
//设置根日志级别
LogManager.getRootLogger().setLevel(level);

//获取配置文件中的所有log4j对象
final Enumeration logs = LogManager.getCurrentLoggers();
while (logs.hasMoreElements())
{
final org.apache.log4j.Logger logger = logs.nextElement();
//给log4j对象设置级别
logger.setLevel(level);
}
}



2、根据log4j对象的名称改变日志级别,名称将会和配置文件中的名称匹配,匹配上的对象设置日志级别。


//value日志级别eroor、info、debug等
public void test(String value){

final String sArg = LogManager.getRootLogger().getLevel().toString();
if (!sArg.equals(value))
{
final Level level = Level.toLevel(value);
LogManager.getRootLogger().setLevel(level);
final Enumeration logs = LogManager.getCurrentLoggers();
while (logs.hasMoreElements())
{
final org.apache.log4j.Logger logger = logs.nextElement();
final String loggerName = logger.getName();
// 匹配调整部分logger
if (loggerName.contains("java.sql") || loggerName.contains("com.miri.boss")
|| loggerName.contains("com.ibatis"))
{
logger.setLevel(level);
}
}
}



上面的匹配条件将会匹配到日志配置文件中的:


log4j.logger.com.miri.boss=DEBUG

log4j.logger.com.ibatis=ERROR
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=ERROR
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=ERROR
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=ERROR

#log4j.logger.java.sql=INFO
log4j.logger.java.sql.Statement=ERROR
log4j.logger.java.sql.ResultSet=ERROR
log4j.logger.java.sql.PreparedStatement=ERROR

你可能感兴趣的:(log4j,java,log4j,level,日志级别,log4j对象名称)