I am developing a logger where certain transaction messages need to be logged to a file and some application logs need to be logged to the console. Currently, the transaction messages are also being logged onto the console and I want to avoid that.
for eg:
if i write logger.info("application process started");
this needs to be logged on console .
and when i write logger.info(new ObjectArrayMessage("msg1","msg2","msg3"));
this need to be logged to a csv file .
This is my current output:
19:18:42.230 [main5] INFO New.CSVlog.App - application process started
19:18:42.233 [main5] INFO New.CSVlog.App - [msg1,msg2,msg3]
I want to log only the first log to the console and the second one to the CSV file.
I have the following properties file:
appenders = csvFile, console
appender.csvFile.type = RollingFile
appender.csvFile.name = CSVFILE
appender.csvFile.fileName = csvLog.csv
appender.csvFile.filePattern= csvLog-%d{MM-dd-yyyy}-%i.csv
appender.csvFile.layout.type = CsvParameterLayout
appender.csvFile.layout.delimiter = ,
appender.csvFile.layout.header = column1,column2,column3\n
appender.csvFile.policies.type = Policies
appender.csvFile.policies.time.type = TimeBasedTriggeringPolicy
appender.csvFile.policies.time.interval = 1
appender.csvFile.policies.time.modulate = true
appender.csvFile.policies.size.type = SizeBasedTriggeringPolicy
appender.csvFile.policies.size.size=10MB
appender.csvFile.strategy.type = DefaultRolloverStrategy
appender.csvFile.strategy.max = 200
appender.console.type = Console
appender.console.name = consoleLog
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{HH:mm:ss.SSS} [%t5] %-5level %logger{36} - %msg%n
rootLogger.level =debug
rootLogger.appenderRefs = csvFile, consoleLog
rootLogger.appenderRef.csvFile.ref = CSVFILE
rootLogger.appenderRef.console.ref = consoleLog
Can anyone please help me to use the same logger for both transaction and application logging? and what changes do I need to make in my properties file? Thanks!
You can use Filter
s to seperate ObjectArrayMessage
messages from the rest (cf. documentation ). Although there is no built-in filter that checks the class of the Message
object, you can easily write one:
@Plugin(name = "ObjectArrayMessageFilter", category = Node.CATEGORY, elementType = Filter.ELEMENT_TYPE)
public class ObjectArrayMessageFilter extends AbstractFilter {
public static class Builder extends AbstractFilterBuilder<Builder>
implements org.apache.logging.log4j.core.util.Builder<ObjectArrayMessageFilter> {
@Override
public ObjectArrayMessageFilter build() {
return new ObjectArrayMessageFilter(getOnMatch(), getOnMismatch());
}
}
@PluginBuilderFactory
public static Builder newBuilder() {
return new Builder();
}
private ObjectArrayMessageFilter(Result onMatch, Result onMismatch) {
super(onMatch, onMismatch);
}
private static Result filter(Object msg) {
return msg instanceof ObjectArrayMessage ? onMatch : onMismatch;
}
@Override
public Result filter(final LogEvent event) {
return filter(event.getMessage());
}
@Override
public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
final Throwable t) {
return filter(msg);
}
@Override
public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
final Throwable t) {
return filter(msg);
}
}
You can then attach the filter to the appenders, so that it is always applied to a given appender:
appender.csvFile.type = RollingFile
...
appender.csvFile.filter.omaf.type = ObjectArrayMessageFilter
appender.csvFile.filter.omaf.onMatch = ACCEPT
appender.csvFile.filter.omaf.onMismatch = DENY
appender.console.type = Console
...
appender.console.filter.omaf.type = ObjectArrayMessageFilter
appender.console.filter.omaf.onMatch = DENY
appender.console.filter.omaf.onMismatch = ACCEPT
Remark : When developing custom plugins, some standard rules apply: cf. documentation . Basically the plugin's code must be compiled with the annotation processor in log4j-core
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.