背景:与(一)相同,避免主项目pom中众多log jar包冲突(比如:java日志组件的关系 slf4j logback log4j ),套路还是一样
现成的代码继承:load/find class 与 forname 在static代码块加载的不同 (二)非系统类jdbc
static { try { InputStream inputStream2 = JdbcDriverClassLoader.class.getClassLoader().getResourceAsStream("jdbcdriver/ojdbc6-11.2.0.3.resource"); InputStream inputStreamSlf4jApi = JdbcDriverClassLoader.class.getClassLoader().getResourceAsStream("log/slf4j-api-1.7.25.resource"); InputStream inputStreamLogbackCore = JdbcDriverClassLoader.class.getClassLoader().getResourceAsStream("log/logback-core-1.2.3.resource"); InputStream inputStreamLogbackClassic = JdbcDriverClassLoader.class.getClassLoader().getResourceAsStream("log/logback-classic-1.2.3.resource"); jdbcDriverClassLoader = new JdbcDriverClassLoader(new JarInputStream[]{new JarInputStream(inputStream2), new JarInputStream(inputStreamSlf4jApi), new JarInputStream(inputStreamLogbackCore), new JarInputStream(inputStreamLogbackClassic)}); jdbcDriverClassLoader.configureLogback(); } catch (Exception e) { e.printStackTrace(); } } public void configureLogback() throws Exception { Class clLoggerFactory = loadClass("org.slf4j.LoggerFactory"); Method getILoggerFactory = clLoggerFactory.getMethod("getILoggerFactory"); Object loggerContext = getILoggerFactory.invoke(null); Class clLoggerContext = loadClass("ch.qos.logback.classic.LoggerContext"); Method method = clLoggerContext.getMethod("reset"); method.invoke(loggerContext); Class clJoranConfigurator = loadClass("ch.qos.logback.classic.joran.JoranConfigurator"); Object configurator = clJoranConfigurator.newInstance(); URL url = this.getClass().getClassLoader().getResource("xxx-logback.xml"); method = clJoranConfigurator.getMethod("setContext", loadClass("ch.qos.logback.core.Context")); method.invoke(configurator, loggerContext); method = clJoranConfigurator.getMethod("doConfigure", URL.class); method.invoke(configurator, url); }
import java.lang.reflect.Method; public class MyLogbackFactory { public static MyLogger getLogger(Class c) { try { Class cl = JdbcDriverClassLoader.getJdbcDriverClassLoader().loadClass("org.slf4j.LoggerFactory"); Method method = cl.getMethod("getLogger", Class.class); Object log = method.invoke(null, c); ScefLogger scefLogger = new ScefLogger(); scefLogger.setLogger(log); return scefLogger; } catch (Exception e) { throw new RuntimeException(e); } } public static class ScefLogger { public Object getLogger() { return logger; } public void setLogger(Object logger) { this.logger = logger; } Object logger; public void info(String var1) { try { Class logcl = JdbcDriverClassLoader.getJdbcDriverClassLoader().loadClass("org.slf4j.Logger"); Method info = logcl.getMethod("info", String.class); info.invoke(this.logger, var1); } catch (Exception e) { throw new RuntimeException(e) ; } } public void info(String var1, Object... var2) { try { Class logcl = JdbcDriverClassLoader.getJdbcDriverClassLoader().loadClass("org.slf4j.Logger"); Method info = logcl.getMethod("info", String.class, new Object[0].getClass()); info.invoke(this.logger, var1, var2); } catch (Exception e) { throw new RuntimeException(e) ; } } public void debug(String var1, Object... var2) { try { Class logcl = JdbcDriverClassLoader.getJdbcDriverClassLoader().loadClass("org.slf4j.Logger"); Method info = logcl.getMethod("debug", String.class, new Object[0].getClass()); info.invoke(this.logger, var1, var2); } catch (Exception e) { throw new RuntimeException(e) ; } } public void debug(String var1) { try { Class logcl = JdbcDriverClassLoader.getJdbcDriverClassLoader().loadClass("org.slf4j.Logger"); Method info = logcl.getMethod("debug", String.class); info.invoke(this.logger, var1); } catch (Exception e) { throw new RuntimeException(e) ; } } public void warn(String var1, Object... var2) { try { Class logcl = JdbcDriverClassLoader.getJdbcDriverClassLoader().loadClass("org.slf4j.Logger"); Method info = logcl.getMethod("warn", String.class, new Object[0].getClass()); info.invoke(this.logger, var1, var2); } catch (Exception e) { throw new RuntimeException(e) ; } } public void warn(String var1) { try { Class logcl = JdbcDriverClassLoader.getJdbcDriverClassLoader().loadClass("org.slf4j.Logger"); Method info = logcl.getMethod("warn", String.class); info.invoke(this.logger, var1); } catch (Exception e) { throw new RuntimeException(e) ; } } public void error(String var1, Object... var2) { try { Class logcl = JdbcDriverClassLoader.getJdbcDriverClassLoader().loadClass("org.slf4j.Logger"); Method info = logcl.getMethod("error", String.class, new Object[0].getClass()); info.invoke(this.logger, var1, var2); } catch (Exception e) { throw new RuntimeException(e) ; } } public void error(String var1) { try { Class logcl = JdbcDriverClassLoader.getJdbcDriverClassLoader().loadClass("org.slf4j.Logger"); Method info = logcl.getMethod("error", String.class); info.invoke(this.logger, var1); } catch (Exception e) { throw new RuntimeException(e) ; } } } }
private static final MyLogbackFactory.MyLogger logger = ScefLogbackFactory.getLogger(xxx.class); logger.info("create"); logger.info("create {} df {} {}", "xx", "xx", "xx");
原文:https://www.cnblogs.com/silyvin/p/12582740.html