Skip to content

Instantly share code, notes, and snippets.

@mushkevych
Last active August 29, 2015 13:56
Show Gist options
  • Save mushkevych/9042175 to your computer and use it in GitHub Desktop.
Save mushkevych/9042175 to your computer and use it in GitHub Desktop.
CSV with Log4j2
import org.apache.logging.log4j.EventLogger;
import org.apache.logging.log4j.message.StructuredDataMessage;
import java.util.ArrayList;
import java.util.List;
/**
* @author Bohdan Mushkevych
* wraps Class holds List<StructuredDataMessage> and flushes it to the EventLogger
*/
public class EventLoggerTx {
public static final String MESSAGE_KEY = "m";
private List<StructuredDataMessage> eventPipe = new ArrayList<StructuredDataMessage>();
private boolean isTransactionOn = false;
public EventLoggerTx() {
}
/**
* Method creates an event and flushes it to the filesystem immediately
* @param body message body
* @param type message type and thus - name of the log file to record the event
*/
public void logEventDirectly(String body, String type) {
StructuredDataMessage msg = new StructuredDataMessage("", "", type);
msg.put(MESSAGE_KEY, body);
EventLogger.logEvent(msg);
}
/**
* Method creates an event and keeps it in the pipeline
* The pipeline will be flushed to the filesystem during CommitTransaction method
* @param body message body
* @param type name of the table, and thus - name of the log file to record the event
*/
public void logEvent(String body, String type) {
if (!isTransactionOn) {
logEventDirectly(body, type);
} else {
StructuredDataMessage msg = new StructuredDataMessage("", "", type);
msg.put(MESSAGE_KEY, body);
eventPipe.add(msg);
}
}
public void startTransaction() {
if (isTransactionOn) {
LogHandler.write("On StartTransaction: transaction was already ON.");
}
if (!eventPipe.isEmpty()) {
LogHandler.write("On StartTransaction: pipeline was not empty. Purging " + eventPipe.size() + " elements.");
eventPipe = new ArrayList<StructuredDataMessage>();
}
isTransactionOn = true;
}
public void commitTransaction() {
if (eventPipe.isEmpty()) {
LogHandler.write("On CommitTransaction: pipeline is empty. Nothing to flush.");
}
for (StructuredDataMessage msg: eventPipe) {
EventLogger.logEvent(msg);
}
isTransactionOn = false;
eventPipe = new ArrayList<StructuredDataMessage>();
}
public void rollbackTransaction() {
if (!isTransactionOn) {
LogHandler.write("On RollbackTransaction: transaction was already OFF.");
}
LogHandler.write("On RollbackTransaction: Purging " + eventPipe.size() + " elements.");
eventPipe = new ArrayList<StructuredDataMessage>();
isTransactionOn = false;
}
}
public class HeaderContext {
public static final Map<String, TableSchema> TABLE_CONTEXT = new HashMap<String, TableSchema>();
static {
// Initialize TABLE_CONTEXT with actual data
}
public static outputHeaders() {
for (String tableName: TABLE_CONTEXT.keySet()) {
PrintWriter writer = new PrintWriter(tableName + ".header", "UTF-8");
TableSchema schema = TABLE_CONTEXT.get(tableName);
writer.println(schema.getFieldNames());
writer.close();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- status The level of internal Log4j events that should be logged to the console. -->
<!-- name The name of the configuration. -->
<!-- packages A comma separated list of package names to search for plugins. -->
<Configuration status="info" name="RoutingExample" packages="">
<Properties>
<Property name="routing_filename">${log.path}/type-$${sd:type}.log</Property>
</Properties>
<Appenders>
<Console name="STDOUT">
<PatternLayout pattern="%d{yyyyMMddHH}{GMT+0} %m%n"/>
</Console>
<Routing name="Routing">
<!-- Here, $${sd:type} inspects message.type field of incoming message for later use in routing and RollingFile setup -->
<Routes pattern="$${sd:type}">
<Route>
<!-- ${log.path} is replaced by Maven during the build with absolute path to the folder containing log files -->
<RollingFile name="RollingFile-${sd:type}"
fileName="${routing_filename}"
filePattern="${log.path}/%d{yyyyMMdd}{GMT+0}/%d{yyyyMMddHH}{GMT+0}-${sd:type}-${hostName}.%i.log.gz">
<PatternLayout>
<!-- %K{m} stands for the message passed in StructuredDataMessage map with key "m" -->
<!-- %n stands for new line -->
<Pattern>%d{yyyyMMddHH}{GMT+0} %K{m}%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="64 MB"/>
</Policies>
<DefaultRolloverStrategy max="999"/>
</RollingFile>
</Route>
</Routes>
</Routing>
</Appenders>
<Loggers>
<Logger name="EventLogger" level="info" additivity="false">
<AppenderRef ref="Routing"/>
</Logger>
<Root level="warn">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment