SSブログ

How to separate access log from app log in Restlet [技術]

This is a memo on Restlet logging configuration based on java.util.logging. There is an article in Talend's site, but the description was not all that clear to me. I had trouble separating app log from access log and it took almost one full day to figure this out. The Restlet version I used is 2.3 because that is the stable version available in Maven repository.

In short, you need 2 separate loggers configured in the app, one for app log and the other for access log. This may be obvious if you know java.util.logging well. It is worth noting that the access log comes from the Restlet framework itself even though you modify its behaviour by configuration. You do not write particular code for access logs, except for initialization.

The following is the configuration file for java.util.logging. I call it "logging.config".
# These are for app logging.
com.example.myapp.level=ALL
com.example.myapp.handlers=java.util.logging.FileHandler
com.example.myapp.useParentHandlers=false

# These are for access logging.
com.example.myapp.www.level=INFO
com.example.myapp.www.handlers=org.restlet.engine.log.AccessLogFileHandler
com.example.myapp.www.useParentHandlers=false

# Referenced from com.example.myapp
java.util.logging.FileHandler.level=ALL
java.util.logging.FileHandler.formatter=com.example.myapp.LogFormatter
java.util.logging.FileHandler.limit=10000000
java.util.logging.FileHandler.count=5
java.util.logging.FileHandler.pattern=./myapp-%u-%g.log

# Referenced from com.example.myapp.www
org.restlet.engine.log.AccessLogFileHandler.level=ALL
org.restlet.engine.log.AccessLogFileHandler.limit=10000000
org.restlet.engine.log.AccessLogFileHandler.count=5
org.restlet.engine.log.AccessLogFileHandler.pattern=./myapp-access-%u-%g.log

In the first part, I declare two different name spaces, one for app logging (com.example.myapp) and the other for access logging (com.example.myapp.www). Each one refers to a log handler in the bottom half. I have written a small Java code "com.example.myapp.LogFormatter" for log formatting, which is referenced from FileHandler configuration.

Then I give it to JVM by this parameter at startup:
-Djava.util.logging.config.file=./logging.config

Next, in my code, I have configured two separate loggers as follows. Both are in the initialization section of the program. The first one is for app logging.
import java.util.logging.Logger;
...
// for app log
logger = Logger.getLogger("com.example.myapp");
...
logger.info("This is app log.");

The Java package name "com.example.myapp", or reverse domain name, whatever you call it, can be anything as long as it matches the namespace in the configuration file.

Then, the second set of parameters will let the Restlet framework write access logs to a separate file whenever a request comes from a client. This is the code to configure the framework:
import org.restlet.Component;
...
Component component = new Component();
// for access log
component.getLogService().setLoggerName("com.example.myapp.www");

Again, the package name "com.example.myapp.www" must match the configuration in logging.config.

The configuration file and codes combined, I could separate access log from app log in two distinct files.
nice!(0)  コメント(0) 
共通テーマ:日記・雑感

nice! 0

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

※ブログオーナーが承認したコメントのみ表示されます。

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。