Skip to content

Commit

Permalink
fixes issue #11
Browse files Browse the repository at this point in the history
also extracted util classes
  • Loading branch information
[email protected] committed Oct 8, 2009
1 parent c7967e7 commit 7743ffc
Show file tree
Hide file tree
Showing 22 changed files with 1,680 additions and 278 deletions.
4 changes: 2 additions & 2 deletions at.fpmedv.jdbc.logger/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/log4j-1.2.15.jar"/>
<classpathentry combineaccessrules="false" kind="src" path="/at.fpmedv.jdbc.decorator"/>
<classpathentry combineaccessrules="false" kind="src" path="/at.fpmedv.util"/>
<classpathentry kind="lib" path="lib/fpmedv-util.jar"/>
<classpathentry kind="lib" path="lib/jdbc-decorator.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
28 changes: 1 addition & 27 deletions at.fpmedv.jdbc.logger/jdbclogger.properties
Original file line number Diff line number Diff line change
@@ -1,32 +1,6 @@
#jdbcdecorator.drivers=
jdbcdecorator.preloadKnownDrivers=true

jdbclogger.maxStatementStatistics = 400
jdbclogger.loggingEnabled = true

jdbclogger.displayFullStackTrace = false

# normalize numbers
jdbclogger.normalize.0.pattern =([=,\\s(]\\s*)\\d+(,|\\s|\\)|$)
jdbclogger.normalize.0.replacement =$1X$2

# normalize strings
jdbclogger.normalize.1.pattern =([=,\\s(]\\s*)'(('')|[^'])+'(,|\\s|\\)|$)
jdbclogger.normalize.1.replacement =$1'X'$4

# remove timestamps
jdbclogger.normalize.2.pattern =ts\\s*'[^']+'
jdbclogger.normalize.2.replacement =ts X
#jdbclogger.normalize.2.mayBeDisable =true

# remove , 'X' or , X
jdbclogger.normalize.3.pattern =,\\s*'?X'?
jdbclogger.normalize.3.replacement =

# remove , null or , NULL
jdbclogger.normalize.4.pattern =,\\s*(null)|(NULL)
jdbclogger.normalize.4.replacement =
jdbclogger.normalize.4.mayBeDisable =true
#jdbclogger.displayFullStackTrace = false

# log4j config
# TRACE, DEBUG, INFO, WARN, ERROR, FATAL
Expand Down
Binary file added at.fpmedv.jdbc.logger/lib/fpmedv-util.jar
Binary file not shown.
225 changes: 1 addition & 224 deletions at.fpmedv.jdbc.logger/src/at/fpmedv/jdbc/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.regex.PatternSyntaxException;

import at.fpmedv.jdbc.logging.LoggerDelegator;
import at.fpmedv.jdbc.logging.LoggerFactory;
import at.fpmedv.util.Stopwatch;
import at.fpmedv.util.regexp.RegExpReplacementContainer;

/**
Expand Down Expand Up @@ -68,51 +65,16 @@ public class Logger {
*/
private boolean loggingEnabled = true;

/**
* how many sql statements to store internaly
*/
private int maxStatements = 400;

/**
* the filepath to the Statistic properties file, read from a java system property
*/
public static final String CONFIGURATION_FILE_NAME = System.getProperty(Logger.JDBC_LOGGER_PREFIX + "configuration");

/**
* the properties of the Statistic object
* the properties of the Logging object
*/
private Properties properties = new Properties();

/**
* counter for update, select, insert statements
*/
private volatile long[] count = {0L,0L,0L,0L,0L};

/**
* counter for execution time for update, select, insert statements
*/
private volatile long[] millis = {0L,0L,0L,0L,0L};

/**
* counter for errors for update, select, insert statements
*/
private volatile long errorCount = 0L;

/**
* if the full stacktrace should be logged or not
*/
private boolean displayFullStackTrace = false;

/**
* contains StatementStatistics objects for each statement passing through
*/
private volatile HashMap<String, StatementCharacteristics> statementStatistics = new HashMap<String, StatementCharacteristics>();

/**
* represents the number of Statements that are not stored in {@link #statementStatistics}
*/
private volatile long statementStatisticsOverflow = 0L;

/**
* logger for all statement categories
*/
Expand Down Expand Up @@ -184,10 +146,6 @@ public class Logger {
*/
private ArrayList<RegExpReplacementContainer> replacePatterns = new ArrayList<RegExpReplacementContainer>();

private Date dateLastRendered;

private long dateLastRenderedTime = 0L;

/**
* opens properties file and reads configuration values
*/
Expand All @@ -211,19 +169,6 @@ public static Logger getInstance() {
return uniqueInstance;
}

/**
* wrapper for recordStatistics and log
*
* @param sql the sql that was executed
* @param millis execution time
* @param exception an exception object if something went wrong
*/
public void recordStatisticsAndLog(String sql, long millis, Throwable exception) {
int type = getType(sql);
recordStatistics(sql, millis, type);
log(sql, millis, exception, type);
}

/**
* try to find out if UPDATE, INSERT or SELECT statement and returns the equvivalent
* internal type representation
Expand Down Expand Up @@ -273,82 +218,6 @@ private void log(String sql, long millis, Throwable exception, int type) {
logger[type].info(millis + " - " + sql, exception);
}

/**
* wrapper for private recordStatistics so nothing can go wrong with the indexing
* of the internal arrays
*
* @param sql the sql that was executed
* @param millis execution time
*/
public void recordStatistics(String sql, long millis) {
int type = getType(sql);

recordStatistics(sql, millis, type);
}

/**
* stores the given arguments, applies the {@link #replacePatterns} to the sql statement
*
* @param sql the sql that was executed
* @param millis execution time
* @param type internal type representation
*/
private void recordStatistics(String sql, long millis, int type) {
Stopwatch sw = new Stopwatch();
sw.start();
this.millis[type] += millis;
this.count[type]++;
String normalizedSql = new String(sql);
Iterator<RegExpReplacementContainer> replacePatternsIterator = replacePatterns.iterator();
Throwable errorThrown = null;
while (replacePatternsIterator.hasNext()) {
RegExpReplacementContainer patternReplacement = replacePatternsIterator.next();
if (patternReplacement.isEnabled()) {
try {
normalizedSql = normalizedSql.replaceAll(patternReplacement.getRegexp(), patternReplacement.getReplacement());
} catch (Throwable t) {
// something went terrible wrong
// log message
String errorMessage = "Error applying pattern: " + patternReplacement.getRegexp() + " on " + normalizedSql;
if (displayFullStackTrace) {
logger[DEFAULT].error(errorMessage, t);
} else {
logger[DEFAULT].error("(" + t + ") " + errorMessage);
}
// disable the pattern so it is not applied
patternReplacement.disable();
errorThrown = t;
increaseErrorCount();
}
}
}
sw.stop();
this.logger[DEFAULT].trace("Normalized SQL(" + sw.getElapsedTimeMillis() + "): " + normalizedSql);
// only add statement statistics if no error occurred
if (errorThrown == null) {
if (statementStatistics.containsKey(normalizedSql)) {
StatementCharacteristics currentStatementStatistics = (StatementCharacteristics) statementStatistics.get(normalizedSql);
currentStatementStatistics.increaseCount();
currentStatementStatistics.increaseMillis(millis);
currentStatementStatistics.setLastCommit(getDate());
} else {
if (statementStatistics.size() < maxStatements){
StatementCharacteristics newStatementStatistics = new StatementCharacteristics(millis);
statementStatistics.put(normalizedSql, newStatementStatistics);
} else
statementStatisticsOverflow++;
}
}
}

private Date getDate() {
if ((System.currentTimeMillis() - 1000) > dateLastRenderedTime) {
dateLastRendered = new Date();
dateLastRenderedTime = dateLastRendered.getTime();
}
return dateLastRendered;
}

/**
* just a getter
*
Expand All @@ -367,82 +236,6 @@ public void setLoggingEnabled(boolean loggingEnabled) {
this.loggingEnabled = loggingEnabled;
}

/**
* checks if type is valid gets it
*
* @param type one of {@link #SELECT}, {@link #UPDATE}, {@link #INSERT} or {@link #DELETE}
* @return the millis of the given type
*/
public long getMillis(int type) {
if (type >= millis.length || type < 0)
return 0;
return millis[type];
}

/**
* checks if type is valid and returns global count of type
*
* @param type one of {@link #SELECT}, {@link #UPDATE}, {@link #INSERT} or {@link #DELETE}
* @return the count of the given type
*/
public long getCount(int type) {
if (type >= count.length || type < 0)
return 0;
return count[type];
}

/**
* just a getter
*
* @return the normalized HashMap of the Statements
*/
public HashMap<String, StatementCharacteristics> getNormalizedStatements() {
return statementStatistics;
}

/**
* just a getter
*
* @return number of errors occurred when applying {@link #replacePatterns}
*/
public long getErrorCount() {
return errorCount;
}

/**
* just a setter
*
* @param errorCount the new {@link #errorCount}
*/
public void setErrorCount(long errorCount) {
this.errorCount = errorCount;
}

/**
* just a setter. increases {@link #errorCount} by 1
*/
public void increaseErrorCount() {
this.errorCount++;
}

/**
* just a getter {@link #maxStatements}
*
* @return max allowed statements to be recorded
*/
public int getMaxStatements() {
return maxStatements;
}

/**
* just a getter {@link #statementStatisticsOverflow}
*
* @return number of not stored Statements in {@link #statementStatisticsOverflow}
*/
public long getStatementStatisticsOverflow() {
return statementStatisticsOverflow;
}

/**
* a getter
*
Expand All @@ -458,9 +251,7 @@ public Date getLoggingStarted() {
public void init() {
try {
properties.load(new FileInputStream(CONFIGURATION_FILE_NAME));
maxStatements = Integer.parseInt(properties.getProperty(Logger.JDBC_LOGGER_PREFIX + "maxStatementStatistics"));
loggingEnabled = Boolean.parseBoolean(properties.getProperty(Logger.JDBC_LOGGER_PREFIX + "loggingEnabled"));
displayFullStackTrace = Boolean.parseBoolean(properties.getProperty(Logger.JDBC_LOGGER_PREFIX + "displayFullStackTrace"));
String baseNormalizeProperty = Logger.JDBC_LOGGER_PREFIX + "normalize.";
int y = 0;
while (properties.getProperty(baseNormalizeProperty + y + ".pattern") != null && properties.getProperty(baseNormalizeProperty + y + ".replacement") != null) {
Expand All @@ -480,18 +271,4 @@ public void init() {
logger[DEFAULT].error("Error reading properties file: " + CONFIGURATION_FILE_NAME + ". Using defaults.", e);
}
}

/**
* sets defaults for all Statistics
*/
public void resetStatistics() {
for (int i = 0; i < 5; i++) {
count[i] = 0L;
millis[i] = 0L;
}
errorCount = 0L;
statementStatistics = new HashMap<String, StatementCharacteristics>();
statementStatisticsOverflow = 0L;
loggingStarted = new Date();
}
}
Loading

0 comments on commit 7743ffc

Please sign in to comment.