forked from scouter-project/scouter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.java
More file actions
254 lines (220 loc) · 6.5 KB
/
Logger.java
File metadata and controls
254 lines (220 loc) · 6.5 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* Copyright 2015 the original author or authors.
* @https://github.com/scouter-project/scouter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package scouter.agent;
import scouter.util.*;
import java.io.*;
public class Logger {
private static StringLongLinkedMap lastLog = new StringLongLinkedMap().setMax(1000);
static Configure conf = Configure.getInstance();
public static void println(Object message) {
println(build("SCOUTER", toString(message)), true);
}
public static void println(String id, Object message) {
if (checkOk(id, 0) == false) {
return;
}
println(build(id, toString(message)), true);
}
public static void println(String id, String message, Throwable t) {
if (checkOk(id, 10) == false) {
return;
}
println(build(id, message), true);
println(ThreadUtil.getStackTrace(t), true);
}
public static void trace(Object message) {
if(conf._trace) {
if(conf._trace_use_logger) {
println(build("SCOUTER-TRC", toString(message)), true);
} else {
System.out.println(build("SCOUTER-TRC", toString(message)));
}
}
}
private static String toString(Object message) {
return message == null ? "null" : message.toString();
}
private static String build(String id, String message) {
return new StringBuffer(20 + id.length() + message.length())
.append(DateUtil.datetime(System.currentTimeMillis())).append(" [").append(id).append("] ")
.append(message).toString();
}
public static String getCallStack(Throwable t) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
try {
t.printStackTrace(pw);
return sw.toString();
} finally {
pw.close();
}
}
private static boolean checkOk(String id, int sec) {
if (Configure.getInstance().isIgnoreLog(id))
return false;
if (sec > 0) {
long last = lastLog.get(id);
long now = System.currentTimeMillis();
if (now < last + sec * 1000)
return false;
lastLog.put(id, now);
}
return true;
}
static PrintWriter pw = null;
static File logfile = null;
private static void println(String msg, boolean sysout) {
try {
if (pw != null) {
pw.println(msg);
pw.flush();
return;
} else {
if (sysout) {
System.out.println(msg);
}
}
} catch (Throwable e) {
pw = (PrintWriter) FileUtil.close(pw);
if (sysout) {
System.out.println(msg);
}
}
}
private static synchronized void openFile(String prefix) throws IOException {
if (pw == null && StringUtil.isEmpty(conf.log_dir) == false) {
File root = new File(conf.log_dir);
if (root.canWrite() == false) {
root.mkdirs();
}
if (root.canWrite() == false) {
return;
}
if (conf.log_rotation_enabled) {
File file = new File(conf.log_dir, "scouter-" + prefix + "-" + DateUtil.yyyymmdd() + ".log");
FileWriter fw = new FileWriter(file, true);
pw = new PrintWriter(fw);
logfile = file;
} else {
File file = new File(conf.log_dir, "scouter-" + prefix + ".log");
pw = new PrintWriter(new FileWriter(file, true));
logfile = file;
}
}
}
static Runnable initializer = new Runnable() {
long last = System.currentTimeMillis();
long lastDataUnit = DateUtil.getDateUnit();
String lastDir = conf.log_dir;
boolean lastFileRotation = conf.log_rotation_enabled;
String scouter_name = "boot";
public void run() {
try {
process();
} catch (Throwable t) {
}
}
private synchronized void process() {
long now = System.currentTimeMillis();
if (now > last + DateUtil.MILLIS_PER_HOUR) {
last = now;
clearOldLog();
}
if (CompareUtil.equals(lastDir, conf.log_dir) == false //
|| lastFileRotation != conf.log_rotation_enabled //
|| lastDataUnit != DateUtil.getDateUnit() //
|| scouter_name.equals(conf.obj_name) == false//
|| (logfile != null && logfile.exists() == false)) {
pw = (PrintWriter) FileUtil.close(pw);
logfile = null;
lastDir = conf.log_dir;
lastFileRotation = conf.log_rotation_enabled;
lastDataUnit = DateUtil.getDateUnit();
scouter_name = conf.obj_name;
}
try {
openFile(scouter_name);
} catch (Throwable t) {
sysout(t.getMessage());
}
}
};
protected static void clearOldLog() {
if (conf.log_rotation_enabled == false)
return;
if (conf.log_keep_days <= 0)
return;
String scouter_prefix = "scouter-" + conf.obj_name;
long nowUnit = DateUtil.getDateUnit();
File dir = new File(conf.log_dir);
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory())
continue;
String name = files[i].getName();
if (name.startsWith(scouter_prefix + "-") == false)
continue;
int x = name.lastIndexOf('.');
if (x < 0)
continue;
String date = name.substring(scouter_prefix.length() + 1, x);
if (date.length() != 8)
continue;
try {
long d = DateUtil.yyyymmdd(date);
long fileUnit = DateUtil.getDateUnit(d);
if (nowUnit - fileUnit > DateUtil.MILLIS_PER_DAY * conf.log_keep_days) {
files[i].delete();
}
} catch (Exception e) {
}
}
}
public static void main(String[] args) {
String name = "scouter-19701123.log";
int x = name.lastIndexOf('.');
String date = name.substring("scouter-".length(), x);
System.out.println(date);
}
public static void info(String message) {
message = build("SCOUTER", message);
sysout(message);
println(message, false);
}
private static void sysout(String message) {
System.out.println(message);
}
public static class FileLog implements IClose {
private PrintWriter out;
public FileLog(String filename) {
try {
this.out = new PrintWriter(new FileWriter(filename));
} catch (Exception e) {
e.printStackTrace();
}
}
public void println(String message) {
if (this.out == null)
return;
this.out.println(DateUtil.datetime(System.currentTimeMillis()) + " " + message);
this.out.flush();
}
public void close() {
FileUtil.close(this.out);
}
}
}