{"record":{"id":"cb1939583c75fe72","repo":"mybatis/mybatis-3","slug":"error-creating-logger-for-logger-cause","errorCode":null,"errorMessage":"Error creating logger for logger {}.  Cause: {}","messagePattern":"Error creating logger for logger (.+?)\\.  Cause: (.+?)","errorType":"exception","errorClass":"LogException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/logging/LogFactory.java","lineNumber":56,"sourceCode":"    tryImplementation(LogFactory::useLog4J2Logging);\n    tryImplementation(LogFactory::useLog4JLogging);\n    tryImplementation(LogFactory::useJdkLogging);\n    tryImplementation(LogFactory::useNoLogging);\n  }\n\n  private LogFactory() {\n    // disable construction\n  }\n\n  public static Log getLog(Class<?> clazz) {\n    return getLog(clazz.getName());\n  }\n\n  public static Log getLog(String logger) {\n    try {\n      return logConstructor.newInstance(logger);\n    } catch (Throwable t) {\n      throw new LogException(\"Error creating logger for logger \" + logger + \".  Cause: \" + t, t);\n    }\n  }\n\n  public static void useCustomLogging(Class<? extends Log> clazz) {\n    setImplementation(clazz);\n  }\n\n  public static void useSlf4jLogging() {\n    setImplementation(org.apache.ibatis.logging.slf4j.Slf4jImpl.class);\n  }\n\n  public static void useCommonsLogging() {\n    setImplementation(org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.class);\n  }\n\n  /**\n   * @deprecated Since 3.5.9 - See https://github.com/mybatis/mybatis-3/issues/1223. This method will remove future.\n   */","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/logging/LogFactory.java#L38-L74","documentation":"LogFactory.getLog(String) throws LogException when instantiating the selected Log implementation fails — logConstructor.newInstance(logger) threw. The constructor was resolved earlier by setImplementation(); failure at this point usually means the underlying logging library threw in its constructor or the class could not be initialized at runtime (missing dependency after repackaging, classloader isolation).","triggerScenarios":"Any first use of MyBatis logging (getLog called during mapper initialization) after a setImplementation/useSlf4jLogging-style call succeeded but the adapter's constructor (or static init of the underlying logger like Log4j2/Slf4j classes) fails in the deployment classloader.","commonSituations":"Application-server classloader hierarchies where the logging jar is visible to LogFactory but not instantiable; shaded/uber-jar builds stripping logging classes; OSGi deployments; logging backend throwing during initialization (misconfigured Log4j2 config).","solutions":["Check the chained cause — it names the exact class/init failure (NoClassDefFoundError, ClassNotFoundException, backend exception).","Ensure the chosen logging backend jar (slf4j-api, log4j, etc.) is on the runtime classpath at the same classloader level as mybatis.","Switch to a backend you actually ship, e.g. LogFactory.useSlf4jLogging() when only SLF4J is present, or useStdOutLogging() for isolated tests.","In shaded jars, keep META-INF services and logging packages un-relocated or exclude them from shading."],"exampleFix":"// before: backend classes missing at runtime\nLogFactory.useLog4J2Logging(); // resolves, then fails per-logger\n\n// after: force a backend you control\nLogFactory.useSlf4jLogging(); // slf4j-api is on the classpath","handlingStrategy":"try-catch","validationCode":"// Probe the backend once at startup before any mapper work\ntry {\n  LogFactory.getLog(\"startup-probe\");\n} catch (LogException e) {\n  LogFactory.useStdOutLogging(); // known-good fallback for isolated environments\n  LogFactory.getLog(\"startup-probe\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  log = LogFactory.getLogger(Xxx.class);\n} catch (LogException e) {\n  // fall back to a logger you control; do not let logging kill initialization\n  log = internalStdOutLog;\n}","preventionTips":["Pin exactly one logging backend in dependencies so adapter probing is deterministic.","In shaded/OSGi deployments, verify the logging packages remain loadable by the same classloader as mybatis.","Smoke-test LogFactory.getLog() during application startup, when falling back is still cheap."],"tags":["mybatis","logging","classpath","classloader"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}