0

My application used spring boot and log4j. To change the log level at runtime , i used the following log4j.properties:

    # Root logger option
log4j.rootLogger= ${LOGGER.LEVEL}, stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.stdout.Threshold=ERROR

# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${log.file.path}//${project.artifactId}.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file.Threshold= ${LOGGER.LEVEL}

To change logging level at runtime, i have used an external application.properties where i changed the value of LOGGER.LEVEL. BUT unfortunately, no result.

Can you tell me please what should i do to resolve this problem ?

NAZEHA
  • 455
  • 9
  • 24

1 Answers1

0

I think you dont need an extra properties file. If you want to change the log level at runtime you simply do this programmatically.

Logger logger = Logger.getLogger(mylogger);
logger.setLevel(Level.INFO);

And with Level you can save the current log level if you need it:

Level previousLevel = logger.getLevel();

EDIT 1:

Try a configuration like this:

log4j.rootLogger=INFO(or what you want), stdout

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.stdout.Threshold=ERROR(or what you want)

# Redirect log messages to a log file, support file rolling.
log4j.logger.mylogger=${LOGGER.LEVEL},file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${log.file.path}//${project.artifactId}.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
#log4j.appender.file.Threshold= ${LOGGER.LEVEL}

You have to use mylogger in code.

Patrick
  • 12,336
  • 15
  • 73
  • 115
  • the LOGGER.LEVEL into external app.properties or command line is to define the log level of logging file. for example: LOGGER.LEVEL=INFO or LOGGER.LEVEL=ERROR – NAZEHA Nov 13 '15 at 08:41
  • @Angel you mean that the `logger.level.info` will write to one log file and `logger.level.error` will write to another log file? – Patrick Nov 13 '15 at 08:52
  • No, i mean that i change the level only for log messages of logging file not for log messages of console. – NAZEHA Nov 13 '15 at 09:01
  • @Angel I edited my answer. May be you have to refactor it for your needs. I think this is a similar question as yours is: [log-level-per-appender-for-a-single-logger](http://stackoverflow.com/questions/2154539/log-level-per-appender-for-a-single-logger) – Patrick Nov 13 '15 at 09:37