I have the following log4j2.properties file:

status = info
name = my-app

# properties
property.name = my-app
property.basePath = log
property.filename = ${basePath}/${name}.log

# console layout
appender.console.type = Console
appender.console.name = console-logger
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{ISO8601} %-5p [%t] %c{1}.%M:%L - %msg%n

# rolling layout
appender.rolling.type = RollingFile
appender.rolling.name = rolling-file-logger
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = ${basePath}/${name}-%d{yyyy-MM-dd-hh-mm}.log
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{ISO8601} %-5p [%t] %c{1}.%M:%L - %msg%n

# rolling policies
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.strategy.type = DefaultRolloverStrategy

# root logger
rootLogger.level = info
rootLogger.appenderRefs = rolling, stdout
rootLogger.appenderRef.console.ref = console-logger
rootLogger.appenderRef.rolling.ref = rolling-file-logger

The main criteria I am aiming for is:

  1. Log files to log/my-app.log.
  2. Rollover log files each minute to log/my-app-yyyy-MM-dd-hh-mm.log.

The properties file above works for my purposes, but I would like to remove redundancy wherever possible.

A couple questions:

  1. Is there a way to use the variable name = my-app? I created another variable property.name, but not entirely sure that's necessary.
  2. Both console-logger and rolling-file-logger have the same layout.type and layout.pattern. Is there a simpler way to have both loggers use the same layout.type and layout.pattern without defining it twice?
  3. Any other opportunities for simplifying.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.