BeanUtils icon indicating copy to clipboard operation
BeanUtils copied to clipboard

Logger must use log4J

Open Huizhe-Yu opened this issue 5 years ago • 1 comments

com.tuyang.beanutils.internal.logger.Logger

springboot recommends logback, but the jar must use the log4j, Otherwise, it will directly system.out.println. it does not make sense,

public static Logger getLogger(Class<?> clazz) {
	 try {
		Class<?> loggerClass = Logger.class.getClassLoader().loadClass("org.apache.log4j.Logger");
		Method method = loggerClass.getMethod("getLogger", Class.class);
		Object logger = method.invoke(null, clazz);
		return new Logger(logger);
	}catch (Exception e) {
			
	}
	return new Logger(null);
}


public void info(Object message, Throwable t) {
	if( logger != null ) {
		try {
			Method method = logger.getClass().getMethod("info", Object.class, Throwable.class);
			method.invoke(logger, message, t);
		} catch (Exception e) {
			e.printStackTrace();
		}
	} else {
		if( BeanCopyConfig.instance().getLogLevel() < LogLevelInfo )
			return;
		
		System.out.println(message.toString());
		t.printStackTrace();
	}
}

Huizhe-Yu avatar Jun 16 '20 13:06 Huizhe-Yu

It is designed for common use, not only for springboot. And it is easy to extend log4j to logback.

yangtu222 avatar Jul 18 '20 03:07 yangtu222