001/** 002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.jboot.core.log; 017 018import com.jfinal.log.Log; 019import io.jboot.exception.JbootExceptionHolder; 020import org.slf4j.helpers.FormattingTuple; 021import org.slf4j.helpers.MessageFormatter; 022import org.slf4j.spi.LocationAwareLogger; 023 024/** 025 * @author michael 026 */ 027public class Slf4jLogger extends Log { 028 029 private LocationAwareLogger log; 030 031 private static final Object[] NULL_ARGS = new Object[0]; 032 private static final String callerFQCN = Slf4jLogger.class.getName(); 033 034 public Slf4jLogger(LocationAwareLogger log) { 035 this.log = log; 036 } 037 038 @Override 039 public void trace(String message) { 040 log.log(null, callerFQCN, LocationAwareLogger.TRACE_INT, message, NULL_ARGS, null); 041 } 042 043 @Override 044 public void trace(String message, Throwable t) { 045 log.log(null, callerFQCN, LocationAwareLogger.TRACE_INT, message, NULL_ARGS, t); 046 } 047 048 @Override 049 public void debug(String message) { 050 log.log(null, callerFQCN, LocationAwareLogger.DEBUG_INT, message, NULL_ARGS, null); 051 } 052 053 @Override 054 public void debug(String message, Throwable t) { 055 log.log(null, callerFQCN, LocationAwareLogger.DEBUG_INT, message, NULL_ARGS, t); 056 } 057 058 @Override 059 public void info(String message) { 060 log.log(null, callerFQCN, LocationAwareLogger.INFO_INT, message, NULL_ARGS, null); 061 } 062 063 @Override 064 public void info(String message, Throwable t) { 065 log.log(null, callerFQCN, LocationAwareLogger.INFO_INT, message, NULL_ARGS, t); 066 } 067 068 @Override 069 public void warn(String message) { 070 log.log(null, callerFQCN, LocationAwareLogger.WARN_INT, message, NULL_ARGS, null); 071 } 072 073 @Override 074 public void warn(String message, Throwable t) { 075 log.log(null, callerFQCN, LocationAwareLogger.WARN_INT, message, NULL_ARGS, t); 076 } 077 078 @Override 079 public void error(String message) { 080 JbootExceptionHolder.hold(message,null); 081 log.log(null, callerFQCN, LocationAwareLogger.ERROR_INT, message, NULL_ARGS, null); 082 } 083 084 @Override 085 public void error(String message, Throwable t) { 086 JbootExceptionHolder.hold(message, t); 087 log.log(null, callerFQCN, LocationAwareLogger.ERROR_INT, message, NULL_ARGS, t); 088 } 089 090 @Override 091 public void fatal(String message) { 092 // throw new UnsupportedOperationException("slf4j logger does not support fatal level"); 093 log.log(null, callerFQCN, LocationAwareLogger.ERROR_INT, message, NULL_ARGS, null); 094 } 095 096 @Override 097 public void fatal(String message, Throwable t) { 098 JbootExceptionHolder.hold(message, t); 099 // throw new UnsupportedOperationException("slf4j logger does not support fatal level"); 100 log.log(null, callerFQCN, LocationAwareLogger.ERROR_INT, message, NULL_ARGS, t); 101 } 102 103 @Override 104 public boolean isTraceEnabled() { 105 return log.isTraceEnabled(); 106 } 107 108 @Override 109 public boolean isDebugEnabled() { 110 return log.isDebugEnabled(); 111 } 112 113 @Override 114 public boolean isInfoEnabled() { 115 return log.isInfoEnabled(); 116 } 117 118 @Override 119 public boolean isWarnEnabled() { 120 return log.isWarnEnabled(); 121 } 122 123 @Override 124 public boolean isErrorEnabled() { 125 return log.isErrorEnabled(); 126 } 127 128 @Override 129 public boolean isFatalEnabled() { 130 // throw new UnsupportedOperationException("slf4j logger does not support fatal level"); 131 return log.isErrorEnabled(); 132 } 133 134 // ------------------------------------------------------- 135 136 @Override 137 public void trace(String format, Object... args) { 138 if (isTraceEnabled()) { 139 FormattingTuple ft = MessageFormatter.arrayFormat(format, args); 140 log.log(null, callerFQCN, LocationAwareLogger.TRACE_INT, ft.getMessage(), NULL_ARGS, ft.getThrowable()); 141 } 142 } 143 144 @Override 145 public void debug(String format, Object... args) { 146 if (isDebugEnabled()) { 147 FormattingTuple ft = MessageFormatter.arrayFormat(format, args); 148 log.log(null, callerFQCN, LocationAwareLogger.DEBUG_INT, ft.getMessage(), NULL_ARGS, ft.getThrowable()); 149 } 150 } 151 152 @Override 153 public void info(String format, Object... args) { 154 if (isInfoEnabled()) { 155 FormattingTuple ft = MessageFormatter.arrayFormat(format, args); 156 log.log(null, callerFQCN, LocationAwareLogger.INFO_INT, ft.getMessage(), NULL_ARGS, ft.getThrowable()); 157 } 158 } 159 160 @Override 161 public void warn(String format, Object... args) { 162 if (isWarnEnabled()) { 163 FormattingTuple ft = MessageFormatter.arrayFormat(format, args); 164 log.log(null, callerFQCN, LocationAwareLogger.WARN_INT, ft.getMessage(), NULL_ARGS, ft.getThrowable()); 165 } 166 } 167 168 @Override 169 public void error(String format, Object... args) { 170 if (isErrorEnabled()) { 171 FormattingTuple ft = MessageFormatter.arrayFormat(format, args); 172 JbootExceptionHolder.hold(ft.getMessage(), ft.getThrowable()); 173 log.log(null, callerFQCN, LocationAwareLogger.ERROR_INT, ft.getMessage(), NULL_ARGS, ft.getThrowable()); 174 } 175 } 176 177 @Override 178 public void fatal(String format, Object... args) { 179 // throw new UnsupportedOperationException("slf4j logger does not support fatal level"); 180 error(format, args); 181 } 182}