实际开发中,记录日志是常用的功能,jboss默认情况下已经记录了很多运行日志,如果开发人员要手动在server.log中写入日志,可以参考下面的方法:
1 package utils; 2 3 import java.util.logging.Level; 4 import java.util.logging.Logger; 5 6 public final class LoggerUtils { 7 8 private final static Logger LOGGER = Logger.getLogger(LoggerUtils.class 9 .toString()); 10 11 public static void info(String msg) { 12 LOGGER.info(msg); 13 System.out.println(msg); 14 } 15 16 public static void severe(String msg) { 17 LOGGER.log(Level.SEVERE, msg); 18 System.out.println(msg); 19 } 20 21 }
LoggerUtils
有了这个类后,其它地方就可以方便使用了,比如在web应用中,可以在ManagedBean上直接用:
1 package controller; 2 3 import javax.inject.Named; 4 import utils.LoggerUtils; 5 6 @Named("Index") 7 public class IndexController { 8 9 public String sayHello() { 10 String msg = "hello ,this is a log message for test"; 11 LoggerUtils.info(msg); 12 LoggerUtils.severe(msg); 13 14 return "Hello Logeer!"; 15 16 } 17 18 }
IndexController
随便建个xhtml页面调用一下:
<b>#{Index.sayHello()}</b>
最后运行效果如下:
示例源代码下载:jboss-logger-test.zip
时间: 2024-10-31 10:06:39