浏览代码

jfinal 4.8

James 6 年之前
父节点
当前提交
731c384b35

+ 6 - 5
src/main/java/com/jfinal/log/Log.java

@@ -18,11 +18,12 @@ package com.jfinal.log;
 
 /**
  * The five logging levels used by Log are (in order):
- * 1. DEBUG (the least serious)
- * 2. INFO
- * 3. WARN
- * 4. ERROR
- * 5. FATAL (the most serious)
+ * 1. TRACE (the least serious)
+ * 2. DEBUG
+ * 3. INFO
+ * 4. WARN
+ * 5. ERROR
+ * 6. FATAL (the most serious)
  */
 public abstract class Log {
 	

+ 195 - 0
src/main/java/com/jfinal/log/Slf4jLog.java

@@ -0,0 +1,195 @@
+/**
+ * Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.com).
+ *
+ * Licensed 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 com.jfinal.log;
+
+import org.slf4j.spi.LocationAwareLogger;
+
+/**
+ * Slf4jLog
+ */
+public class Slf4jLog extends Log {
+	
+	private LocationAwareLogger log;
+	
+	private static final Object[] NULL_ARGS = new Object[0];
+	private static final String callerFQCN = Slf4jLog.class.getName();
+	
+	Slf4jLog(LocationAwareLogger log) {
+		this.log = log;
+	}
+	
+	@Override
+	public void trace(String message) {
+		if (isTraceEnabled()) {
+			log.log(null, callerFQCN, LocationAwareLogger.TRACE_INT, message, NULL_ARGS, null);
+		}
+	}
+	
+	@Override
+	public void trace(String message, Throwable t) {
+		if (isTraceEnabled()) {
+			log.log(null, callerFQCN, LocationAwareLogger.TRACE_INT, message, NULL_ARGS, t);
+		}
+	}
+	
+	@Override
+	public void debug(String message) {
+		if (isDebugEnabled()) {
+			log.log(null, callerFQCN, LocationAwareLogger.DEBUG_INT, message, NULL_ARGS, null);
+		}
+	}
+	
+	@Override
+	public void debug(String message, Throwable t) {
+		if (isDebugEnabled()) {
+			log.log(null, callerFQCN, LocationAwareLogger.DEBUG_INT, message, NULL_ARGS, t);
+		}
+	}
+	
+	@Override
+	public void info(String message) {
+		if (isInfoEnabled()) {
+			log.log(null, callerFQCN, LocationAwareLogger.INFO_INT, message, NULL_ARGS, null);
+		}
+	}
+	
+	@Override
+	public void info(String message, Throwable t) {
+		if (isInfoEnabled()) {
+			log.log(null, callerFQCN, LocationAwareLogger.INFO_INT, message, NULL_ARGS, t);
+		}
+	}
+	
+	@Override
+	public void warn(String message) {
+		if (isWarnEnabled()) {
+			log.log(null, callerFQCN, LocationAwareLogger.WARN_INT, message, NULL_ARGS, null);
+		}
+	}
+
+	@Override
+	public void warn(String message, Throwable t) {
+		if (isWarnEnabled()) {
+			log.log(null, callerFQCN, LocationAwareLogger.WARN_INT, message, NULL_ARGS, t);
+		}
+	}
+	
+	@Override
+	public void error(String message) {
+		if (isErrorEnabled()) {
+			log.log(null, callerFQCN, LocationAwareLogger.ERROR_INT, message, NULL_ARGS, null);
+		}
+	}
+	
+	@Override
+	public void error(String message, Throwable t) {
+		if (isErrorEnabled()) {
+			log.log(null, callerFQCN, LocationAwareLogger.ERROR_INT, message, NULL_ARGS, t);
+		}
+	}
+	
+	@Override
+	public void fatal(String message) {
+		// throw new UnsupportedOperationException("slf4j logger does not support fatal level");
+		if (isErrorEnabled()) {
+			log.log(null, callerFQCN, LocationAwareLogger.ERROR_INT, message, NULL_ARGS, null);
+		}
+	}
+	
+	@Override
+	public void fatal(String message, Throwable t) {
+		// throw new UnsupportedOperationException("slf4j logger does not support fatal level");
+		if (isErrorEnabled()) {
+			log.log(null, callerFQCN, LocationAwareLogger.ERROR_INT, message, NULL_ARGS, t);
+		}
+	}
+	
+	@Override
+	public boolean isTraceEnabled() {
+		return log.isTraceEnabled();
+	}
+	
+	@Override
+	public boolean isDebugEnabled() {
+		return log.isDebugEnabled();
+	}
+	
+	@Override
+	public boolean isInfoEnabled() {
+		return log.isInfoEnabled();
+	}
+	
+	@Override
+	public boolean isWarnEnabled() {
+		return log.isWarnEnabled();
+	}
+	
+	@Override
+	public boolean isErrorEnabled() {
+		return log.isErrorEnabled();
+	}
+	
+	@Override
+	public boolean isFatalEnabled() {
+		// throw new UnsupportedOperationException("slf4j logger does not support fatal level");
+		return log.isErrorEnabled();
+	}
+	
+	// -------------------------------------------------------
+	
+	public void trace(String format, Object... args) {
+		if (isTraceEnabled()) {
+			log.log(null, callerFQCN, LocationAwareLogger.TRACE_INT, format, args, null);
+		}
+	}
+	
+	public void debug(String format, Object... args) {
+		if (isDebugEnabled()) {
+			log.log(null, callerFQCN, LocationAwareLogger.DEBUG_INT, format, args, null);
+		}
+	}
+	
+	public void info(String format, Object... args) {
+		if (isInfoEnabled()) {
+			log.log(null, callerFQCN, LocationAwareLogger.INFO_INT, format, args, null);
+		}
+	}
+	
+	public void warn(String format, Object... args) {
+		if (isWarnEnabled()) {
+			log.log(null, callerFQCN, LocationAwareLogger.WARN_INT, format, args, null);
+		}
+	}
+	
+	public void error(String format, Object... args) {
+		if (isErrorEnabled()) {
+			log.log(null, callerFQCN, LocationAwareLogger.ERROR_INT, format, args, null);
+		}
+	}
+	
+	public void fatal(String format, Object... args) {
+		// throw new UnsupportedOperationException("slf4j logger does not support fatal level");
+		if (isFatalEnabled()) {
+			log.log(null, callerFQCN, LocationAwareLogger.ERROR_INT, format, args, null);
+		}
+	}
+}
+
+
+
+
+

+ 184 - 0
src/main/java/com/jfinal/log/Slf4jLogFactory.java

@@ -0,0 +1,184 @@
+/**
+ * Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.com).
+ *
+ * Licensed 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 com.jfinal.log;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.slf4j.spi.LocationAwareLogger;
+
+/**
+ * Slf4jLogFactory
+ */
+public class Slf4jLogFactory implements ILogFactory {
+	
+	@Override
+	public Log getLog(Class<?> clazz) {
+		Logger log = LoggerFactory.getLogger(clazz);
+		return log instanceof LocationAwareLogger ? new Slf4jLog((LocationAwareLogger)log) : new Slf4jSimpleLog(log);
+	}
+	
+	@Override
+	public Log getLog(String name) {
+		Logger log = LoggerFactory.getLogger(name);
+		return log instanceof LocationAwareLogger ? new Slf4jLog((LocationAwareLogger)log) : new Slf4jSimpleLog(log);
+	}
+	
+	/**
+	 * Slf4jSimpleLog 支持 slf4j-simple
+	 */
+	public static class Slf4jSimpleLog extends Log {
+		
+		private org.slf4j.Logger log;
+		
+		Slf4jSimpleLog(org.slf4j.Logger log) {
+			this.log = log;
+		}
+		
+		@Override
+		public void debug(String message) {
+			log.debug(message);
+		}
+		
+		@Override
+		public void debug(String message, Throwable t) {
+			log.debug(message, t);
+		}
+		
+		@Override
+		public void info(String message) {
+			log.info(message);
+		}
+		
+		@Override
+		public void info(String message, Throwable t) {
+			log.info(message, t);
+		}
+		
+		@Override
+		public void warn(String message) {
+			log.warn(message);
+		}
+		
+		@Override
+		public void warn(String message, Throwable t) {
+			log.warn(message, t);
+		}
+		
+		@Override
+		public void error(String message) {
+			log.error(message);
+		}
+		
+		@Override
+		public void error(String message, Throwable t) {
+			log.error(message, t);
+		}
+		
+		@Override
+		public void fatal(String message) {
+			log.error(message);
+		}
+		
+		@Override
+		public void fatal(String message, Throwable t) {
+			log.error(message, t);
+		}
+		
+		@Override
+		public boolean isDebugEnabled() {
+			return log.isDebugEnabled();
+		}
+		
+		@Override
+		public boolean isInfoEnabled() {
+			return log.isInfoEnabled();
+		}
+		
+		@Override
+		public boolean isWarnEnabled() {
+			return log.isWarnEnabled();
+		}
+		
+		@Override
+		public boolean isErrorEnabled() {
+			return log.isErrorEnabled();
+		}
+		
+		@Override
+		public boolean isFatalEnabled() {
+			return log.isErrorEnabled();
+		}
+		
+		// -------------------------------------------------------
+		
+		public boolean isTraceEnabled() {
+			return log.isTraceEnabled();
+		}
+		
+		public void trace(String message) {
+			log.trace(message);
+		}
+		
+		public void trace(String message, Throwable t) {
+			log.trace(message, t);
+		}
+		
+		// -------------------------------------------------------
+		
+		public void trace(String format, Object... args) {
+			if (isTraceEnabled()) {
+				log.trace(format, args);
+			}
+		}
+		
+		public void debug(String format, Object... args) {
+			if (isDebugEnabled()) {
+				log.debug(format, args);
+			}
+		}
+		
+		public void info(String format, Object... args) {
+			if (isInfoEnabled()) {
+				log.info(format, args);
+			}
+		}
+		
+		public void warn(String format, Object... args) {
+			if (isWarnEnabled()) {
+				log.warn(format, args);
+			}
+		}
+		
+		public void error(String format, Object... args) {
+			if (isErrorEnabled()) {
+				log.error(format, args);
+			}
+		}
+		
+		public void fatal(String format, Object... args) {
+			if (isFatalEnabled()) {
+				log.error(format, args);
+			}
+		}
+	}
+}
+
+
+
+
+
+