3

tomcat/confディレクトリ内に共通のlogging.propertiesがあります。

アプリケーションによって生成されたすべてのログを(Javaクラスで)処理できるように変更するにはどうすればよいですか?

(Log APIからの)ラッパーを使用せずにそれを行う方法はありますか?

4

1 に答える 1

0

Handlerサブクラスを使用して、ログに記録されるデータを処理できます。

addHandler次に、構成ファイルを介して、またはプログラムでLoggerクラスのメソッドを介して、ルートロガーに追加できます。

たとえば、「Foobar」テキストのプレフィックスが付いたすべてのログデータをコンソールに出力するHandlerサブクラスFoobarHandlerを作成できます。

public class MyHandler extends Handler {

    @Override
    public void close() throws SecurityException {
        // housekeeping before handler close
    }

    @Override
    public void flush() {
        // not really needed as data is processed without any sort of buffering
    }

    @Override
    public void publish(LogRecord record) {
        System.out.println("Foobar " + record.getMessage().toUpperCase());
    }

}

構成ファイルで、ハンドラーのクラス名(つまりmyapp.FoobarHandler)をグローバルハンドラー構成に追加します。

# Global logging properties.
# ------------------------------------------
# The set of handlers to be loaded upon startup.
# Comma-separated list of class names.
# (? LogManager docs say no comma here, but JDK example has comma.)
handlers=java.util.logging.FileHandler, myapp.FoobarHandler

# Default global logging level.
# Loggers and Handlers may override this level
.level=INFO

# Loggers
# ------------------------------------------
# Loggers are usually attached to packages.
# Here, the level for each package is specified.
# The global level is used by default, so levels
# specified here simply act as an override.
myapp.ui.level=ALL
myapp.business.level=CONFIG
myapp.data.level=SEVERE 
于 2013-02-19T19:56:07.327 に答える