View Javadoc

1   /*
2      Licensed under the Apache License, Version 2.0 (the "License");
3      you may not use this file except in compliance with the License.
4      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 uk.nhs.interoperability.util;
15  
16  import java.text.SimpleDateFormat;
17  import java.util.Calendar;
18  
19  /**
20   * Simple Logging class - created to avoid unnecessary dependencies
21   * on external libraries.
22   *
23   * @author Michael Odling-Smee
24   * @author Nicholas Jones
25   * @since 0.1
26   */
27  public class Logger { 
28  	
29  	/** The Constant DATE_FORMAT. */
30  	private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
31  	
32  	/** The Constant TRACE. */
33  	public static final int TRACE = 0;
34  	
35  	/** The Constant DEBUG. */
36  	public static final int DEBUG = 10;
37  	
38  	/** The Constant INFO. */
39  	public static final int INFO = 20;
40  	
41  	/** The Constant WARN. */
42  	public static final int WARN = 30;
43  	
44  	/** The Constant ERROR. */
45  	public static final int ERROR = 40;
46  	
47  	/** The Constant FATAL. */
48  	public static final int FATAL = 50;
49  	
50  	/** The log level. */
51  	private static int logLevel = INFO;
52  	
53  	static {
54  		logLevel = parseLogLevel(ITKApplicationProperties.getProperty("uk.nhs.interoperability.util.Logger.logLevel"));
55  	}
56  	
57  	
58  	/**
59  	 * Log.
60  	 *
61  	 * @param level the level
62  	 * @param message the message
63  	 * @param t the t
64  	 */
65  	private static final void log(int level, String message, Throwable t) {
66  		if (level >= logLevel) {
67  			Calendar CAL = Calendar.getInstance();
68  			String dateTime = DATE_FORMAT.format(CAL.getTime());
69  			String threadId = Thread.currentThread().getName();
70  			String logMessage = dateTime + " [" + threadId + "] " + " [" + logLevelStr(level) + "] " + getCallerInfo() + message;
71  			if (t == null) {
72  				System.out.println(logMessage);
73  			} else {
74  				System.out.println(logMessage + ", " + t.getLocalizedMessage());
75  				t.printStackTrace(System.out);
76  			}
77  		}
78  	}
79  	
80  	/**
81  	 * Parses the log level.
82  	 *
83  	 * @param logLevelStr the log level str
84  	 * @return the int
85  	 */
86  	private static final int parseLogLevel(String logLevelStr) {
87  		if (logLevelStr != null) {
88  			if (logLevelStr.equalsIgnoreCase("TRACE")) {
89  				return TRACE;
90  			} else if (logLevelStr.equalsIgnoreCase("DEBUG")) {
91  				return DEBUG;
92  			}  else if (logLevelStr.equalsIgnoreCase("INFO")) {
93  				return INFO;
94  			}  else if (logLevelStr.equalsIgnoreCase("WARN")) {
95  				return WARN;
96  			}  else if (logLevelStr.equalsIgnoreCase("ERROR")) {
97  				return ERROR;
98  			}  else if (logLevelStr.equalsIgnoreCase("FATAL")) {
99  				return FATAL;
100 			}
101 		}
102 		//Default to INFO level
103 		return INFO;
104 	}
105 	
106 	/**
107 	 * Log level str.
108 	 *
109 	 * @param level the level
110 	 * @return the string
111 	 */
112 	private static final String logLevelStr(int level) {
113 		switch (level) {
114 			case TRACE:
115 				return "TRACE";
116 			case DEBUG:
117 				return "DEBUG";
118 			case INFO:
119 				return "INFO";
120 			case WARN:
121 				return "WARN";
122 			case ERROR:
123 				return "ERROR";
124 			case FATAL:
125 				return "FATAL";
126 			default:
127 				return null;
128 		}
129 	}
130 	
131 	/**
132 	 * Trace.
133 	 *
134 	 * @param message the message
135 	 */
136 	public static final void trace(String message) {
137 		log(TRACE, message, null);
138 	}
139 	
140 	/**
141 	 * Debug.
142 	 *
143 	 * @param message the message
144 	 */
145 	public static final void debug(String message) {
146 		log(DEBUG, message, null);
147 	}
148 	
149 	/**
150 	 * Info.
151 	 *
152 	 * @param message the message
153 	 */
154 	public static final void info(String message) {
155 		log(INFO, message, null);
156 	}
157 	
158 	/**
159 	 * Warn.
160 	 *
161 	 * @param message the message
162 	 */
163 	public static final void warn(String message) {
164 		log(WARN, message, null);
165 	}
166 	
167 	/**
168 	 * Warn.
169 	 *
170 	 * @param message the message
171 	 * @param t the t
172 	 */
173 	public static final void warn(String message, Throwable t) {
174 		log(WARN, message, t);
175 	}
176 	
177 	/**
178 	 * Error.
179 	 *
180 	 * @param message the message
181 	 * @param t the t
182 	 */
183 	public static final void error(String message, Throwable t) {
184 		log(ERROR, message, t);
185 	}
186 	
187 	/**
188 	 * Fatal.
189 	 *
190 	 * @param message the message
191 	 * @param t the t
192 	 */
193 	public static final void fatal(String message, Throwable t) {
194 		log(FATAL, message, t);
195 	}
196 	
197 	/**
198 	 * Gets the caller info.
199 	 *
200 	 * @return the caller info
201 	 */
202 	private static String getCallerInfo() {
203 		return getClassAndMethod(Thread.currentThread().getStackTrace()[4]);
204 	}
205 	
206 	/**
207 	 * Gets the class and method.
208 	 *
209 	 * @param ste the ste
210 	 * @return the class and method
211 	 */
212 	private static String getClassAndMethod(StackTraceElement ste) {
213 		String className = ste.getClassName().substring(ste.getClassName().lastIndexOf(".") + 1);
214 		return className + "." + ste.getMethodName() + "(l:" + ste.getLineNumber() + ") ";
215 	}
216 
217 }