Skip to content

Commit

Permalink
Reformatted to standard Java conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertStewart committed Feb 5, 2017
1 parent d72ebe2 commit 05ad95c
Show file tree
Hide file tree
Showing 19 changed files with 2,376 additions and 2,367 deletions.
78 changes: 40 additions & 38 deletions src/main/java/org/log4mongo/BsonAppender.java
Expand Up @@ -21,13 +21,13 @@
import org.apache.log4j.spi.LoggingEvent;
import org.bson.BSONObject;


/**
* Abstract Log4J Appender class that stores log events in the BSON format. Concrete implementation classes must
* implement append(DBObject) to store the BSON representation of a LoggingEvent.
* Abstract Log4J Appender class that stores log events in the BSON format. Concrete implementation
* classes must implement append(DBObject) to store the BSON representation of a LoggingEvent.
* <p>
* An example BSON structure for a single log entry is as follows:
* <p>
*
* <pre>
* {
* "_id" : ObjectId("f1c0895fd5eee04a445deb00"),
Expand Down Expand Up @@ -96,49 +96,51 @@
* }
* </pre>
*
* @see <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Appender.html">Log4J Appender
* Interface</a>
* @see <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Appender.html">Log4J
* Appender Interface</a>
* @see <a href="http://www.mongodb.org/">MongoDB</a>
*/
public abstract class BsonAppender extends AppenderSkeleton {

private LoggingEventBsonifier bsonifier = new LoggingEventBsonifierImpl();
private LoggingEventBsonifier bsonifier = new LoggingEventBsonifierImpl();

/**
* @see org.apache.log4j.Appender#requiresLayout()
*/
public boolean requiresLayout() {
return ( false );
}
/**
* @see org.apache.log4j.Appender#requiresLayout()
*/
public boolean requiresLayout() {
return (false);
}

/**
* @see org.apache.log4j.AppenderSkeleton#append(org.apache.log4j.spi.LoggingEvent)
*/
@Override
protected void append( final LoggingEvent loggingEvent ) {
BSONObject bson = bsonifier.bsonify( loggingEvent );
append( bson );
}
/**
* @see org.apache.log4j.AppenderSkeleton#append(org.apache.log4j.spi.LoggingEvent)
*/
@Override
protected void append(final LoggingEvent loggingEvent) {
BSONObject bson = bsonifier.bsonify(loggingEvent);
append(bson);
}

/**
* Method implemented by a concrete class to store the BSON object.
*
* @param bson The BSON representation of a Logging Event that will be stored
*/
protected abstract void append( BSONObject bson );
/**
* Method implemented by a concrete class to store the BSON object.
*
* @param bson
* The BSON representation of a Logging Event that will be stored
*/
protected abstract void append(BSONObject bson);

/**
* @return Object used to Bsonify LoggingEvent objects
*/
public LoggingEventBsonifier getBsonifier() {
return bsonifier;
}
/**
* @return Object used to Bsonify LoggingEvent objects
*/
public LoggingEventBsonifier getBsonifier() {
return bsonifier;
}

/**
* @param bsonifier Object used to Bsonify LoggingEvent objects
*/
public void setBsonifier( LoggingEventBsonifier bsonifier ) {
this.bsonifier = bsonifier;
}
/**
* @param bsonifier
* Object used to Bsonify LoggingEvent objects
*/
public void setBsonifier(LoggingEventBsonifier bsonifier) {
this.bsonifier = bsonifier;
}

}
108 changes: 52 additions & 56 deletions src/main/java/org/log4mongo/ExtendedMongoDbAppender.java
Expand Up @@ -7,72 +7,68 @@
import java.util.LinkedHashMap;
import java.util.Map;


/**
* This appender is designed so you can add top level elements to each logging
* entry. Users can also extend MongoDbAppender themselves in order to add the
* top level elements.
* This appender is designed so you can add top level elements to each logging entry. Users can also
* extend MongoDbAppender themselves in order to add the top level elements.
* <p>
* Use case: A desire to use a common appender for unified logs across different
* code bases, such that commonly logged elements be consistent, such as
* application, eventType, etc. This is enabled by adding a property called
* rootLevelProperties with a key=value list of elements to be added to the root
* level log. See log4j.properties.sample for an example.
* Use case: A desire to use a common appender for unified logs across different code bases, such
* that commonly logged elements be consistent, such as application, eventType, etc. This is enabled
* by adding a property called rootLevelProperties with a key=value list of elements to be added to
* the root level log. See log4j.properties.sample for an example.
*
* @author Mick Knutson (http://www.baselogic.com)
*/
public class ExtendedMongoDbAppender extends MongoDbAppender {

private DBObject constants;
private DBObject constants;

private Map <String, String> rootProperties = new LinkedHashMap <String, String>();
private Map<String, String> rootProperties = new LinkedHashMap<String, String>();

/**
* @see org.apache.log4j.AppenderSkeleton#activateOptions()
*/
@Override
public void activateOptions() {
super.activateOptions();
initTopLevelProperties();
}
/**
* @see org.apache.log4j.AppenderSkeleton#activateOptions()
*/
@Override
public void activateOptions() {
super.activateOptions();
initTopLevelProperties();
}

/**
* Initialize custom top level elements to appear in a log event
* <p>
* Allows users to create custom properties to be added to the top level
* log event.
*/
public void initTopLevelProperties() {
constants = new BasicDBObject();
if ( !rootProperties.isEmpty() ) {
constants.putAll( rootProperties );
}
}
/**
* Initialize custom top level elements to appear in a log event
* <p>
* Allows users to create custom properties to be added to the top level log event.
*/
public void initTopLevelProperties() {
constants = new BasicDBObject();
if (!rootProperties.isEmpty()) {
constants.putAll(rootProperties);
}
}

/**
* This will handle spaces and empty values
* A = minus- & C=equals= & E==F
* For XML, must escape (&amp;)
*
* @param rootLevelProperties
*/
public void setRootLevelProperties( String rootLevelProperties ) {
for ( String keyValue : rootLevelProperties.split( " *& *" ) ) {
String[] pairs = keyValue.split( " *= *", 2 );
rootProperties.put( pairs[ 0 ], pairs.length == 1 ? "" : pairs[ 1 ] );
}
}
/**
* This will handle spaces and empty values A = minus- & C=equals= & E==F For XML, must escape
* (&amp;)
*
* @param rootLevelProperties
*/
public void setRootLevelProperties(String rootLevelProperties) {
for (String keyValue : rootLevelProperties.split(" *& *")) {
String[] pairs = keyValue.split(" *= *", 2);
rootProperties.put(pairs[0], pairs.length == 1 ? "" : pairs[1]);
}
}

/**
* @param bson The BSON object to insert into a MongoDB database collection.
*/
@Override
public void append( BSONObject bson ) {
if ( this.isInitialized() && bson != null ) {
if ( constants != null ) {
bson.putAll( constants );
}
super.append( bson );
}
}
/**
* @param bson
* The BSON object to insert into a MongoDB database collection.
*/
@Override
public void append(BSONObject bson) {
if (this.isInitialized() && bson != null) {
if (constants != null) {
bson.putAll(constants);
}
super.append(bson);
}
}
}
7 changes: 3 additions & 4 deletions src/main/java/org/log4mongo/LoggingEventBsonifier.java
Expand Up @@ -20,13 +20,12 @@
import org.apache.log4j.spi.LoggingEvent;
import org.bson.BSONObject;


/**
* Interface implemented by classes that create a BSON representation of a Log4J
* LoggingEvent. LoggingEventBsonifierImpl is the default implementation.
* Interface implemented by classes that create a BSON representation of a Log4J LoggingEvent.
* LoggingEventBsonifierImpl is the default implementation.
*/
public interface LoggingEventBsonifier {

BSONObject bsonify( LoggingEvent loggingEvent );
BSONObject bsonify(LoggingEvent loggingEvent);

}

0 comments on commit 05ad95c

Please sign in to comment.