我试图在我的应用程序中升级log4j版本,并且在重写包装器类时遇到麻烦。该应用程序具有Logger包装器类,并使用它来记录语句。
包装器类使用一条语句
static public Category cat = Category.getInstance (Logger.class.getName());
当我尝试使用
Logger cat = LogManager.getLogger(Logger.class.getName())
重新编写它时,我得到类型转换错误-“类型不匹配:无法从org.apache.logging.log4j.Logger转换为Logger”,当我使用强制类型转换时,出现错误“ org.apache.logging.log4j.Logger无法转换为Logger”
/*
* $Id: Logger.java,v 1.10 2006/11/09 16:43:24 bh1465 Exp $
* Created on Aug 4, 2004
*/
package com.sbc.swat.logging ;
import org.apache.log4j.* ;
/**
* Wrapper for log4j logging utility.
*
* @author Brian Holliday
* @version %I%, %G%
* @since 1.4
*/
public class Logger
{
//*******************************************************************/
//* */
//* CLASS DATA */
//* */
//*******************************************************************/
//*--private class data---------------------------------------------*/
//*--protected class data-------------------------------------------*/
//*--package class data---------------------------------------------*/
//*--public class data----------------------------------------------*/
//*=================================================================*/
//* constants for logging */
//*=================================================================*/
static public final int ALL = 0 ;
static public final int DEBUG = 0 ;
static public final int INFO = 1 ;
static public final int WARN = 2 ;
static public final int ERROR = 3 ;
static public final int FATAL = 4 ;
static public final int OFF = 5 ;
//*=================================================================*/
//* constants for logging exceptions */
//*=================================================================*/
static public final int DETAIL_LOW = 0;
static public final int DETAIL_MED = 1;
static public final int DETAIL_FULL = 2;
//*=================================================================*/
static public Category cat = Category.getInstance (Logger.class.getName());
//*******************************************************************/
//* */
//* OBJECT DATA */
//* */
//*******************************************************************/
//*--private object data--------------------------------------------*/
//*--protected object data------------------------------------------*/
//*--package object data--------------------------------------------*/
//*--public object data---------------------------------------------*/
//*******************************************************************/
//* */
//* CLASS METHODS */
//* */
//*******************************************************************/
//*--private class methods------------------------------------------*/
//*******************************************************************/
//* logDetails -- log exception details */
//*******************************************************************/
/**
* Log exception details.
*
* @param pad left-side padding before every output line
* @param e exception
* @param level ALL, DEBUG, INFO, WARN, ERROR, FATAL, or OFF
*/
static private void logDetails (String pad, Throwable e, int level)
{
if (e.getMessage() == null)
{
log(level, pad + "Msg: (null)");
}
else if (e.getMessage().indexOf('\n') == -1)
{
log (level, pad + "Msg : {" + e.getMessage() + "}");
}
else
{
String[] lines = e.getMessage().split("\r?\n", -1);
log(level, pad + "Msg :");
log(level, pad + "{");
for (int i = 0; i < lines.length; i++)
{
log(level, pad + " " + lines[i]);
}
log(level, pad + "}");
}
log(level, pad + "Excpt: {" + e.getClass() + "}");
}
//*--protected class methods----------------------------------------*/
//*--package class methods------------------------------------------*/
//*--public class methods-------------------------------------------*/
//*******************************************************************/
//* debug -- log message if at DEBUG level or better */
//*******************************************************************/
/**
* Log message if at DEBUG level or better.
*
* @param msg output message
*/
static public void debug (String msg)
{
log (DEBUG, msg) ;
}
//*******************************************************************/
//* debug -- log exception if at DEBUG level or better */
//*******************************************************************/
/**
* Log exception if at DEBUG level or better.
*
* @param e exception
* @param detail DETAIL_LOW, DETAIL_MED, or DETAIL_FULL
*/
static public void debug (Throwable e, int detail)
{
log (e, DEBUG, detail) ;
}
//*******************************************************************/
//* error -- log message if at ERROR level or better */
//*******************************************************************/
/**
* Log message if at ERROR level or better.
*
* @param msg output message
*/
static public void error (String msg)
{
log (ERROR, msg) ;
}
//*******************************************************************/
//* error -- log exception if at ERROR level or better */
//*******************************************************************/
/**
* Log exception if at ERROR level or better.
*
* @param e exception
* @param detail DETAIL_LOW, DETAIL_MED, or DETAIL_FULL
*/
static public void error (Throwable e, int detail)
{
log (e, ERROR, detail) ;
}
//*******************************************************************/
//* fatal -- log message if at FATAL level or better */
//*******************************************************************/
/**
* Log message if at FATAL level or better.
*
* @param msg output message
*/
static public void fatal (String msg)
{
log (FATAL, msg) ;
}
//*******************************************************************/
//* fatal -- log exception if at FATAL level or better */
//*******************************************************************/
/**
* Log exception if at FATAL level or better.
*
* @param e exception
* @param detail DETAIL_LOW, DETAIL_MED, or DETAIL_FULL
*/
static public void fatal (Throwable e, int detail)
{
log (e, FATAL, detail) ;
}
//*******************************************************************/
//* info -- log message if at INFO level or better */
//*******************************************************************/
/**
* Log message if at INFO level or better.
*
* @param msg output message
*/
static public void info (String msg)
{
log (INFO, msg) ;
}
//*******************************************************************/
//* info -- log exception if at INFO level or better */
//*******************************************************************/
/**
* Log exception if at INFO level or better.
*
* @param e exception
* @param detail DETAIL_LOW, DETAIL_MED, or DETAIL_FULL
*/
static public void info (Throwable e, int detail)
{
log (e, INFO, detail) ;
}
//*******************************************************************/
//* log -- log message if at appropriate level */
//*******************************************************************/
/**
* Log message if at appropriate level.
*
* @param level ALL, DEBUG, INFO, WARN, ERROR, FATAL, or OFF
* @param msg output message
*/
static public void log (int level, String msg)
{
switch (level)
{
case DEBUG :
cat.debug (msg) ;
break ;
case INFO :
cat.info (msg) ;
break ;
case WARN :
cat.warn (msg) ;
break ;
case ERROR :
cat.error (msg) ;
break ;
case FATAL :
cat.fatal (msg) ;
break ;
case OFF :
break ;
default :
cat.warn ("Problem using Logger class") ;
}
}
//*******************************************************************/
//* log -- log exception if at appropriate level */
//*******************************************************************/
/**
* Log exception if at appropriate level.
*
* @param e exception
* @param level ALL, DEBUG, INFO, WARN, ERROR, FATAL, or OFF
* @param detail DETAIL_LOW, DETAIL_MED, or DETAIL_FULL
*/
static public void log (Throwable e, int level, int detail)
{
log ("", e, level, detail) ;
}
//*******************************************************************/
//* log -- log exception if at appropriate level */
//*******************************************************************/
/**
* Log exception if at appropriate level.
*
* @param pad left-side padding before every output line
* @param e exception
* @param level ALL, DEBUG, INFO, WARN, ERROR, FATAL, or OFF
* @param detail DETAIL_LOW, DETAIL_MED, or DETAIL_FULL
*/
static public void log (String pad, Throwable e, int level, int detail)
{
logDetails (pad, e, level) ;
logStack (pad, e, level, detail) ;
if (e.getCause() != null)
{
log (level, "");
log (level, pad + "Nested Error:") ;
log (pad + " ", e.getCause(), level, detail) ;
}
}
//*******************************************************************/
//* logIfData -- log if Boolean datum not null */
//*******************************************************************/
/**
* Log if Boolean datum not null.
*
* @param msg output message (before Boolean datum)
* @param datum Boolean datum
*/
static public void logIfData (int level, String msg, Boolean datum)
{
if (datum != null)
{
log (level, msg + " [" + datum.booleanValue() + "]") ;
}
}
//*******************************************************************/
//* logIfData -- log if Float datum not null */
//*******************************************************************/
/**
* Log if Float datum not null.
*
* @param msg output message (before Float datum)
* @param datum Float datum
*/
static public void logIfData (int level, String msg, Float datum)
{
if (datum != null)
{
log (level, msg + " [" + datum + "]") ;
}
}
//*******************************************************************/
//* logIfData -- log if Long datum not null */
//*******************************************************************/
/**
* Log if Long datum not null.
*
* @param msg output message (before Long datum)
* @param datum Long datum
*/
static public void logIfData (int level, String msg, Long datum)
{
if (datum != null)
{
log (level, msg + " [" + datum + "]") ;
}
}
//*******************************************************************/
//* logIfData -- log if String datum not empty */
//*******************************************************************/
/**
* Log if String datum not empty.
*
* @param msg output message (before String datum)
* @param datum output String datum
*/
static public void logIfData (int level, String msg, String datum)
{
if (datum != null)
{
if (datum.equals ("") == false)
{
log (level, msg + " [" + datum + "]") ;
}
}
}
//*******************************************************************/
//* logIfData -- log if String datum not empty */
//*******************************************************************/
/**
* Log if String datum not empty.
*
* @param msg output message (before string datum)
* @param datum output string datum
*/
static public void logIfData (String msg, String datum)
{
if (datum != null)
{
if (datum.equals ("") == false)
{
log (INFO, msg + " [" + datum + "]") ;
}
}
}
//*******************************************************************/
//* logIfData -- log if int datum not zero */
//*******************************************************************/
/**
* Log if int datum not zero.
*
* @param msg output message (before integer datum)
* @param datum output integer datum
*/
static public void logIfData (int level, String msg, int datum)
{
if (datum != 0)
{
log (level, msg + " [" + datum + "]") ;
}
}
//*******************************************************************/
//* logIfData -- log if integer datum not zero */
//*******************************************************************/
/**
* Log if integer datum not zero.
*
* @param msg output message (before integer datum)
* @param datum output integer datum
*/
static public void logIfData (String msg, int datum)
{
if (datum != 0)
{
log (INFO, msg + " [" + datum + "]") ;
}
}
//*******************************************************************/
//* logStack -- log exception stack */
//*******************************************************************/
/**
* Log exception stack.
*
* @param pad left-side padding before every output line
* @param e exception
* @param level ALL, DEBUG, INFO, WARN, ERROR, FATAL, or OFF
* @param detail DETAIL_LOW, DETAIL_MED, or DETAIL_FULL
*/
static public void logStack(String pad, Throwable e, int level, int detail)
{
if (detail <= DETAIL_LOW)
return;
StackTraceElement[] trace = e.getStackTrace();
int till = trace.length;
if (detail <= DETAIL_MED)
{
//This code hides any common stack frames, we should be
// left with just the frames between where the exception
// was thrown, and where it was logged.
StackTraceElement[] to = new Throwable().getStackTrace();
int i = trace.length - 1;
int j = to.length -1;
for(; i > -1 && j > -1; i--, j--, till--)
{
if (!trace[i].equals(to[j]))
break;
}
}
for (int i = 0; i < till; i++)
{
log(level, pad + " at " + trace[i].toString());
}
if (till != trace.length)
log(level, pad + " at ...");
}
//*******************************************************************/
//* warn -- log message if at WARN level or better */
//*******************************************************************/
/**
* Log message if at WARN level or better.
*
* @param msg output message
*/
static public void warn (String msg)
{
log (WARN, msg) ;
}
//*******************************************************************/
//* warn -- log exception if at WARN level or better */
//*******************************************************************/
/**
* Log exception if at WARN level or better.
*
* @param e exception
* @param detail DETAIL_LOW, DETAIL_MED, or DETAIL_FULL
*/
static public void warn (Throwable e, int detail)
{
log (e, WARN, detail) ;
}
//*******************************************************************/
//* */
//* OBJECT METHODS */
//* */
//*******************************************************************/
//*--private object methods-----------------------------------------*/
//*--protected object methods---------------------------------------*/
//*--package object methods-----------------------------------------*/
//*--public object methods------------------------------------------*/
}
最佳答案
通过使用解决了问题
org.apache.logging.log4j.Logger cat = LogManager.getLogger(Logger.class.getName())
而不是问题中使用的内容。