Skip to content

Commit

Permalink
#604 Implement ability to plug a custom logging filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Polevoy committed May 30, 2017
1 parent 4d939c3 commit 21abad2
Show file tree
Hide file tree
Showing 20 changed files with 296 additions and 213 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import org.javalite.activejdbc.connection_config.ConnectionJndiSpec;
import org.javalite.activejdbc.connection_config.ConnectionSpec;
import org.javalite.activejdbc.dialects.*;
import org.javalite.activejdbc.logging.ActiveJDBCLogger;
import org.javalite.activejdbc.logging.LogFilter;
import org.javalite.activejdbc.logging.LogLevel;
import org.javalite.common.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -57,7 +60,7 @@ protected Configuration(){
Enumeration<URL> resources = getClass().getClassLoader().getResources("activejdbc_models.properties");
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
LogFilter.log(LOGGER, "Load models from: {}", url.toExternalForm());
LogFilter.log(LOGGER, LogLevel.INFO, "Load models from: {}", url.toExternalForm());
InputStream inputStream = null;
InputStreamReader isreader = null;
BufferedReader reader = null;
Expand Down Expand Up @@ -89,7 +92,7 @@ protected Configuration(){
throw new InitException(e);
}
if(modelsMap.isEmpty()){
LogFilter.log(LOGGER, "ActiveJDBC Warning: Cannot locate any models, assuming project without models.");
LogFilter.log(LOGGER, LogLevel.INFO, "ActiveJDBC Warning: Cannot locate any models, assuming project without models.");
return;
}
try {
Expand Down Expand Up @@ -320,7 +323,7 @@ public static String getEnv(){

if(blank(ENV)){
ENV = "development";
LOGGER.warn("Environment variable ACTIVE_ENV not provided, defaulting to '" + ENV + "'");
LogFilter.log(LOGGER, LogLevel.INFO, "Environment variable ACTIVE_ENV not provided, defaulting to '" + ENV + "'");
}
}
return ENV;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.javalite.activejdbc;

import org.javalite.activejdbc.logging.LogFilter;
import org.javalite.activejdbc.logging.LogLevel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -63,11 +65,11 @@ static void attach(String dbName, Connection connection, String extraInfo) {
throw new InternalException("You are opening a connection " + dbName + " without closing a previous one. Check your logic. Connection still remains on thread: " + ConnectionsAccess.getConnectionMap().get(dbName));
}
ConnectionsAccess.getConnectionMap().put(dbName, connection);
LogFilter.log(LOGGER, "Attached connection named: {}: to current thread: {}. Extra info: {}", dbName, connection, extraInfo);
LogFilter.log(LOGGER, LogLevel.DEBUG, "Attached connection named: {}: to current thread: {}. Extra info: {}", dbName, connection, extraInfo);
}

static void detach(String dbName){
LogFilter.log(LOGGER, "Detached connection named: {} from current thread: {}", dbName, getConnectionMap().get(dbName));
LogFilter.log(LOGGER, LogLevel.DEBUG, "Detached connection named: {} from current thread: {}", dbName, getConnectionMap().get(dbName));
getConnectionMap().remove(dbName);
}

Expand Down
22 changes: 12 additions & 10 deletions activejdbc/src/main/java/org/javalite/activejdbc/DB.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.javalite.activejdbc.connection_config.ConnectionJdbcSpec;
import org.javalite.activejdbc.connection_config.ConnectionJndiSpec;
import org.javalite.activejdbc.connection_config.ConnectionSpec;
import org.javalite.activejdbc.logging.LogFilter;
import org.javalite.activejdbc.logging.LogLevel;
import org.javalite.common.Convert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -105,7 +107,7 @@ private DB open(String driver, String url, String user, String password, Propert
Connection connection;
connection = properties == null ? DriverManager.getConnection(url, user, password)
: DriverManager.getConnection(url, properties);
LogFilter.log(LOGGER, "Opened connection: " + connection);
LogFilter.log(LOGGER, LogLevel.DEBUG, "Opened connection: " + connection);
ConnectionsAccess.attach(name, connection, url);
return this;
} catch (Exception e) {
Expand All @@ -125,7 +127,7 @@ public DB open(String jndiName) {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup(jndiName);
Connection connection = ds.getConnection();
LogFilter.log(LOGGER, "Opened connection: " + connection);
LogFilter.log(LOGGER, LogLevel.DEBUG, "Opened connection: " + connection);
ConnectionsAccess.attach(name, connection, jndiName);
return this;
} catch (Exception e) {
Expand Down Expand Up @@ -179,7 +181,7 @@ public Connection detach() {
ConnectionsAccess.detach(name); // let's free the thread from connection
StatementCache.instance().cleanStatementCache(connection);
} catch (DBException e) {
LOGGER.warn("Could not close connection! MUST INVESTIGATE POTENTIAL CONNECTION LEAK!", e);
LogFilter.log(LOGGER, LogLevel.ERROR, "Could not close connection! MUST INVESTIGATE POTENTIAL CONNECTION LEAK!", e);
}
return connection;
}
Expand All @@ -193,7 +195,7 @@ public DB open(DataSource datasource) {
checkExistingConnection(name);
try {
Connection connection = datasource.getConnection();
LogFilter.log(LOGGER, "Opened connection: " + connection);
LogFilter.log(LOGGER, LogLevel.DEBUG, "Opened connection: " + connection);
ConnectionsAccess.attach(name, connection, datasource.toString());
return this;
} catch (SQLException e) {
Expand All @@ -215,7 +217,7 @@ public DB open(String jndiName, Properties jndiProperties) {
Context ctx = new InitialContext(jndiProperties);
DataSource ds = (DataSource) ctx.lookup(jndiName);
Connection connection = ds.getConnection();
LogFilter.log(LOGGER, "Opened connection: " + connection);
LogFilter.log(LOGGER, LogLevel.DEBUG, "Opened connection: " + connection);
ConnectionsAccess.attach(name, connection,
jndiProperties.contains("url") ? jndiProperties.getProperty("url") : jndiName);
return this;
Expand Down Expand Up @@ -291,7 +293,7 @@ private DB openContext(InitialContext context, String jndiName) {
try {
DataSource ds = (DataSource) context.lookup(jndiName);
Connection connection = ds.getConnection();
LogFilter.log(LOGGER, "Opened connection: " + connection);
LogFilter.log(LOGGER, LogLevel.DEBUG, "Opened connection: " + connection);
ConnectionsAccess.attach(name, connection, jndiName);
return this;
} catch (Exception e) {
Expand Down Expand Up @@ -319,7 +321,7 @@ public void close(boolean suppressWarning) {
}
StatementCache.instance().cleanStatementCache(connection);
connection.close();
LogFilter.log(LOGGER, "Closed connection: {}", connection);
LogFilter.log(LOGGER, LogLevel.DEBUG, "Closed connection: {}", connection);
} catch (Exception e) {
if (!suppressWarning) {
LOGGER.warn("Could not close connection! MUST INVESTIGATE POTENTIAL CONNECTION LEAK!", e);
Expand Down Expand Up @@ -712,7 +714,7 @@ public void openTransaction() {
throw new DBException("Cannot open transaction, connection '" + name + "' not available");
}
c.setAutoCommit(false);
LogFilter.log(LOGGER, "Transaction opened");
LogFilter.log(LOGGER, LogLevel.DEBUG, "Transaction opened");
} catch (SQLException ex) {
throw new DBException(ex.getMessage(), ex);
}
Expand All @@ -729,7 +731,7 @@ public void commitTransaction() {
throw new DBException("Cannot commit transaction, connection '" + name + "' not available");
}
c.commit();
LogFilter.log(LOGGER, "Transaction committed");
LogFilter.log(LOGGER, LogLevel.DEBUG, "Transaction committed");
} catch (SQLException ex) {
throw new DBException(ex.getMessage(), ex);
}
Expand All @@ -745,7 +747,7 @@ public void rollbackTransaction() {
throw new DBException("Cannot rollback transaction, connection '" + name + "' not available");
}
c.rollback();
LogFilter.log(LOGGER, "Transaction rolled back");
LogFilter.log(LOGGER, LogLevel.DEBUG, "Transaction rolled back");
} catch (SQLException ex) {
throw new DBException(ex.getMessage(), ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.javalite.activejdbc.associations.*;
import org.javalite.activejdbc.cache.QueryCache;
import org.javalite.activejdbc.logging.LogFilter;
import org.javalite.common.Inflector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
125 changes: 0 additions & 125 deletions activejdbc/src/main/java/org/javalite/activejdbc/LogFilter.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.javalite.activejdbc.annotations.*;
import org.javalite.activejdbc.associations.*;
import org.javalite.activejdbc.dialects.Dialect;
import org.javalite.activejdbc.logging.LogFilter;
import org.javalite.activejdbc.logging.LogLevel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -320,7 +322,7 @@ public List<Association> getAssociationsForTarget(Class<? extends Model> targetM

protected void addAssociation(Association association) {
if (!associations.contains(association)) {
LogFilter.log(LOGGER, "Association found: {}", association);
LogFilter.log(LOGGER, LogLevel.INFO, "Association found: {}", association);
associations.add(association);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package org.javalite.activejdbc;

import org.javalite.activejdbc.associations.Many2ManyAssociation;
import org.javalite.activejdbc.logging.LogFilter;
import org.javalite.activejdbc.logging.LogLevel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -36,12 +38,12 @@ class MetaModels {
void addMetaModel(MetaModel mm, Class<? extends Model> modelClass) {
Object o = metaModelsByClassName.put(modelClass.getName(), mm);
if (o != null) {
LOGGER.warn("Double-register: {}: {}", modelClass, o);
LogFilter.log(LOGGER, LogLevel.WARNING, "Double-register: {}: {}", modelClass, o);
}
o = metaModelsByTableName.put(mm.getTableName(), mm);
many2ManyAssociations.addAll(mm.getManyToManyAssociations(Collections.<Association>emptyList()));
if (o != null) {
LOGGER.warn("Double-register: {}: {}", mm.getTableName(), o);
LogFilter.log(LOGGER, LogLevel.WARNING, "Double-register: {}: {}", mm.getTableName(), o);
}
}

Expand Down
6 changes: 4 additions & 2 deletions activejdbc/src/main/java/org/javalite/activejdbc/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.javalite.activejdbc.conversion.Converter;
import org.javalite.activejdbc.conversion.ZeroToNullConverter;
import org.javalite.activejdbc.dialects.Dialect;
import org.javalite.activejdbc.logging.LogFilter;
import org.javalite.activejdbc.logging.LogLevel;
import org.javalite.activejdbc.validation.NumericValidationBuilder;
import org.javalite.activejdbc.validation.ValidationBuilder;
import org.javalite.activejdbc.validation.ValidationException;
Expand Down Expand Up @@ -659,7 +661,7 @@ private void deleteChildrenDeep(List<? extends Association> childAssociations){
String targetTableName = metaModelOf(association.getTargetClass()).getTableName();
Class c = Registry.instance().getModelClass(targetTableName, false);
if(c == null){// this model is probably not defined as a class, but the table exists!
LOGGER.error("ActiveJDBC WARNING: failed to find a model class for: {}, maybe model is not defined for this table?"
LogFilter.log(LOGGER, LogLevel.ERROR, "ActiveJDBC WARNING: failed to find a model class for: {}, maybe model is not defined for this table?"
+ " There might be a risk of running into integrity constrain violation if this model is not defined.",
targetTableName);
}
Expand Down Expand Up @@ -1177,7 +1179,7 @@ public <P extends Model> P parent(Class<P> parentClass, boolean cache) {
}

if (fkValue == null) {
LOGGER.debug("Attribute: {} is null, cannot determine parent. Child record: {}", fkName, this);
LogFilter.log(LOGGER, LogLevel.DEBUG, "Attribute: {} is null, cannot determine parent. Child record: {}", fkName, this);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.javalite.activejdbc.conversion.BlankToNullConverter;
import org.javalite.activejdbc.conversion.Converter;
import org.javalite.activejdbc.conversion.ZeroToNullConverter;
import org.javalite.activejdbc.logging.LogFilter;
import org.javalite.activejdbc.validation.DateConverter;
import org.javalite.activejdbc.validation.EmailValidator;
import org.javalite.activejdbc.validation.NumericValidationBuilder;
Expand Down
Loading

0 comments on commit 21abad2

Please sign in to comment.