-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.java
More file actions
192 lines (160 loc) · 6.23 KB
/
Logger.java
File metadata and controls
192 lines (160 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package javaxt.express;
import java.util.List;
import java.util.LinkedList;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import javaxt.http.servlet.HttpServletRequest;
//******************************************************************************
//** Logger Class
//******************************************************************************
/**
* Used to log server activity to a text file. A new text file is created
* for each day.
*
******************************************************************************/
public class Logger implements Runnable {
private List pool;
private File logDir;
private FileChannel outChannel;
private FileOutputStream outputFile;
private int date;
private java.util.TimeZone tz;
private Long maxFileSize;
//**************************************************************************
//** Constructor
//**************************************************************************
public Logger(File logDir) {
this(logDir, null, "UTC");
}
//**************************************************************************
//** Constructor
//**************************************************************************
public Logger(File logDir, Long maxFileSize, String timezone) {
if (!logDir.exists()) logDir.mkdirs();
this.logDir = logDir;
this.date = -1;
this.maxFileSize = maxFileSize;
this.tz = javaxt.utils.Date.getTimeZone(timezone);
this.pool = new LinkedList();
}
//**************************************************************************
//** log
//**************************************************************************
/** Used to log web requests
*/
public void log(HttpServletRequest request) {
String clientIP = request.getRemoteAddr();
if (clientIP.startsWith("/") && clientIP.length()>1) clientIP = clientIP.substring(1);
StringBuilder str = new StringBuilder();
str.append("New Request From: " + clientIP + "\r\n");
str.append(request.getMethod() + ": " + request.getURL() + "\r\n");
str.append("TimeStamp: " + getDate().toString("yyyy-MM-dd HH:mm a") + "\r\n");
str.append("\r\n");
str.append(request.toString());
log(str.toString());
}
// //**************************************************************************
// //** log
// //**************************************************************************
// /** Used to log exceptions
// */
// public void log(Exception e){
// String s = e.getClass().getName();
// s = s.substring(s.lastIndexOf(".")+1);
// String message = e.getLocalizedMessage();
// String error = (message != null) ? (s + ": " + message) : s;
// log(error);
// }
//**************************************************************************
//** log
//**************************************************************************
/** Used to add a string to the log file. The string will be terminated with
* a line break.
*/
public void log(String str){
if (str!=null){
if (!str.endsWith("\r\n")) str+= "\r\n";
synchronized (pool) {
pool.add(pool.size(), str);
pool.notifyAll();
}
}
}
//**************************************************************************
//** run
//**************************************************************************
/** Used to remove an entry from the queue and write it to a file.
*/
public void run() {
while (true) {
String request;
synchronized (pool) {
while (pool.isEmpty()) {
try {
pool.wait();
}
catch (InterruptedException e) {
break;
}
}
request = (String) pool.remove(0);
}
try{
byte[] b = request.getBytes();
if (b.length>8*1024){
if (request.length()>250) request = request.substring(0, 250);
request += "...\r\n";
b = request.getBytes();
}
ByteBuffer output = ByteBuffer.allocateDirect(b.length);
output.put(b);
output.flip();
getFileChannel().write(output);
}
catch(Exception e){
//e.printStackTrace();
}
}
}
//**************************************************************************
//** getFileChannel
//**************************************************************************
private FileChannel getFileChannel() throws Exception {
javaxt.utils.Date d = getDate();
int date = Integer.parseInt(d.toString("yyyyMMdd"));
int year = d.getYear();
File dir = new File(logDir, year+"");
if (!dir.exists()) dir.mkdirs();
if (date>this.date){
this.date = date;
if (outputFile!=null) outputFile.close();
if (outChannel!=null) outChannel.close();
File file = new File(dir, this.date + ".log");
outputFile = new FileOutputStream(file, true);
outChannel = outputFile.getChannel();
}
else{
File file = new File(dir, this.date + ".log");
if (maxFileSize!=null){
if (file.length()>maxFileSize){
if (outputFile!=null) outputFile.close();
if (outChannel!=null) outChannel.close();
throw new Exception("Log file too big: " + file.length() + " vs " + maxFileSize);
}
}
}
return outChannel;
}
//**************************************************************************
//** getDate
//**************************************************************************
/** Returns a new Date object with the current time. The date will be set
* to whatever time zone was used to instantiate this class (default is
* UTC).
*/
public javaxt.utils.Date getDate(){
return new javaxt.utils.Date().setTimeZone(tz);
}
}