我在Log4J中遇到以下错误:
2015-07-07 18:24:00,974 ERROR Error processing element Flume: CLASS_NOT_FOUND
2015-07-07 18:24:01,009 ERROR Appender AuditLogger cannot be located. Route igno
red
以下是我的Log4J2 XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="MyFile" fileName="OutputLogFile.log" immediateFlush="false" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<Flume name="AuditLogger" compress="true">
<Agent host="192.168.10.101" port="8800"/>
<Agent host="192.168.10.102" port="8800"/>
<RFC5424Layout enterpriseNumber="18060" includeMDC="true" appName="MyApp"/>
</Flume>
<Routing name="Routing">
<Routes pattern="$${sd:type}">
<Route>
<RollingFile name="Rolling-${sd:type}" fileName="${sd:type}.log"
filePattern="${sd:type}.%i.log.gz">
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<SizeBasedTriggeringPolicy size="100" />
</RollingFile>
</Route>
<Route ref="AuditLogger" key="Audit"/>
</Routes>
</Routing>
</Appenders>
<Loggers>
<Root level="all">
<Appender-Ref ref="Console"/>
<Appender-Ref ref="MyFile"/> <!-- added_in now -->
</Root>
</Loggers>
</Configuration>
然后,我尝试像这样添加FlumeAppender:
import org.apache.logging.log4j.flume;
但之后它没有工作......如何设置FlumeAppender?
答案 0 :(得分:1)
<RollingFile name="MESSAGING_FILE" fileName="log/messaging.log"
filePattern="log/MM_messaging.log.%i">
<PatternLayout pattern="%m%n/>
<Policies>
<SizeBasedTriggeringPolicy size="1MB"/>
</Policies>
<DefaultRolloverStrategy max="3"/>
<Filters>
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
</RollingFile>
Here is an example that creates a new file every time the file size becomes 1 MB, you can set the size to standard sizes such as 10 kb 345 MB etc.
The %i in the file pattern must be included for doing the size based rollovers. The max variable determines the number of files it will keep. Over time you will lose the oldest logs.
Lastly the Threshold filter logs only INFO messages and Below ie. (INFO,WARN,ERROR...) to get an exact match set the onMatch to Neutral and then add another Threshold filter like this
<Filters>
<ThresholdFilter level="INFO" onMatch="NEUTRAL" onMismatch="DENY"/>
<ThresholdFilter level="EROR" onMatch="DENY" onMismatch="ACCEPT"/>
</Filters>
They get processed in order. Neutral just means move on to the next filter. Accept means it will be immediately logged. If the message gets passed through all the filters with neutral status it will be default accepted just as if there were no filters.
As far as seperating logs by output the most straightforward way is to choose the appenders class path's log to by defining the logger for that path and adding the appender ref to the log you want. Another simple way is to use a marker and markerfilter. Lastly i believe there is also the routingappender which is probably more efficient but more complicated and i don't have experience using it.
Spend some time on the developer website for log4j2 they have a relatively good api reference. It will help you with most everything you want to do in a new implementation.