View Javadoc
1   package gboat2.base.logging.util;
2   
3   import gboat2.base.bridge.GboatAppContext;
4   import gboat2.base.bridge.util.PropertiesUtil;
5   import gboat2.base.core.logging.Level;
6   
7   import java.io.File;
8   import java.util.Arrays;
9   import java.util.Properties;
10  
11  import org.apache.commons.lang3.StringUtils;
12  import org.apache.log4j.helpers.LogLog;
13  import org.slf4j.LoggerFactory;
14  
15   /** 
16   * @author zhangxj-a
17   * @author hemw
18   * @date 2012-3-20
19   */
20  public class Priority {
21  	
22  	/** 业务日志地址LOG_CONFIG_LOCATION */
23  	public final static String LOG_CONFIG_LOCATION = "WEB-INF/config/businessLog.properties";
24  	
25  	/** 业务日志参数 */
26  	public final static String BUSINESS_LOG_KEY = "business.level";
27  	
28  	/** 默认的日志级别 */
29  	public static Level DEFAULT_LEVEL = Level.DEBUG;
30  	
31  	protected Level level;
32  	
33  	private static final Priority instance = new Priority();
34  	
35      /**
36       * 私有构造方法,防止在外部被实例化
37       */
38      private Priority() {
39          File file = new File(getLogRealPath()); // 获得日志文件的绝对路径
40          if (file.exists()) {
41              Properties props = PropertiesUtil.loadProperties(file);
42              try {
43                  level = Level.valueOf(StringUtils.upperCase(props.getProperty(BUSINESS_LOG_KEY)));
44              } catch (IllegalArgumentException e) { // No enum const
45                  LoggerFactory.getLogger(Priority.class).warn(
46                          "业务日志配置文件 [{}] 中没有指定 [{}] 的值,或指定的值无效,将使用默认的日志级别 [{}],允许指定的值有:{}", LOG_CONFIG_LOCATION,
47                          BUSINESS_LOG_KEY, DEFAULT_LEVEL, Arrays.toString(Level.values()));
48                  level = DEFAULT_LEVEL;
49              }
50          } else {
51              LoggerFactory.getLogger(Priority.class).warn("没有找到业务日志的配置文件 [{}],将使用默认的日志级别 [{}]",
52                      LOG_CONFIG_LOCATION, DEFAULT_LEVEL);
53              level = DEFAULT_LEVEL;
54          }
55      }
56  
57      /**
58       * @return 获取业务日志配置文件的绝对位置
59       */
60      protected String getLogRealPath() {
61          String webRootPath = GboatAppContext.getWebRootPath();
62          if (webRootPath == null) {
63              LogLog.warn("can't get web app root, please add context param into web.xml like below:\r\n" +
64                      "<context-param>\r\n" +
65                      "    <param-name>webAppRootKey</param-name>\r\n" +
66                      "    <param-value>webapp.gboat2.root</param-value>\r\n" +
67                      "</context-param>");
68              return LOG_CONFIG_LOCATION;
69          }
70          
71          return webRootPath + LOG_CONFIG_LOCATION;
72      }
73  
74      /**
75       * @return 获取单例
76       */
77      public static Priority getInstance() {
78          return instance;
79      }
80  
81  	/**
82  	 * Check whether business log used is enabled for <code>DEBUG</code>  .
83  	 * @return
84  	 */
85  	public boolean isDebugEnabled() {
86          return isEnabled(Level.DEBUG);
87  	}
88  	/**
89  	 * Check whether business log used is enabled for <code>INFO</code>  .
90  	 * @return
91  	 */
92  	public boolean isInfoEnabled() {
93          return isEnabled(Level.INFO);
94  	}
95  	/**
96  	 * Check whether business log used is enabled for <code>WARN</code>  .
97  	 * @return
98  	 */
99  	public boolean isWarnEnabled() {
100         return isEnabled(Level.WARN);
101 	}
102 	
103 	/**
104 	 * Check whether business log used is enabled for <code>ERROR</code>  .
105 	 * @return
106 	 */
107 	public boolean isErrorEnabled() {
108 		return isEnabled(Level.ERROR);
109 	}
110 
111     /**
112      * @param level
113      * @return
114      */
115     public boolean isEnabled(Level level) {
116         return level.getValue() >= this.level.getValue();
117     }
118 
119     /**
120      * 设置默认的日志级别
121      * @param defaultLevel
122      */
123     public static void setDefaultLevel(Level defaultLevel) {
124         if(defaultLevel != null) {
125             DEFAULT_LEVEL = defaultLevel;            
126         }
127     }
128     
129 }