log4j migration to log4j2

Not Exactly related to the Question, But the below are my learning in log4j2 migration. hope it helps someone. Since the log4j vulnerability which was discovered in late 2021, many organizations have upgraded to log4j2 to overcome it. and many developers needed to understand what exactly is log4j and what is the vulnerability. I recommend … Read more

Log4J2 property substitution – default

Default Property map Looking at http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution you can specify a default property map in the configuration file. That takes this form: <Configuration status=”debug”> <Properties> <Property name=”oauthLoginLogPath”>default/location/of/oauth2.log</Property> </Properties> … <Appenders> <Appender type=”File” name=”File” fileName=”${sys:oauthLoginLogPath}”> …. </Configuration Then, if you start your app with system property -DoauthLoginLogPath=/path/oauth2.log, the File appender fileName value will first be looked up … Read more

How to log within shutdown hooks with Log4j2?

As of 2.0-beta9 this is now configurable in xml <configuration … shutdownHook=”disable”> Considering its now disabled, I guess I need to manually shutdown the logging system at the end of my shutdown hook. However I couldn’t find a means thorough the external interface, only in the internal api import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.core.config.Configurator; import org.apache.logging.log4j.core.LoggerContext; … … Read more

Configuring log4j2 and log4j using a single log4j2 xml file

I would recommend using the log4j-1.2 adapter that is included in the log4j2 distribution. That way, any libraries coded to the log4j-1.2 API will work with log4j2 without any code changes. Your classpath should include: log4j-api-2.6.1.jar log4j-core-2.6.1.jar log4j-1.2-api-2.6.1.jar log4j2.xml Your classpath should not include: log4j-1.2.x.jar log4j.properties or log4j.xml (these will be ignored by log4j2 anyway) … Read more

How to specify Log4J 2.x config location?

You could use the static method #initialize(String contextName, ClassLoader loader, String configLocation) (see source here) in org.apache.logging.log4j.core.config.Configurator. (You can pass null for the class loader.) Be aware that this class is not part of the public API so your code may break with any minor release. For completeness, you can also specify the location of … Read more

How to delete old logs with log4j2

Since 2.5, Log4j supports a custom Delete action that is executed on every rollover. You can control which files are deleted by any combination of: Name (matching a glob or a regex) Age (“delete if 14 days old or older”) Count (“keep only the most recent 3”) Size (“keep only the most recent files up … Read more

Migrating from log4j to log4j2 – properties file configuration

Here is what I constructed after going through the documentation and worked. rootLogger.level = INFO property.filename = trace.log appenders = R, console appender.console.type = Console appender.console.name = STDOUT appender.console.layout.type = PatternLayout appender.console.layout.pattern = %d %5p [%t] (%F:%L) – %m%n appender.R.type = RollingFile appender.R.name = File appender.R.fileName = ${filename} appender.R.filePattern = ${filename}.%d{yyyy-MM-dd} appender.R.layout.type = PatternLayout appender.R.layout.pattern … Read more