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
17
18
19
20 public class Priority {
21
22
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) {
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
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
76
77 public static Priority getInstance() {
78 return instance;
79 }
80
81
82
83
84
85 public boolean isDebugEnabled() {
86 return isEnabled(Level.DEBUG);
87 }
88
89
90
91
92 public boolean isInfoEnabled() {
93 return isEnabled(Level.INFO);
94 }
95
96
97
98
99 public boolean isWarnEnabled() {
100 return isEnabled(Level.WARN);
101 }
102
103
104
105
106
107 public boolean isErrorEnabled() {
108 return isEnabled(Level.ERROR);
109 }
110
111
112
113
114
115 public boolean isEnabled(Level level) {
116 return level.getValue() >= this.level.getValue();
117 }
118
119
120
121
122
123 public static void setDefaultLevel(Level defaultLevel) {
124 if(defaultLevel != null) {
125 DEFAULT_LEVEL = defaultLevel;
126 }
127 }
128
129 }