Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
JdkLogManager |
|
| 8.833333333333334;8.833 |
1 | /* | |
2 | * This software is licensed under the Apache License, Version 2.0 | |
3 | * (the "License") agreement; you may not use this file except in compliance with | |
4 | * the License. You may obtain a copy of the License at | |
5 | * | |
6 | * http://www.apache.org/licenses/LICENSE-2.0 | |
7 | * | |
8 | * Unless required by applicable law or agreed to in writing, software | |
9 | * distributed under the License is distributed on an "AS IS" BASIS, | |
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
11 | * See the License for the specific language governing permissions and | |
12 | * limitations under the License. | |
13 | */ | |
14 | package net.admin4j.log; | |
15 | ||
16 | import java.util.ArrayList; | |
17 | import java.util.Collections; | |
18 | import java.util.Enumeration; | |
19 | import java.util.List; | |
20 | ||
21 | import net.admin4j.exception.JdkLoggingExceptionHandler; | |
22 | import net.admin4j.ui.servlets.LogLevelServlet; | |
23 | import net.admin4j.util.Admin4jRuntimeException; | |
24 | import net.admin4j.vo.LoggerVO; | |
25 | ||
26 | /** | |
27 | * Jdk Implementation of a Log Manager. | |
28 | * @author M. Lyons | |
29 | * | |
30 | */ | |
31 | 12 | public class JdkLogManager implements LogManager { |
32 | ||
33 | @SuppressWarnings("rawtypes") | |
34 | public List<LoggerVO> findLoggers() { | |
35 | 9 | List<LoggerVO> loggerVOs = new ArrayList<LoggerVO>(); |
36 | ||
37 | 9 | java.util.logging.LogManager lm = java.util.logging.LogManager.getLogManager(); |
38 | 9 | Enumeration loggerNames = lm.getLoggerNames(); |
39 | ||
40 | 45 | while (loggerNames.hasMoreElements()) |
41 | { | |
42 | 36 | String loggerName = (String)loggerNames.nextElement(); |
43 | 36 | java.util.logging.Logger logger = lm.getLogger(loggerName); |
44 | 36 | String loggerLevel = LogLevelServlet.NONE; |
45 | 36 | java.util.logging.Level level = logger.getLevel(); |
46 | 36 | if (level != null) |
47 | { | |
48 | 9 | loggerLevel = level.toString(); |
49 | } | |
50 | 36 | LoggerVO loggerVO = new LoggerVO(loggerName, loggerLevel, this.getLoggerSoftwareProductName()); |
51 | 36 | loggerVOs.add(loggerVO); |
52 | 36 | } |
53 | 9 | Collections.sort(loggerVOs); |
54 | ||
55 | 9 | return loggerVOs; |
56 | } | |
57 | ||
58 | public String performLogLevelChange(String requestedLoggerName, | |
59 | LogIncrementType logIncrementType) { | |
60 | 12 | java.util.logging.LogManager lm = java.util.logging.LogManager.getLogManager(); |
61 | 12 | java.util.logging.Logger l = lm.getLogger(requestedLoggerName); |
62 | 12 | if (l==null) |
63 | { | |
64 | 3 | throw new Admin4jRuntimeException("Logger not found") |
65 | .addContextValue("requestedLoggerName", requestedLoggerName); | |
66 | } | |
67 | ||
68 | 9 | java.util.logging.Level lvl = l.getLevel(); |
69 | 9 | if (LogIncrementType.SHOW_MORE.equals(logIncrementType)) |
70 | { | |
71 | 3 | if (lvl==null) |
72 | { | |
73 | 0 | l.setLevel( java.util.logging.Level.INFO ); |
74 | 0 | return l.getLevel().toString(); |
75 | } | |
76 | else | |
77 | { | |
78 | 3 | l.setLevel( this.showMoreJdk14Logging(l.getLevel()) ); |
79 | 3 | return l.getLevel().toString(); |
80 | } | |
81 | } | |
82 | 6 | else if (LogIncrementType.SHOW_LESS.equals(logIncrementType)) |
83 | { | |
84 | 3 | if (lvl==null) |
85 | { | |
86 | 0 | l.setLevel( java.util.logging.Level.WARNING ); |
87 | 0 | return l.getLevel().toString(); |
88 | } | |
89 | else | |
90 | { | |
91 | 3 | l.setLevel( this.showLessJdk14Logging(l.getLevel()) ); |
92 | 3 | return l.getLevel().toString(); |
93 | } | |
94 | } | |
95 | 3 | else if (LogIncrementType.CLEAR.equals(logIncrementType)) |
96 | { | |
97 | 3 | l.setLevel( null ); |
98 | 3 | return ""; |
99 | } | |
100 | 0 | throw new Admin4jRuntimeException("Unsupported Log Increment Type") |
101 | .addContextValue("logIncrementType", logIncrementType); | |
102 | } | |
103 | ||
104 | /** | |
105 | * The levels in descending order are: | |
106 | * <ul> | |
107 | * <li>OFF | |
108 | * <li>SEVERE (highest value) | |
109 | * <li>WARNING | |
110 | * <li>INFO | |
111 | * <li>CONFIG | |
112 | * <li>FINE | |
113 | * <li>FINER | |
114 | * <li>FINEST (lowest value) | |
115 | * </ul> | |
116 | * @param lvl | |
117 | * @return | |
118 | */ | |
119 | private java.util.logging.Level showLessJdk14Logging(java.util.logging.Level lvl) { | |
120 | 3 | if (java.util.logging.Level.OFF.equals(lvl)) return java.util.logging.Level.OFF; |
121 | 3 | if (java.util.logging.Level.SEVERE.equals(lvl)) return java.util.logging.Level.OFF; |
122 | 3 | if (java.util.logging.Level.WARNING.equals(lvl)) return java.util.logging.Level.SEVERE; |
123 | 3 | if (java.util.logging.Level.INFO.equals(lvl)) return java.util.logging.Level.WARNING; |
124 | 3 | if (java.util.logging.Level.CONFIG.equals(lvl)) return java.util.logging.Level.INFO; |
125 | 0 | if (java.util.logging.Level.FINE.equals(lvl)) return java.util.logging.Level.CONFIG; |
126 | 0 | if (java.util.logging.Level.FINER.equals(lvl)) return java.util.logging.Level.FINE; |
127 | 0 | if (java.util.logging.Level.FINEST.equals(lvl)) return java.util.logging.Level.FINER; |
128 | 0 | return java.util.logging.Level.INFO; |
129 | } | |
130 | /** | |
131 | * The levels in descending order are: | |
132 | * <ul> | |
133 | * <li>OFF | |
134 | * <li>SEVERE (highest value) | |
135 | * <li>WARNING | |
136 | * <li>INFO | |
137 | * <li>CONFIG | |
138 | * <li>FINE | |
139 | * <li>FINER | |
140 | * <li>FINEST (lowest value) | |
141 | * </ul> | |
142 | * @param lvl | |
143 | * @return | |
144 | */ | |
145 | private java.util.logging.Level showMoreJdk14Logging(java.util.logging.Level lvl) { | |
146 | 3 | if (java.util.logging.Level.OFF.equals(lvl)) return java.util.logging.Level.SEVERE; |
147 | 3 | if (java.util.logging.Level.SEVERE.equals(lvl)) return java.util.logging.Level.WARNING; |
148 | 3 | if (java.util.logging.Level.WARNING.equals(lvl)) return java.util.logging.Level.INFO; |
149 | 3 | if (java.util.logging.Level.INFO.equals(lvl)) return java.util.logging.Level.CONFIG; |
150 | 0 | if (java.util.logging.Level.CONFIG.equals(lvl)) return java.util.logging.Level.FINE; |
151 | 0 | if (java.util.logging.Level.FINE.equals(lvl)) return java.util.logging.Level.FINER; |
152 | 0 | if (java.util.logging.Level.FINER.equals(lvl)) return java.util.logging.Level.FINEST; |
153 | 0 | if (java.util.logging.Level.FINEST.equals(lvl)) return java.util.logging.Level.FINEST; |
154 | 0 | return java.util.logging.Level.INFO; |
155 | } | |
156 | ||
157 | public String getLoggerSoftwareProductName() { | |
158 | 57 | return "Jdk"; |
159 | } | |
160 | ||
161 | public void installExceptionTrackingLogAppender() { | |
162 | 9 | java.util.logging.LogManager.getLogManager().getLogger("").addHandler(new JdkLoggingExceptionHandler()); |
163 | 9 | } |
164 | ||
165 | } |