Reputation: 385
I use this in my program, but how can I filter this log in log4j.properties.
Marker marker = MarkerFactory.getMarker("tp-count");
log.info(marker, "The num of tp is {}", 10);
The log4j.properties is like this:
log4j.rootLogger=info,F
log4j.appender.F=org.apache.log4j.RollingFileAppender
log4j.appender.F.File=client.log
log4j.appender.F.MaxBackupIndex=10
log4j.appender.F.MaxFileSize=100MB
log4j.appender.F.layout=org.apache.log4j.PatternLayout
log4j.appender.F.layout.ConversionPattern=[%d] %p %m (%c)%n
Upvotes: 2
Views: 12310
Reputation: 1758
The question is about using Marker in combination with configuration via propertie file. I will answer for log4j2 version 2.11. with an example.
Sample project files:
log4j2.properties
status = error
appender.ana_whitespace.type = RollingFile
appender.ana_whitespace.name = ana_whitespace
appender.ana_whitespace.fileName = ${sys:es.logs.base_path:-target}${sys:file.separator}ana_whitespace.log
appender.ana_whitespace.filter.1.type = MarkerFilter
appender.ana_whitespace.filter.1.onMismatch=DENY
appender.ana_whitespace.filter.1.onMatch=ACCEPT
appender.ana_whitespace.filter.1.marker=ANA_WHITESPACE
appender.ana_whitespace.policies.type = Policies
appender.ana_whitespace.policies.time.type = TimeBasedTriggeringPolicy
appender.ana_whitespace.policies.time.interval = 1
appender.ana_whitespace.policies.time.modulate = true
rootLogger.level = info
rootLogger.appenderRef.ana_whitespace.ref = ana_whitespace
Example Java code
package de.es.stemmer;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;
import org.apache.http.client.ClientProtocolException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import org.apache.logging.log4j.ThreadContext;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.message.ObjectMessage;
public class JsonLoggerTest {
final static Logger log = LogManager.getLogger(JsonLoggerTest.class);
final static Marker MARKER_WHITESPACE = MarkerManager.getMarker("ANA_WHITESPACE");
public static void main(String[] args) throws ClientProtocolException, IOException {
System.setProperty("es.logs.base_path", "target");
LoggerContext.getContext().reconfigure();
log.info(MARKER_WHITESPACE, "msg_sourceSnippet whitespace");
}
}
Upvotes: 2
Reputation: 1906
Assuming you are using log4j 2.0, You can filter your logs using MarkerFilter
as described in manual
Also please note, that you need to switch from properties
configuration to xml
configuration in order to enable support of Filters as clarified here. You just can not implement filtering using properties
approach!
You can use this online tool to quickly convert properties to xml. Consult with this section of manual to make sure your log4j2.xml is placed and named correctly so that it will be picked up by your application automatically
Your final log4j2.xml can look like as in following example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="F" class="org.apache.log4j.RollingFileAppender">
<!-- below forces inclusion of only log events with marker 'tp-count' to this client.log-->
<MarkerFilter marker="tp-count" onMatch="ACCEPT" onMismatch="DENY"/>
<param name="File" value="client.log"/>
<param name="MaxBackupIndex" value="10"/>
<param name="MaxFileSize" value="100MB"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d] %p %m (%c)%n"/>
</layout>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="F"/>
</root>
</log4j:configuration>
Upvotes: 4