{"record":{"id":"4c98a35ebd830fa9","repo":"mybatis/mybatis-3","slug":"error-setting-log-implementation-cause","errorCode":null,"errorMessage":"Error setting Log implementation.  Cause: {}","messagePattern":"Error setting Log implementation\\.  Cause: (.+?)","errorType":"exception","errorClass":"LogException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/logging/LogFactory.java","lineNumber":116,"sourceCode":"      try {\n        runnable.run();\n      } catch (Throwable t) {\n        // ignore\n      }\n    }\n  }\n\n  private static void setImplementation(Class<? extends Log> implClass) {\n    lock.lock();\n    try {\n      Constructor<? extends Log> candidate = implClass.getConstructor(String.class);\n      Log log = candidate.newInstance(LogFactory.class.getName());\n      if (log.isDebugEnabled()) {\n        log.debug(\"Logging initialized using '\" + implClass + \"' adapter.\");\n      }\n      logConstructor = candidate;\n    } catch (Throwable t) {\n      throw new LogException(\"Error setting Log implementation.  Cause: \" + t, t);\n    } finally {\n      lock.unlock();\n    }\n  }\n\n}\n","sourceCodeStart":98,"sourceCodeEnd":123,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/logging/LogFactory.java#L98-L123","documentation":"LogFactory.setImplementation(Class) throws LogException when the candidate Log implementation cannot be adopted: it has no constructor accepting String, instantiating it fails, or invoking isDebugEnabled/debug throws. This runs for every use*Logging() call and during static initialization when LogFactory probes the classpath for the first available adapter.","triggerScenarios":"Calling useCustomLogging(MyLog.class) where MyLog lacks a public Log(String) constructor or throws in it; calling useSlf4jLogging()/useLog4J2Logging() etc. when the corresponding backend jar is absent (the probe's newInstance throws).","commonSituations":"Custom Log implementations with wrong constructor signature; SLF4J/Log4j2/Logging commons jar missing from dependencies so the explicit switch fails; static-init probing order changed across MyBatis versions producing a different first-pick failure.","solutions":["If using useCustomLogging, give the class a public constructor taking a single String and make sure it does not throw.","Add the missing backend dependency (e.g. org.slf4j:slf4j-api) that matches the use*Logging() call.","Check the chained cause to see whether it is a NoSuchMethodException (constructor) or a backend instantiation error (classpath).","As a last resort for isolated tests, use LogFactory.useStdOutLogging()."],"exampleFix":"// before\npublic class MyLog implements Log {\n  public MyLog() {} // no String constructor -> setImplementation fails\n}\nLogFactory.useCustomLogging(MyLog.class);\n\n// after\npublic class MyLog implements Log {\n  public MyLog(String name) {} // matches required signature\n}\nLogFactory.useCustomLogging(MyLog.class);","handlingStrategy":"try-catch","validationCode":"// Verify the adapter class satisfies the contract before switching\nvoid safeUse(Class<? extends Log> impl) {\n  try {\n    impl.getConstructor(String.class); // required signature\n  } catch (NoSuchMethodException e) {\n    throw new IllegalStateException(impl + \" needs a public Log(String) constructor\");\n  }\n  LogFactory.useCustomLogging(impl);\n}","typeGuard":null,"tryCatchPattern":"try {\n  LogFactory.useSlf4jLogging();\n} catch (LogException e) {\n  throw new IllegalStateException(\"slf4j-api missing on classpath; add org.slf4j:slf4j-api\", e);\n}","preventionTips":["Give custom Log implementations a public (String) constructor that never throws.","Match each use*Logging() call with the corresponding backend dependency in the build file.","Fail fast at startup rather than discovering logging problems during first SQL execution."],"tags":["mybatis","logging","configuration","classpath"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}