-
Notifications
You must be signed in to change notification settings - Fork 213
/
LogFilter.java
173 lines (149 loc) · 5.74 KB
/
LogFilter.java
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
/*
Copyright 2009-2016 Igor Polevoy
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 org.javalite.activejdbc.logging;
import org.javalite.activejdbc.Configuration;
import org.javalite.activejdbc.Registry;
import org.javalite.activejdbc.statistics.QueryExecutionEvent;
import org.slf4j.Logger;
import java.util.regex.Pattern;
/**
* @author Igor Polevoy
*/
public class LogFilter {
private static Pattern pattern;
static{
String logFlag = System.getProperty("activejdbc.log");
if (logFlag != null && logFlag.equals("")) {
//match anything
setLogExpression(".*");
} else if (logFlag != null ) {//match by provided value
setLogExpression(logFlag);
} else {//match nothing
setLogExpression("a{10000000}");
}
}
private LogFilter() {
}
public static void setLogExpression(String regexp){
pattern = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE);
}
public static void logQuery(Logger logger, String query, Object[] params, long queryStartTime){
long time = System.currentTimeMillis() - queryStartTime;
if (Registry.instance().getConfiguration().collectStatistics()) {
Registry.instance().getStatisticsQueue().enqueue(new QueryExecutionEvent(query, time));
}
log(logger, LogLevel.INFO, getJson(query, params, time));
}
private static String getJson(String query, Object[] params, long time) {
StringBuilder paramsSB = new StringBuilder("");
if (params != null) {
for (int i = 0; i < params.length; i++) {
if(params[i] instanceof Number){
paramsSB.append(params[i]);
}else if(params[i] instanceof byte[]){
paramsSB.append("\"bytes[...]\"");
}else {
paramsSB.append("\"").append(params[i]).append("\"");
}
if(i != (params.length - 1)){
paramsSB.append(",");
}
}
}
return "{\"sql\":\"" + query.replace("\"", "'") + "\",\"params\":[" + paramsSB.toString() + "],\"duration_millis\":" + time + "}";
}
public static void log(Logger logger, LogLevel logLevel, String log){
if(Configuration.hasActiveLogger()){
Configuration.getActiveLogger().log(logger, logLevel, log);
}else {
if (pattern.matcher(log).matches()) {
switch (logLevel){
case DEBUG:
logger.debug(log);
break;
case INFO:
logger.info(log);
break;
case WARNING:
logger.warn(log);
break;
case ERROR:
logger.error(log);
break;
default:
}
}
}
}
public static void log(Logger logger, LogLevel logLevel, String log, Object param) {
if(Configuration.hasActiveLogger()) {
Configuration.getActiveLogger().log(logger, logLevel, log, param);
}else {
if (pattern.matcher(log).matches()) {
switch (logLevel){
case DEBUG:
logger.debug(log, param);
break;
case INFO:
logger.info(log, param);
break;
case WARNING:
logger.warn(log, param);
break;
case ERROR:
logger.error(log, param);
break;
default:
}
}
}
}
public static void log(Logger logger, LogLevel logLevel, String log, Object param1, Object param2) {
if(Configuration.hasActiveLogger()) {
Configuration.getActiveLogger().log(logger, logLevel, log, param1, param2);
}else {
if (pattern.matcher(log).matches()) {
switch (logLevel){
case DEBUG:
logger.debug(log, param1, param2);
case INFO:
logger.info(log, param1, param2);
case WARNING:
logger.warn(log, param1, param2);
case ERROR:
logger.error(log, param1, param2);
default:
}
}
}
}
public static void log(Logger logger, LogLevel logLevel, String log, Object... params) {
if(Configuration.hasActiveLogger()) {
Configuration.getActiveLogger().log(logger, logLevel, log, params);
}else {
if (pattern.matcher(log).matches()) {
switch (logLevel){
case DEBUG:
logger.debug(log, params);
case INFO:
logger.info(log, params);
case WARNING:
logger.warn(log, params);
case ERROR:
logger.error(log, params);
default:
}
}
}
}
}