1 package org.opensync.engine.server;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.io.StringWriter;
6 import java.io.Writer;
7 import java.text.SimpleDateFormat;
8 import java.util.Date;
9
10 import org.apache.log4j.BasicConfigurator;
11 import org.apache.log4j.Category;
12 import org.apache.log4j.DailyRollingFileAppender;
13 import org.apache.log4j.PatternLayout;
14 import org.apache.log4j.WriterAppender;
15 import org.opensync.engine.util.I18n;
16 import org.opensync.tools.Utils;
17
18 /***
19 * This class represents the log of the OpenSync application
20 */
21
22 public class Log implements TaskListener {
23 /****/
24 final static protected int LEVEL_ERROR = 0;
25 /****/
26 final static protected int LEVEL_INFO = 1;
27 /****/
28 final static protected int LEVEL_DETAILS = 2;
29 /****/
30 final static public Category ROOT = Category.getInstance("OPENSYNC");
31 /****/
32 final static public Category SOURCE = Category.getInstance("OPENSYNC.SOURCE");
33 /****/
34 final static public Category CONNECTOR = Category.getInstance("OPENSYNC.CONNECTOR");
35 /****/
36 final static public Category SYNCHRONIZER = Category.getInstance("OPENSYNC.SYNCHRONIZER");
37 /****/
38 final static public Category ERROR = Category.getInstance("OPENSYNC.ERROR");
39 /****/
40 final static public Category VIEW = Category.getInstance("OPENSYNC.VIEW");
41 /****/
42 final static public Category CONFIG = Category.getInstance("OPENSYNC.CONFIG");
43 /****/
44 final static public Category STATISTIC = Category.getInstance("OPENSYNC.STATISTIC");
45
46 /****/
47 static protected SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
48 private int level;
49
50 /***
51 * Construct a log object
52 *
53 * @param directoryPath the directory where put log files
54 */
55 public Log(String directoryPath) {
56 try {
57 BasicConfigurator.configure();
58 ROOT.addAppender(
59 new DailyRollingFileAppender(new PatternLayout("%p - %c - %m%n"),
60 directoryPath + "/sophsync.log","'.'yyyy-MM-dd"
61 )
62 );
63 STATISTIC.addAppender(
64 new DailyRollingFileAppender(new PatternLayout("%m%n"),
65 directoryPath + "/sophsync_sumary.log",
66 "'.'yyyy-MM-dd"
67 )
68 );
69
70 }
71 catch (IOException ex) {
72 throw new RuntimeException(ex.getMessage());
73 }
74 }
75 /***
76 * @param writer
77 * @param category
78 */
79 public void addWriter(Writer writer,Category category){
80 category.addAppender(new WriterAppender(new PatternLayout("%p - %c - %m%n"),writer));
81 }
82 /***
83 * @param writer
84 * @param category
85 * @param pattern
86 */
87 public void addWriter(Writer writer,Category category,String pattern){
88 category.addAppender(new WriterAppender(new PatternLayout(pattern),writer));
89 }
90 /***
91 * @param category
92 * @param throwable
93 */
94 public void fatal(Category category,Throwable throwable){
95 category.fatal(formatStackTrace(throwable));
96 }
97 /***
98 * @param category
99 * @param msg
100 */
101 public void fatal(Category category,String msg){
102 category.fatal(msg);
103 }
104 /***
105 * @param category
106 * @param msg
107 */
108 public void error(Category category,String msg){
109 category.error(msg);
110 }
111 /***
112 * @param category
113 * @param msg
114 */
115 public void debug(Category category,String msg){
116 if(level >= LEVEL_DETAILS){
117 category.debug(msg);
118 }
119 }
120 /***
121 * @param category
122 * @param msg
123 */
124 public void info(Category category,String msg){
125 if(level >= LEVEL_INFO){
126 category.info(msg);
127 }
128 }
129 /***
130 * @param category
131 * @param msg
132 */
133 public void warn(Category category,String msg){
134 if(level >= LEVEL_INFO){
135 category.warn(msg);
136 }
137 }
138
139 /***
140 * @param category
141 * @param msgKey
142 * @param params
143 */
144 public void fatal(Category category,String msgKey,Object[]params){
145 category.fatal(formatMsgKey(msgKey,params));
146 }
147 /***
148 * @param category
149 * @param msgKey
150 * @param params
151 */
152 public void error(Category category,String msgKey,Object[]params){
153 category.error(formatMsgKey(msgKey,params));
154 }
155 /***
156 * @param category
157 * @param msgKey
158 * @param params
159 */
160 public void debug(Category category,String msgKey,Object[]params){
161 if(level >= LEVEL_DETAILS){
162 category.debug(formatMsgKey(msgKey,params));
163 }
164 }
165 /***
166 * @param category
167 * @param msgKey
168 * @param params
169 */
170 public void info(Category category,String msgKey,Object[]params){
171 if(level >= LEVEL_INFO){
172 category.info(formatMsgKey(msgKey,params));
173 }
174 }
175 /***
176 * @param category
177 * @param msgKey
178 * @param params
179 */
180 public void warn(Category category,String msgKey,Object[]params){
181 if(level >= LEVEL_INFO){
182 category.warn(formatMsgKey(msgKey,params));
183 }
184 }
185 /***
186 * @param msgKey
187 * @param params
188 */
189 public String formatMsgKey(String msgKey,Object[]params){
190 if(params == null){
191 return I18n.getInstance().get(msgKey);
192 }
193 else{
194 return I18n.getInstance().format(msgKey,params);
195 }
196 }
197 /***
198 * @param t
199 */
200 public static String formatStackTrace(Throwable t){
201 StringWriter sw = null;
202 PrintWriter pw = null;
203 try {
204 sw = new StringWriter();
205 pw = new PrintWriter(sw, true);
206 if (Utils.debug) t.printStackTrace(pw); else pw.print(": "+t.getLocalizedMessage());
207 return sw.toString();
208 }
209 finally {
210 if(pw != null){
211 pw.close();
212 }
213 if(sw != null){
214 try {
215 sw.close();
216 }
217 catch (IOException ex) {
218 throw new RuntimeException(ex.getMessage());
219 }
220 }
221 }
222 }
223 /***
224 * @param event
225 */
226 public void taskStart(TaskEvent event) {
227 Task task = event.getTask();
228 info(Log.SYNCHRONIZER,
229 I18n.getInstance().format(
230 "info.task.start",new Object[]{
231 "'"+task.getName()+"'",dateFormat.format(event.getStart())
232 }
233 )
234 );
235 }
236 /***
237 * @param event
238 */
239 public void taskStop(TaskEvent event) {
240 Task task = event.getTask();
241 info(Log.SYNCHRONIZER,
242 I18n.getInstance().format(
243 "info.task.stop",new Object[]{
244 "'"+task.getName()+"'",dateFormat.format(event.getStop()),new Long(event.getDelay())
245 }
246 )
247 );
248 }
249 /***
250 * @param event
251 */
252 public void taskFail(TaskEvent event) {
253 Task task = event.getTask();
254 Exception e = event.getException();
255 if(e instanceof OpenSyncException){
256 error(Log.SYNCHRONIZER,
257 I18n.getInstance().format(
258 "info.task.fail",new Object[]{
259 "'"+task.getName()+"'",dateFormat.format(event.getStop()),e.getMessage()
260 }
261 )
262 );
263 }
264 else{
265 fatal(Log.SYNCHRONIZER,
266 I18n.getInstance().format(
267 "info.task.fail",new Object[]{
268 "'"+task.getName()+"'",dateFormat.format(event.getStop()),formatStackTrace(e)
269 }
270 )
271 );
272 }
273 }
274 /***
275 * @param date
276 */
277 public static String formatDate(Date date){
278 return date == null ? "" : dateFormat.format(date);
279 }
280 /***
281 * @param level
282 */
283 public void setLevel(String level) {
284 if(level.equals("ERROR")){
285 this.level = LEVEL_ERROR;
286 }
287 if(level.equals("INFO")){
288 this.level = LEVEL_INFO;
289 }
290 if(level.equals("DETAILS")){
291 this.level = LEVEL_DETAILS;
292 }
293 }
294 /****/
295 public int getLevel() {
296 return level;
297 }
298 }