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 org.slf4j.ILoggerFactory; 020import org.slf4j.Logger; 021import org.slf4j.LoggerFactory; 022import org.slf4j.helpers.NOPLoggerFactory; 023import org.slf4j.spi.LocationAwareLogger; 024 025/** 026 * @author michael 027 */ 028public class JbootLogFactory extends com.jfinal.log.Slf4jLogFactory { 029 030 private boolean useJdkLogger = false; 031 032 public JbootLogFactory() { 033 boolean hasStaticLoggerBinder = false; 034 try { 035 Class.forName("org.slf4j.impl.StaticLoggerBinder"); 036 hasStaticLoggerBinder = true; 037 } catch (ClassNotFoundException e) {} 038 039 if (!hasStaticLoggerBinder) { 040 useJdkLogger = true; 041 } else { 042 ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory(); 043 if (loggerFactory.getClass() == NOPLoggerFactory.class){ 044 useJdkLogger = true; 045 }else { 046 System.out.println("Jboot LoggerFactory: " + loggerFactory.getClass().getName()); 047 } 048 } 049 } 050 051 @Override 052 public Log getLog(Class<?> clazz) { 053 if (useJdkLogger) { 054 return new JdkLogger(clazz); 055 } 056 057 Logger log = LoggerFactory.getLogger(clazz); 058 return log instanceof LocationAwareLogger ? new Slf4jLogger((LocationAwareLogger) log) : new Slf4jSimpleLogger(log); 059 } 060 061 @Override 062 public Log getLog(String name) { 063 if (useJdkLogger) { 064 return new JdkLogger(name); 065 } 066 067 Logger log = LoggerFactory.getLogger(name); 068 return log instanceof LocationAwareLogger ? new Slf4jLogger((LocationAwareLogger) log) : new Slf4jSimpleLogger(log); 069 } 070 071 072}