FiltersFilters allow Log Events to be evaluated to determine if or how they should be published. A Filter will be called on one of its filter methods and will return a Result, which is an Enum that has one of 3 values - ACCEPT, DENY or NEUTRAL. Filters may be configured in one of four locations:
BurstFilterThe BurstFilter provides a mechanism to control the rate at which LogEvents are processed by silently discarding events after the maximum limit has been reached.
A configuration containing the BurstFilter might look like: <?xml version="1.0" encoding="UTF-8"?> <Configuration status="warn" name="MyApp" packages=""> <Appenders> <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz"> <BurstFilter level="INFO" rate="16" maxBurst="100"/> <PatternLayout> <pattern>%d %p %c{1.} [%t] %m%n</pattern> </PatternLayout> <TimeBasedTriggeringPolicy /> </RollingFile> </Appenders> <Loggers> <Root level="error"> <AppenderRef ref="RollingFile"/> </Root> </Loggers> </Configuration> CompositeFilterThe CompositeFilter provides a way to specify more than one filter. It is added to the configuration as a filters element and contains other filters to be evaluated. The filters element accepts no parameters. A configuration containing the CompositeFilter might look like: <?xml version="1.0" encoding="UTF-8"?> <Configuration status="warn" name="MyApp" packages=""> <Filters> <MarkerFilter marker="EVENT" onMatch="ACCEPT" onMismatch="NEUTRAL"/> <DynamicThresholdFilter key="loginId" defaultThreshold="ERROR" onMatch="ACCEPT" onMismatch="NEUTRAL"> <KeyValuePair key="User1" value="DEBUG"/> </DynamicThresholdFilter> </Filters> <Appenders> <File name="Audit" fileName="logs/audit.log"> <PatternLayout> <pattern>%d %p %c{1.} [%t] %m%n</pattern> </PatternLayout> </File> <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz"> <BurstFilter level="INFO" rate="16" maxBurst="100"/> <PatternLayout> <pattern>%d %p %c{1.} [%t] %m%n</pattern> </PatternLayout> <TimeBasedTriggeringPolicy /> </RollingFile> </Appenders> <Loggers> <Logger name="EventLogger" level="info"> <AppenderRef ref="Audit"/> </Logger> <Root level="error"> <AppenderRef ref="RollingFile"/> </Root> </Loggers> </Configuration> DynamicThresholdFilterThe DynamicThresholdFilter allows filtering by log level based on specific attributes. For example, if the user's loginId is being captured in the ThreadContext Map then it is possible to enable debug logging for only that user. If the log event does not contain the specified ThreadContext item NEUTRAL will be returned.
Here is a sample configuration containing the DynamicThresholdFilter: <?xml version="1.0" encoding="UTF-8"?> <Configuration status="warn" name="MyApp" packages=""> <DynamicThresholdFilter key="loginId" defaultThreshold="ERROR" onMatch="ACCEPT" onMismatch="NEUTRAL"> <KeyValuePair key="User1" value="DEBUG"/> </DynamicThresholdFilter> <Appenders> <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz"> <BurstFilter level="INFO" rate="16" maxBurst="100"/> <PatternLayout> <pattern>%d %p %c{1.} [%t] %m%n</pattern> </PatternLayout> <TimeBasedTriggeringPolicy /> </RollingFile> </Appenders> <Loggers> <Root level="error"> <AppenderRef ref="RollingFile"/> </Root> </Loggers> </Configuration> MapFilterThe MapFilter allows filtering against data elements that are in a MapMessage.
As in this configuration, the MapFilter can be used to log particular events: <?xml version="1.0" encoding="UTF-8"?> <Configuration status="warn" name="MyApp" packages=""> <MapFilter onMatch="ACCEPT" onMismatch="NEUTRAL" operator="or"> <KeyValuePair key="eventId" value="Login"/> <KeyValuePair key="eventId" value="Logout"/> </MapFilter> <Appenders> <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz"> <BurstFilter level="INFO" rate="16" maxBurst="100"/> <PatternLayout> <pattern>%d %p %c{1.} [%t] %m%n</pattern> </PatternLayout> <TimeBasedTriggeringPolicy /> </RollingFile> </Appenders> <Loggers> <Root level="error"> <AppenderRef ref="RollingFile"/> </Root> </Loggers> </Configuration> This sample configuration will exhibit the same behavior as the preceding example since the only logger configured is the root. <?xml version="1.0" encoding="UTF-8"?> <Configuration status="warn" name="MyApp" packages=""> <Appenders> <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz"> <BurstFilter level="INFO" rate="16" maxBurst="100"/> <PatternLayout> <pattern>%d %p %c{1.} [%t] %m%n</pattern> </PatternLayout> <TimeBasedTriggeringPolicy /> </RollingFile> </Appenders> <Loggers> <Root level="error"> <MapFilter onMatch="ACCEPT" onMismatch="NEUTRAL" operator="or"> <KeyValuePair key="eventId" value="Login"/> <KeyValuePair key="eventId" value="Logout"/> </MapFilter> <AppenderRef ref="RollingFile"> </AppenderRef> </Root> </Loggers> </Configuration> This third sample configuration will exhibit the same behavior as the preceding examples since the only logger configured is the root and the root is only configured with a single appender reference. <?xml version="1.0" encoding="UTF-8"?> <Configuration status="warn" name="MyApp" packages=""> <Appenders> <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz"> <BurstFilter level="INFO" rate="16" maxBurst="100"/> <PatternLayout> <pattern>%d %p %c{1.} [%t] %m%n</pattern> </PatternLayout> <TimeBasedTriggeringPolicy /> </RollingFile> </Appenders> <Loggers> <Root level="error"> <AppenderRef ref="RollingFile"> <MapFilter onMatch="ACCEPT" onMismatch="NEUTRAL" operator="or"> <KeyValuePair key="eventId" value="Login"/> <KeyValuePair key="eventId" value="Logout"/> </MapFilter> </AppenderRef> </Root> </Loggers> </Configuration> MarkerFilterThe MarkerFilter compares the configured Marker value against the Marker that is included in the LogEvent. A match occurs when the Marker name matches either the Log Event's Marker or one of its parents.
A sample configuration that only allows the event to be written by the appender if the Marker matches: <?xml version="1.0" encoding="UTF-8"?> <Configuration status="warn" name="MyApp" packages=""> <Appenders> <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz"> <MarkerFilter marker="FLOW" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout> <pattern>%d %p %c{1.} [%t] %m%n</pattern> </PatternLayout> <TimeBasedTriggeringPolicy /> </RollingFile> </Appenders> <Loggers> <Root level="error"> <AppenderRef ref="RollingFile"/> </Root> </Loggers> </Configuration> NoMarkerFilterThe NoMarkerFilter checks that there is no marker included in the LogEvent. A match occurs when there is no marker in the Log Event.
A sample configuration that only allows the event to be written by the appender if no marker is there: <?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?> <Configuration status="warn" name="MyApp" packages=""> <Appenders> <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz"> <NoMarkerFilter onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout pattern="%d %p %c{1.} [%t] %m%n"/> <TimeBasedTriggeringPolicy /> </RollingFile> </Appenders> <Loggers> <Root level="error"> <AppenderRef ref="RollingFile"/> </Root> </Loggers> </Configuration> RegexFilterThe RegexFilter allows the formatted or unformatted message to be compared against a regular expression.
A sample configuration that only allows the event to be written by the appender if it contains the word "test": <?xml version="1.0" encoding="UTF-8"?> <Configuration status="warn" name="MyApp" packages=""> <Appenders> <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz"> <RegexFilter regex=".* test .*" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout> <pattern>%d %p %c{1.} [%t] %m%n</pattern> </PatternLayout> <TimeBasedTriggeringPolicy /> </RollingFile> </Appenders> <Loggers> <Root level="error"> <AppenderRef ref="RollingFile"/> </Root> </Loggers> </Configuration> ScriptThe ScriptFilter executes a script that returns true or false.
The sample below shows how to declare script fields and then reference them in specific components. See ScriptCondition for an example of how the Script element can be used to embed script code directly in the configuration. <?xml version="1.0" encoding="UTF-8"?> <Configuration status="ERROR"> <Scripts> <ScriptFile name="filter.js" language="JavaScript" path="src/test/resources/scripts/filter.js" charset="UTF-8" /> <ScriptFile name="filter.groovy" language="groovy" path="src/test/resources/scripts/filter.groovy" charset="UTF-8" /> </Scripts> <Appenders> <List name="List"> <PatternLayout pattern="[%-5level] %c{1.} %msg%n"/> </List> </Appenders> <Loggers> <Logger name="TestJavaScriptFilter" level="trace" additivity="false"> <AppenderRef ref="List"> <ScriptFilter onMatch="ACCEPT" onMisMatch="DENY"> <ScriptRef ref="filter.js" /> </ScriptFilter> </AppenderRef> </Logger> <Logger name="TestGroovyFilter" level="trace" additivity="false"> <AppenderRef ref="List"> <ScriptFilter onMatch="ACCEPT" onMisMatch="DENY"> <ScriptRef ref="filter.groovy" /> </ScriptFilter> </AppenderRef> </Logger> <Root level="trace"> <AppenderRef ref="List" /> </Root> </Loggers> </Configuration> StructuredDataFilterThe StructuredDataFilter is a MapFilter that also allows filtering on the event id, type and message.
As in this configuration, the StructuredDataFilter can be used to log particular events: <?xml version="1.0" encoding="UTF-8"?> <Configuration status="warn" name="MyApp" packages=""> <StructuredDataFilter onMatch="ACCEPT" onMismatch="NEUTRAL" operator="or"> <KeyValuePair key="id" value="Login"/> <KeyValuePair key="id" value="Logout"/> </StructuredDataFilter> <Appenders> <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz"> <BurstFilter level="INFO" rate="16" maxBurst="100"/> <PatternLayout> <pattern>%d %p %c{1.} [%t] %m%n</pattern> </PatternLayout> <TimeBasedTriggeringPolicy /> </RollingFile> </Appenders> <Loggers> <Root level="error"> <AppenderRef ref="RollingFile"/> </Root> </Loggers> </Configuration> ThreadContextMapFilter (or ContextMapFilter)The ThreadContextMapFilter or ContextMapFilter allows filtering against data elements that are in the current context. By default this is the ThreadContext Map.
A configuration containing the ContextMapFilter might look like: <?xml version="1.0" encoding="UTF-8"?> <Configuration status="warn" name="MyApp" packages=""> <ContextMapFilter onMatch="ACCEPT" onMismatch="NEUTRAL" operator="or"> <KeyValuePair key="User1" value="DEBUG"/> <KeyValuePair key="User2" value="WARN"/> </ContextMapFilter> <Appenders> <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz"> <BurstFilter level="INFO" rate="16" maxBurst="100"/> <PatternLayout> <pattern>%d %p %c{1.} [%t] %m%n</pattern> </PatternLayout> <TimeBasedTriggeringPolicy /> </RollingFile> </Appenders> <Loggers> <Root level="error"> <AppenderRef ref="RollingFile"/> </Root> </Loggers> </Configuration> The ContextMapFilter can also be applied to a logger for filtering: <?xml version="1.0" encoding="UTF-8"?> <Configuration status="warn" name="MyApp" packages=""> <Appenders> <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz"> <BurstFilter level="INFO" rate="16" maxBurst="100"/> <PatternLayout> <pattern>%d %p %c{1.} [%t] %m%n</pattern> </PatternLayout> <TimeBasedTriggeringPolicy /> </RollingFile> </Appenders> <Loggers> <Root level="error"> <AppenderRef ref="RollingFile"/> <ContextMapFilter onMatch="ACCEPT" onMismatch="NEUTRAL" operator="or"> <KeyValuePair key="foo" value="bar"/> <KeyValuePair key="User2" value="WARN"/> </ContextMapFilter> </Root> </Loggers> </Configuration> ThresholdFilterThis filter returns the onMatch result if the level in the LogEvent is the same or more specific than the configured level and the onMismatch value otherwise. For example, if the ThresholdFilter is configured with Level ERROR and the LogEvent contains Level DEBUG then the onMismatch value will be returned since ERROR events are more specific than DEBUG.
A sample configuration that only allows the event to be written by the appender if the level matches: <?xml version="1.0" encoding="UTF-8"?> <Configuration status="warn" name="MyApp" packages=""> <Appenders> <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz"> <ThresholdFilter level="TRACE" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout> <pattern>%d %p %c{1.} [%t] %m%n</pattern> </PatternLayout> <TimeBasedTriggeringPolicy /> </RollingFile> </Appenders> <Loggers> <Root level="error"> <AppenderRef ref="RollingFile"/> </Root> </Loggers> </Configuration> TimeFilterThe time filter can be used to restrict filter to only a certain portion of the day.
A sample configuration that only allows the event to be written by the appender from 5:00 to 5:30 am each day using the default timezone: <?xml version="1.0" encoding="UTF-8"?> <Configuration status="warn" name="MyApp" packages=""> <Appenders> <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz"> <TimeFilter start="05:00:00" end="05:30:00" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout> <pattern>%d %p %c{1.} [%t] %m%n</pattern> </PatternLayout> <TimeBasedTriggeringPolicy /> </RollingFile> </Appenders> <Loggers> <Root level="error"> <AppenderRef ref="RollingFile"/> </Root> </Loggers> </Configuration> |