alibaba/druid · warning · IllegalArgumentException

logger can not be null

Error message

logger can not be null

What it means

IllegalArgumentException thrown by DruidDataSourceStatLoggerImpl.setLogger(Log) when the argument is null. The stat logger is mandatory (every log/info call dereferences it), so a null logger would cause NPEs later; the setter fails fast instead.

Source

Thrown at core/src/main/java/com/alibaba/druid/pool/DruidDataSourceStatLoggerImpl.java:70

        String property = properties.getProperty("druid.stat.loggerName");
        if (property != null && property.length() > 0) {
            setLoggerName(property);
        }
    }

    public Log getLogger() {
        return logger;
    }

    @Override
    public void setLoggerName(String loggerName) {
        logger = LogFactory.getLog(loggerName);
    }

    @Override
    public void setLogger(Log logger) {
        if (logger == null) {
            throw new IllegalArgumentException("logger can not be null");
        }
        this.logger = logger;
    }

    public boolean isLogEnable() {
        return logger.isInfoEnabled();
    }

    public void log(String value) {
        logger.info(value);
    }

    @Override
    public void log(DruidDataSourceStatValue statValue) {
        if (!isLogEnable()) {
            return;
        }
        Map<String, Object> map = new LinkedHashMap<String, Object>();

View on GitHub (pinned to fa8dc99126)

Solutions

  1. Pass a non-null org.apache.commons.logging.Log (e.g. LogFactory.getLog("DruidDataSourceStat")).
  2. Prefer setLoggerName(String) which resolves the Log itself and will not be null for a valid name.
  3. If the logger should be optional in your wiring, default to a no-op Log (or StdOut) instead of null.

Example fix

// before
statLogger.setLogger(null); // IllegalArgumentException

// after
import org.apache.commons.logging.LogFactory;
statLogger.setLogger(LogFactory.getLog("DruidStat"));
// or
statLogger.setLoggerName("DruidStat");
Defensive patterns

Strategy: validation

Validate before calling

if (logger == null) {
    throw new IllegalArgumentException("logger must be non-null; use setLoggerName if unsure");
}
statLogger.setLogger(logger);

Type guard

// ensure a non-null Log before assignment
java.util.function.Supplier<Log> safe = () ->
    logger != null ? logger : LogFactory.getLog("DruidStat");
statLogger.setLogger(safe.get());

Prevention

When it happens

Trigger: statLogger.setLogger(null) called directly, or a configuration path that resolves a logger name to null and then calls setLogger(null). The guard at line 68-70 throws.

Common situations: Spring XML/YAML wiring a null logger bean; programmatic config where the logger field was never assigned; a property placeholder (e.g. ${druid.stat.logger}) resolving to empty/null; custom monitoring setup constructing the stat logger manually.

Related errors


AI-assisted analysis of alibaba/druid@fa8dc99126 (2026-08-14). Data as JSON: /api/errors/89547b6039983445. Report an issue: GitHub.