1 package org.opensync.engine.server.log;
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
9 import org.apache.log4j.BasicConfigurator;
10 import org.apache.log4j.Category;
11 import org.apache.log4j.DailyRollingFileAppender;
12 import org.apache.log4j.PatternLayout;
13 import org.apache.log4j.WriterAppender;
14 import org.opensync.engine.server.Task;
15 import org.opensync.engine.server.TaskEvent;
16 import org.opensync.engine.server.TaskListener;
17 import org.opensync.engine.util.I18n;
18 import org.opensync.tools.Utils;
19
20
21 public class Log implements TaskListener {
22 /****/
23 final static public Category ROOT = Category.getInstance("OPENSYNC");
24 /****/
25 final static public Category SOURCE = Category.getInstance("OPENSYNC.SOURCE");
26 /****/
27 final static public Category CONNECTOR = Category.getInstance("OPENSYNC.CONNECTOR");
28 /****/
29 final static public Category SYNCHRONIZER = Category.getInstance("OPENSYNC.SYNCHRONIZER");
30 /****/
31 final static public Category ERROR = Category.getInstance("OPENSYNC.ERROR");
32 /****/
33 final static public Category VIEW = Category.getInstance("OPENSYNC.VIEW");
34 /****/
35 final static public Category CONFIG = Category.getInstance("OPENSYNC.CONFIG");
36
37 protected SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
38
39 /****/
40 public Log(String directoryPath) {
41 try {
42 BasicConfigurator.configure();
43 ROOT.addAppender(
44 new DailyRollingFileAppender(new PatternLayout("%p - %c - %m%n"),
45 directoryPath + "/sophsync.log","'.'yyyy-MM-dd"
46 )
47 );
48 }
49 catch (IOException ex) {
50 throw new RuntimeException(ex.getMessage());
51 }
52 }
53 /***
54 * @param writer
55 */
56 public void addWriter(Writer writer){
57 ROOT.addAppender(new WriterAppender(new PatternLayout("%p - %c - %m%n"),writer));
58 }
59 /***
60 * @param category
61 * @param throwable
62 */
63 public void fatal(Category category,Throwable throwable){
64 category.fatal(formatStackTrace(throwable));
65 }
66 /***
67 * @param category
68 * @param msg
69 */
70 public void fatal(Category category,String msg){
71 category.fatal(msg);
72 }
73 /***
74 * @param category
75 * @param msg
76 */
77 public void error(Category category,String msg){
78 category.error(msg);
79 }
80 /***
81 * @param category
82 * @param msg
83 */
84 public void debug(Category category,String msg){
85 category.debug(msg);
86 }
87 /***
88 * @param category
89 * @param msg
90 */
91 public void info(Category category,String msg){
92 category.info(msg);
93 }
94 /***
95 * @param category
96 * @param msg
97 */
98 public void warn(Category category,String msg){
99 category.warn(msg);
100 }
101
102 public void fatal(Category category,String msgKey,Object[]params){
103 category.fatal(formatMsgKey(msgKey,params));
104 }
105 public void error(Category category,String msgKey,Object[]params){
106 category.error(formatMsgKey(msgKey,params));
107 }
108 public void debug(Category category,String msgKey,Object[]params){
109 category.debug(formatMsgKey(msgKey,params));
110 }
111 public void info(Category category,String msgKey,Object[]params){
112 category.info(formatMsgKey(msgKey,params));
113 }
114 public void warn(Category category,String msgKey,Object[]params){
115 category.warn(formatMsgKey(msgKey,params));
116 }
117 public String formatMsgKey(String msgKey,Object[]params){
118 if(params == null){
119 return I18n.getInstance().get(msgKey);
120 }
121 else{
122 return I18n.getInstance().format(msgKey,params);
123 }
124 }
125 /***
126 * @param t
127 */
128 public static String formatStackTrace(Throwable t){
129 StringWriter sw = null;
130 PrintWriter pw = null;
131 try {
132 sw = new StringWriter();
133 pw = new PrintWriter(sw, true);
134 if (Utils.debug) t.printStackTrace(pw); else pw.print(t.getLocalizedMessage());
135 return sw.toString();
136 }
137 finally {
138 if(pw != null){
139 pw.close();
140 }
141 if(sw != null){
142 try {
143 sw.close();
144 }
145 catch (IOException ex) {
146 throw new RuntimeException(ex.getMessage());
147 }
148 }
149 }
150 }
151 public void taskStart(TaskEvent event) {
152 Task task = event.getTask();
153 info(Log.SYNCHRONIZER,
154 I18n.getInstance().format(
155 "info.task.start",new Object[]{
156 task.getName(),dateFormat.format(event.getStart())
157 }
158 )
159 );
160 }
161 public void taskStop(TaskEvent event) {
162 Task task = event.getTask();
163 info(Log.SYNCHRONIZER,
164 I18n.getInstance().format(
165 "info.task.stop",new Object[]{
166 task.getName(),dateFormat.format(event.getStop()),new Long(event.getDelay())
167 }
168 )
169 );
170 }
171 public void taskFail(TaskEvent event) {
172 Task task = event.getTask();
173 error(Log.SYNCHRONIZER,
174 I18n.getInstance().format(
175 "info.task.fail",new Object[]{
176 task.getName(),dateFormat.format(event.getStop()), formatStackTrace(event.getException())
177 }
178 )
179 );
180 }
181 }