간단한 로그 프레임워크
11943 단어 code
/**
*
*
* @author yourname (mailto:[email protected])
*/
public interface ILogger {
boolean isDebugEnabled();
boolean isInfoEnabled();
boolean isWarnEnabled();
boolean isErrorEnabled();
void debug(Object message);
void debug(Object message, Object[] params);
void debug(Throwable t);
void debug(Object message, Throwable t);
void debug(Object message, Object[] params, Throwable t);
void info(Object message);
void info(Object message, Object[] params);
void info(Throwable t);
void info(Object message, Throwable t);
void info(Object message, Object[] params, Throwable t);
void warn(Object message);
void warn(Object message, Object[] params);
void warn(Throwable t);
void warn(Object message, Throwable t);
void warn(Object message, Object[] params, Throwable t);
void error(Object message);
void error(Object message, Object[] params);
void error(Throwable t);
void error(Object message, Throwable t);
void error(Object message, Object[] params, Throwable t);
}
/**
*
*
* @author yourname (mailto:[email protected])
*/
public class DebugLoggerFactory {
private static LoggerFactoryPlugin logFactory = new LoggerFactoryPlugin(null);
public static void setLoggerProvider(ILoggerProvider provider) {
synchronized (DebugLoggerFactory.class) {
logFactory = new LoggerFactoryPlugin(provider);
}
}
/**
*
*
*/
public static void refresh() {
logFactory.refresh();
}
/**
*
*
* @param clazz
* @return
*/
public static ILogger getLogger(Class clazz) {
return logFactory.getLogger(clazz);
}
/**
*
*
* @param loggerName
* @return
*/
public static ILogger getLogger(String loggerName) {
return logFactory.getLogger(loggerName);
}
}
/**
*
*
* @author yourname (mailto:[email protected])
*/
public interface ILoggerProvider {
/**
*
*
* @param loggerName
* @return
*/
ILogger createLogger(String loggerName);
}
/**
*
*
* @author yourname (mailto:[email protected])
*/
public abstract class AbstractLogger implements ILogger {
public void debug(Object message) {
if (message != null && message instanceof Throwable) {
debug(null, null, (Throwable)message);
return;
}
debug(message, null, null);
}
public void debug(Object message, Object[] params) {
debug(message, params, null);
}
public void debug(Throwable t) {
debug(null, null, t);
}
public void debug(Object message, Throwable t) {
debug(message, null, t);
}
public void debug(Object message, Object[] params, Throwable t) {
if (isDebugEnabled()) {
doDebug(formatMessage(message, params), t);
}
}
abstract protected void doDebug(String message, Throwable t);
public void info(Object message) {
if (message != null && message instanceof Throwable) {
info(null, null, (Throwable)message);
return;
}
info(message, null, null);
}
public void info(Object message, Object[] params) {
info(message, params, null);
}
public void info(Throwable t) {
info(null, null, t);
}
public void info(Object message, Throwable t) {
info(message, null, t);
}
public void info(Object message, Object[] params, Throwable t) {
if (isInfoEnabled()) {
doInfo(formatMessage(message, params), t);
}
}
abstract protected void doInfo(String message, Throwable t);
public void warn(Object message) {
if (message != null && message instanceof Throwable) {
warn(null, null, (Throwable)message);
return;
}
warn(message, null, null);
}
public void warn(Object message, Object[] params) {
warn(message, params, null);
}
public void warn(Throwable t) {
warn(null, null, t);
}
public void warn(Object message, Throwable t) {
warn(message, null, t);
}
public void warn(Object message, Object[] params, Throwable t) {
if (isWarnEnabled()) {
doWarn(formatMessage(message, params), t);
}
}
abstract protected void doWarn(String message, Throwable t);
public void error(Object message) {
if (message != null && message instanceof Throwable) {
error(null, null, (Throwable)message);
return;
}
error(message, null, null);
}
public void error(Object message, Object[] params) {
error(message, params, null);
}
public void error(Throwable t) {
error(null, null, t);
}
public void error(Object message, Throwable t) {
error(message, null, t);
}
public void error(Object message, Object[] params, Throwable t) {
if (isErrorEnabled()) {
doError(formatMessage(message, params), t);
}
}
abstract protected void doError(String message, Throwable t);
protected String formatMessage(Object message, Object[] params) {
StringBuilder buf = new StringBuilder();
if (message != null) {
if (message instanceof String) {
String msg = (String) message;
if (msg.trim().length() > 0) {
if (params != null && params.length > 0) {
message = new MessageFormat(msg).format(params);
}
}
}
buf.append(message);
}
return buf.toString();
}
}
/**
*
*
* @author Administrator
*
*/
public class LoggerFactoryPlugin {
private ILoggerProvider _provider = null;
private WeakHashMap<String, LoggerWrapper> logWeakMap = new WeakHashMap<String, LoggerWrapper>();
private Object lock = new Object();
public LoggerFactoryPlugin(ILoggerProvider provider) {
this._provider = provider;
}
/**
*
*
*/
public void refresh() {
for (Entry<String, LoggerWrapper> entry : logWeakMap.entrySet()) {
entry.getValue().logger = doGetLogger(entry.getKey()).logger;
}
}
/**
*
*
* @param clazz
* @return
*/
public ILogger getLogger(Class clazz) {
return getLogger(clazz == null ? "null" : clazz.getName());
}
/**
*
*
* @param loggerName
* @return
*/
public ILogger getLogger(String loggerName) {
if (loggerName == null) {
loggerName = "null";
}
LoggerWrapper log = doGetLogger(loggerName);
synchronized(lock) {
logWeakMap.put(loggerName, log);
}
return log;
}
private LoggerWrapper doGetLogger(String loggerName) {
ILogger log = _provider != null ? _provider.createLogger(loggerName) : null;
if (!(log instanceof LoggerWrapper)) {
log = new LoggerWrapper(loggerName, log);
}
return (LoggerWrapper)log;
}
}
/**
* ( )
*
* @author yourname (mailto:[email protected])
*/
public class LoggerWrapper extends AbstractLogger {
ILogger logger = null;
@SuppressWarnings("unused")
private String name = null;
public LoggerWrapper(String name, ILogger logger) {
this.name = name;
if (logger == null) {
return;
}
if (logger instanceof LoggerWrapper) {
this.logger = ((LoggerWrapper)logger).logger;
} else {
this.logger = logger;
}
}
public boolean isDebugEnabled() {
if (logger == null) {
return true;
}
try {
return logger.isDebugEnabled();
} catch (Throwable t) {
return false;
}
}
public boolean isInfoEnabled() {
if (logger == null) {
return true;
}
try {
return logger.isInfoEnabled();
} catch (Throwable t) {
return false;
}
}
public boolean isWarnEnabled() {
if (logger == null) {
return true;
}
try {
return logger.isWarnEnabled();
} catch (Throwable t) {
return false;
}
}
public boolean isErrorEnabled() {
if (logger == null) {
return true;
}
try {
return logger.isErrorEnabled();
} catch (Throwable t) {
return false;
}
}
protected void doDebug(String message, Throwable t) {
try {
logger.debug(message, t);
} catch (Throwable e) {
try {
System.out.println(convert("DEBUG", message, t));
} catch (Throwable ignore) {
}
}
}
protected void doInfo(String message, Throwable t) {
try {
logger.info(message, t);
} catch (Throwable e) {
try {
System.out.println(convert(" INFO", message, t));
} catch (Throwable ignore) {
}
}
}
protected void doWarn(String message, Throwable t) {
try {
logger.warn(message, t);
} catch (Throwable e) {
try {
System.err.println(convert(" WARN", message, t));
} catch (Throwable ignore) {
}
}
}
protected void doError(String message, Throwable t) {
try {
logger.error(message, t);
} catch (Throwable e) {
try {
System.err.println(convert("ERROR", message, t));
} catch (Throwable ignore) {
}
}
}
private String convert(String level, Object message, Throwable t) {
StringBuilder buf = new StringBuilder();
buf.append("[").append(format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")).append("]");
buf.append("[").append(level).append("]");
// if (name != null && name.trim().length() > 0) {
// buf.append("[").append(name).append("]");
// }
buf.append("[").append(message).append("]");
if (t != null) {
String error_msg = t.getMessage();
if (error_msg != null) {
buf.append("[").append(error_msg).append("]");
}
buf.append("
");
StringWriter sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw));
buf.append(sw.toString());
}
return buf.toString();
}
private static String format(long time, String pattern) {
SimpleDateFormat dateFormat = new SimpleDateFormat();
dateFormat.applyPattern(pattern);
return dateFormat.format(new Date(time));
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
소스 코드가 포함된 Python 프로젝트텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.