2
votes

Hello my current project requires me to, as the title suggests, log to the console when, a developer is running the application on their machine , and log to a rolling log file when the application is running on the server. So far I've created a log4j2.properties file (see below) and it logs to both the console and rolling log file just fine. I've researched and I have seen examples on the Apache website, and others, using a Routing appender, but all of the examples have used a log4j2.xml file. Is it possible to create this using a log4j2.properties file? Thank you.

status = error
dest = err
name = PropertiesConfig

property.filename = C:\\MyApplications\\ExampleApplication\\Example.log

filter.threshold.type = ThresholdFilter
filter.threshold.level = debug

#LOG TO CONSOLE 
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{HH:mm:ss.SSS} [%t] %-5level %logger{1} - %m%n
appender.console.filter.threshold.type = ThresholdFilter
appender.console.filter.threshold.level = debug

#LOG TO ROLLING FILE
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = ExampleLogBackup-%d{MM-dd-yy}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{HH:mm:ss.SSS} [%t] %-5level %logger{1} - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 10
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=1MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 20
logger.rolling.name = com.myapp.example
logger.rolling.level = debug
logger.rolling.additivity = true
logger.rolling.appenderRef.rolling.ref = RollingFile

rootLogger.level = debug
rootLogger.appenderRef.stdout.ref = STDOUT
1

1 Answers

1
votes

Add below lines in your properties configuration file after RollingFileAppender configuration -

appender.routing.type = Routing
appender.routing.name = routingLogger
appender.routing.script.type = Script
appender.routing.script.name = RoutingCondition
appender.routing.script.language = JavaScript
# Condition is - if isLocalMachine VM argument is true (-DisLocalMachine=true), 
# then log messages in console else log messages in file.
appender.routing.script.value = var imports = new JavaImporter(java.lang);                                  \
                                with (imports) {                                                            \
                                    System.getProperty("isLocalMachine") == "true" ? "console" : "file";    \
                                }
appender.routing.routes.type = Routes
appender.routing.routes.route1.type = Route
appender.routing.routes.route1.ref = STDOUT
appender.routing.routes.route1.key = console
appender.routing.routes.route2.type = Route
appender.routing.routes.route2.ref = RollingFile
appender.routing.routes.route2.key = file

then use this RoutingAppender in Logger -

logger.rolling.appenderRef.routing.ref = routingLogger

Same for RootLogger -

rootLogger.appenderRef.routing.ref = routingLogger

Routing of logs will be done based on the value of isLocalMachine VM argument. if isLocalMachine VM argument is true (-DisLocalMachine=true), then log messages in console else log messages in file.

So, in your local development environment, you can set this VM argument with true value to enabling logging in console. For Server, no need to set this VM argument.