Java のログ出力がローカライズされて鬱陶しい

f:id:Naotsugu:20170705221406p:plain

例えば JUL のログレベルや、H2 database や Glassfish などのログメッセージなどローカライズされていて鬱陶しい。

エンコーディングが違ったりすると読めないし。

ログ内容で検索しても国外の情報拾えないし。


起動時のシステムプロパティで変更する

起動時に -D のオプションで 言語コード、国コードを指定して変更する。

-Dproperty=value
システムプロパティの値を設定します。 
value が、スペースを含む文字列である場合は、文字列を二重引用符で囲む必要があります。

すなわち以下のようにする。

java -Duser.language=en -Duser.country=US

とか

java -Duser.language=ja -Duser.country=JP

実行時にロケールを変更する

実行時に以下のようにしても上手く反映されない。

System.setProperty("user.country","US");
System.setProperty("user.language","en");

ロケールはクラスロード時にデフォルトロケールをメモリに入れてる。

public final class Locale implements Cloneable, Serializable {

    private volatile static Locale defaultLocale = initDefault();

    private static Locale initDefault() {
        String language, region, script, country, variant;
        language = AccessController.doPrivileged(
            new GetPropertyAction("user.language", "en"));
        // for compatibility, check for old user.region property
        region = AccessController.doPrivileged(
            new GetPropertyAction("user.region"));
        if (region != null) {
            // region can be of form country, country_variant, or _variant
            int i = region.indexOf('_');
            if (i >= 0) {
                country = region.substring(0, i);
                variant = region.substring(i + 1);
            } else {
                country = region;
                variant = "";
            }
            script = "";
        } else {
            script = AccessController.doPrivileged(
                new GetPropertyAction("user.script", ""));
            country = AccessController.doPrivileged(
                new GetPropertyAction("user.country", ""));
            variant = AccessController.doPrivileged(
                new GetPropertyAction("user.variant", ""));
        }

        return getInstance(language, script, country, variant, null);
    }

}

なので、あとからシステムプロパティを設定してもロケールは変更できない。

直接ロケールにデフォルトを設定すれば変更できる。

Locale.setDefault(Locale.US);

ついでにシステムプロパティの値も変えておくと良い。

System.setProperty("user.country","US");
System.setProperty("user.language","en");