解決編: logbackã§åä¸ã¬ãã«ã®è¤æ°ãã°ãç°ãªãåºåå ã«åºã
ååã®ã疑問メモ: logbackで同一レベルの複数ログを異なる出力先に出したいãã®è§£æ±ºç·¨ããåä¸ã¬ãã«ã®ãã°ãç¨éã«å¿ãã¦åºãåããã話ã®ç¶ãã§ãã
師å ãã参考になる情報をいただきãç¡äºå®ç¾ã§ãã¾ããããããã¨ããããã¾ãã
JaninoEventEvaluator ãæ´»ç¨ããã°ããã®ã§ããã
JaninoEventEvaluator ã使ãã«ã¯ Janino libraryã追å ã§å¿ è¦ãªã®ã§ãæºåãã¾ãã
- http://docs.codehaus.org/display/JANINO/Download ãããææ°çï¼æ¬æ¥æç¹ã§ã¯2.6.1ï¼ããã¦ã³ãã¼ããã
- zipã解åãã¦ãåºã¦ãã janino.jar 㨠commons-compiler.jar ããã«ããã¹ã«éã
è¨å®ãã¡ã¤ã«ï¼logback.xmlï¼ã次ã®ããã«ãã¾ãã
<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="FILE" class="ch.qos.logback.core.FileAppender"> <filter class="ch.qos.logback.core.filter.EvaluatorFilter"> <evaluator> <expression>return message.contains("> ");</expression> </evaluator> <OnMismatch>ACCEPT</OnMismatch> <OnMatch>DENY</OnMatch> </filter> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>DENY</onMatch> <onMismatch>ACCEPT</onMismatch> </filter> <File>sample.log</File> <Append>true</Append> <encoder> <pattern>%d %5p %c{1} - %m%n</pattern> </encoder> </appender> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <filter class="ch.qos.logback.core.filter.EvaluatorFilter"> <evaluator> <!-- defaults to type ch.qos.logback.classic.boolex.JaninoEventEvaluator --> <expression>return message.contains("> ");</expression> </evaluator> <OnMismatch>DENY</OnMismatch> <OnMatch>ACCEPT</OnMatch> </filter> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <encoder> <Pattern>%msg%n</Pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="STDOUT" /> <appender-ref ref="FILE" /> </root> </configuration>
ä»åã¯ãã¡ãã»ã¼ã¸ã«ã> ãã¨ããæååãå«ã¾ãã¦ããã°ã³ã³ã½ã¼ã«ã«åºåããããã§ãªããã°ãã¡ã¤ã«ã«åºåããããã«ãã¾ããã
åããã³ã¼ãã®ãã°åºåé¨åãããã®åæãè¸ã¾ããå½¢å¼ã«å¤æ´ãã¾ãã
package xxx.yyy.zzz; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.Marker; import org.slf4j.MarkerFactory; import xxx.yyy.zzz.impl.Foo; public class Main { private static final Logger logger = LoggerFactory.getLogger(Main.class); private static Marker msg = MarkerFactory.getMarker("MESSAGE"); private static Marker detail = MarkerFactory.getMarker("DETAIL"); public static void main(String[] args) { QQQ foo = new Foo("foo"); logger.info(msg, "> hello!"); logger.info(detail, "mogamoga(for developers)"); foo.execute(); logger.info(msg, "> bye!"); // for sample logger.debug("Main debug hogehoge"); logger.warn("Main warn hogehoge"); logger.error("Main error hogehoge"); } }
æ®ãã®ã³ã¼ãï¼ã¤ã³ã¿ãã§ã¼ã¹QQQ.javaã¨ãå®è£ ã¯ã©ã¹Foo.javaï¼ã¯ãååã¨åãã§ãã
ã³ã³ã½ã¼ã«åºåçµæã
> hello! > bye!
ãã¡ã¤ã«åºåçµæã
2012-06-19 01:50:46,887 INFO x.y.z.Main - mogamoga(for developers) 2012-06-19 01:50:46,889 DEBUG x.y.z.i.Foo - Foo#execute done. 2012-06-19 01:50:46,889 INFO x.y.z.i.Foo - Foo info hogehoge 2012-06-19 01:50:46,889 WARN x.y.z.i.Foo - Foo warn hogehoge 2012-06-19 01:50:46,889 ERROR x.y.z.i.Foo - Foo error hogehoge 2012-06-19 01:50:46,889 DEBUG x.y.z.Main - Main debug hogehoge 2012-06-19 01:50:46,889 WARN x.y.z.Main - Main warn hogehoge 2012-06-19 01:50:46,889 ERROR x.y.z.Main - Main error hogehoge
ãã£ãã¼ããããããããã£ãããã¨ã¼ã
expression ã®è¦ç´ ã«ã¯ãå¼ãæ¸ãã¾ããã»ãã³ãã³ã§åºåã£ã¦è¤æ°ã®å¼ãæ¸ããã¨ãã§ãã¾ãããifæãªãããæ¸ãã¾ããï¼ã ããcommons-compilerãå¿ è¦ã«ãªããã§ããï¼
ã»ãã«ããããã¥ã¡ã³ãã®ãµã³ãã«ãè¦ãã¨ãã¨ã©ã¼ãªãã¸ã§ã¯ãã«è©°ãè¾¼ãã ç¶æ ãè¦ã¦å¦çãåå²ããããããªå¦çããã£ã¦ãã¾ããã¨ã©ã¼ã®ã¹ãã¼ã¿ã¹ã³ã¼ãã404ã ã£ãããç¹å®ã®URIã«ãªã¯ã¨ã¹ããæãããã ã¨ãããã°ã®è¨å®ãã¡ã¤ã«ã«æ¸ãã«ã¯ãã¡ã¨è¤éãªå¦çã®ãããªæ°ããã¾ããããããã使ãæ¹ããããã§ããã
logbackãããã