Created
June 11, 2012 17:35
-
-
Save adutra/2911479 to your computer and use it in GitHub Desktop.
Using custom StreamProcessors with Embedmongo / How to redirect mongo output to SLF4J
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import com.mongodb.Mongo; | |
import com.mongodb.MongoException; | |
import com.mongodb.WriteConcern; | |
import de.flapdoodle.embedmongo.MongoDBRuntime; | |
import de.flapdoodle.embedmongo.MongodExecutable; | |
import de.flapdoodle.embedmongo.config.MongodConfig; | |
import de.flapdoodle.embedmongo.config.MongodProcessOutputConfig; | |
import de.flapdoodle.embedmongo.config.RuntimeConfig; | |
import de.flapdoodle.embedmongo.distribution.Version; | |
import de.flapdoodle.embedmongo.io.IStreamProcessor; | |
import de.flapdoodle.embedmongo.io.NamedOutputStreamProcessor; | |
import de.flapdoodle.embedmongo.io.StreamToLineProcessor; | |
import de.flapdoodle.embedmongo.runtime.Network; | |
/** | |
* Special subclass of {@link Mongo} that | |
* launches an embedded Mongod server instance in a separate process. | |
* | |
* @author Alexandre Dutra | |
*/ | |
public class EmbeddedMongo extends Mongo { | |
private static final Logger LOG = LoggerFactory.getLogger(EmbeddedMongo.class); | |
public static final String DEFAULT_HOST = "127.0.0.1"; | |
public static final int DEFAULT_PORT = 27017; | |
public static final WriteConcern DEFAULT_WRITE_CONCERN = WriteConcern.FSYNC_SAFE; | |
public static final Version DEFAULT_VERSION = Version.V2_0; | |
public EmbeddedMongo() throws MongoException, IOException { | |
this(DEFAULT_HOST, DEFAULT_PORT); | |
} | |
public EmbeddedMongo(int port) throws MongoException, IOException { | |
this(DEFAULT_HOST, port); | |
} | |
public EmbeddedMongo(String host, int port) throws MongoException, IOException { | |
this(host, port, DEFAULT_WRITE_CONCERN); | |
} | |
public EmbeddedMongo(String host, int port, WriteConcern writeConcern) throws MongoException, IOException { | |
this(host, port, writeConcern, DEFAULT_VERSION); | |
} | |
public EmbeddedMongo(String host, int port, WriteConcern writeConcern, Version version) throws MongoException, IOException { | |
super(host, port); | |
this.setWriteConcern(writeConcern); | |
RuntimeConfig runtimeConfig = new RuntimeConfig(); | |
IStreamProcessor mongodOutput = | |
new NamedOutputStreamProcessor("[mongod out]", | |
new StreamToLineProcessor( | |
new Slf4jStreamProcessor(LOG, Slf4jLevel.INFO))); | |
IStreamProcessor mongodError = | |
new NamedOutputStreamProcessor("[mongod err]", | |
new StreamToLineProcessor( | |
new Slf4jStreamProcessor(LOG, Slf4jLevel.WARN))); | |
IStreamProcessor commandsOutput = | |
new NamedOutputStreamProcessor("[mongod kill]", | |
new StreamToLineProcessor( | |
new Slf4jStreamProcessor(LOG, Slf4jLevel.INFO))); | |
runtimeConfig.setMongodOutputConfig(new MongodProcessOutputConfig(mongodOutput, mongodError, commandsOutput)); | |
MongoDBRuntime runtime = MongoDBRuntime.getInstance(runtimeConfig); | |
MongodExecutable mongodExe = runtime.prepare(new MongodConfig(version, port, Network.localhostIsIPv6())); | |
mongodExe.start(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.slf4j.Logger; | |
public enum Slf4jLevel { | |
TRACE { | |
@Override | |
public void log(Logger logger, String message) { | |
logger.trace(message); | |
} | |
}, | |
DEBUG { | |
@Override | |
public void log(Logger logger, String message) { | |
logger.debug(message); | |
} | |
}, | |
INFO { | |
@Override | |
public void log(Logger logger, String message) { | |
logger.info(message); | |
} | |
}, | |
WARN { | |
@Override | |
public void log(Logger logger, String message) { | |
logger.warn(message); | |
} | |
}, | |
ERROR { | |
@Override | |
public void log(Logger logger, String message) { | |
logger.error(message); | |
} | |
} | |
; | |
public abstract void log(Logger logger, String message); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.slf4j.Logger; | |
import de.flapdoodle.embedmongo.io.IStreamProcessor; | |
import de.flapdoodle.embedmongo.io.StreamToLineProcessor; | |
/** | |
* {@link IStreamProcessor} using SLF4J to log messages. | |
* Should be preferably wrapped within a {@link StreamToLineProcessor} | |
* for a better output. | |
* | |
* The constructor takes a {@link Logger} that will be used to log messages, | |
* and an instance of {@link Slf4jLevel} to determine which method to call | |
* on the {@link Logger} instance. | |
* | |
* Example of usage: | |
* | |
* <pre> | |
* Logger logger = ... | |
* IStreamProcessor mongodOutput = | |
* new NamedOutputStreamProcessor("[mongod out]", | |
* new StreamToLineProcessor( | |
* new Slf4jStreamProcessor(logger, Slf4jLevel.INFO))); | |
* </pre> | |
* | |
* @author Alexandre Dutra | |
*/ | |
public class Slf4jStreamProcessor implements IStreamProcessor { | |
private final Logger logger; | |
private final Slf4jLevel level; | |
public Slf4jStreamProcessor(Logger logger, Slf4jLevel level) { | |
this.logger = logger; | |
this.level = level; | |
} | |
@Override | |
public void process(String line) { | |
level.log(logger, stripLineEndings(line)); | |
} | |
@Override | |
public void onProcessed() { | |
} | |
protected String stripLineEndings(String line) { | |
//we still need to remove line endings that are passed on by StreamToLineProcessor... | |
return line.replaceAll("[\n\r]+", ""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment