Backwards compatible Logger.warn(ing) in Elixir

Elixir 1.15 deprecated the Logger.warn/1 function:

require Logger

Logger.warn("A warn!")
warning: Logger.warn/1 is deprecated. Use Logger.warning/2 instead

The Logger.warning/2 function was added to Elixir in 1.11.0. So if, for SemVer-related reasons, your library still supports 1.10, you’ll get an UndefinedFunctionError when calling Logger.warning/2 on any version before 1.11.0:

require Logger

Logger.warning("A warning!")
w** (UndefinedFunctionError) function Logger.warning/1 is undefined or private. Did you mean one of:

      * warn/1
      * warn/2

A workaround, at least until you finally drop 1.9, is to add a backwards compatibility helper that delegates to the appropriate function based on System.version/0:

defmodule BackwardsCompatibleLogger do
  require Logger

  case Version.compare(System.version(), "1.11.0") do
    :lt -> defdelegate warning(message), to: Logger, as: :warn
    _ -> defdelegate warning(message), to: Logger
  end
end

BackwardsCompatibleLogger.warning("A warn(ing)!")
[warning] A warn(ing)!