log4j2 xml configuration – Log to file and console (with different levels)

I figured it out! The <Logger> tag shouldn’t be used in this case, see Gaurang Patel’s answer for details. <?xml version=”1.0″ encoding=”UTF-8″?> <configuration status=”WARN”> <appenders> <Console name=”Console” target=”SYSTEM_OUT”> <PatternLayout pattern=”%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} – %msg%n”/> </Console> <File name=”MyFile” fileName=”logs/app.log”> <PatternLayout pattern=”%d{yyyy-mm-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} – %msg%n”/> </File> </appenders> <loggers> <root level=”debug”> <appender-ref ref=”Console” level=”info”/> … Read more

log4j2 ERROR StatusLogger Unrecognized conversion specifier

If a dependency contains log4j2 plugin, a Log4j2Plugins.dat cache file is included in the jar. When the Maven shade plugin is used to merge multiple jars with a Log4j2Plugins.dat file, only one will survive. Without the plugin definitions, errors are shown on startup. Log4j2 issue One solution for this is excluding Log4j2Plugins.dat cache file from … Read more

How to add Log4J2 appenders at runtime programmatically?

There have been a number of requests to support better programmatic configuration for Log4j 2. Sorry it took so long. As of Log4j 2.4, API was added to log4j-core to facilitate programmatic configuration. The new ConfigurationBuilder API allows users to construct component definitions. With this API, there is no need to work directly with actual … 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

log4j2 specify relative path to tomcat home dir for FileAppender

I solved my own issue. For system properties you need to prefix variables with sys:. <FastFile name=”ALog” fileName=”${sys:catalina.home}/logs/test.log” immediateFlush=”true” append=”true”> <PatternLayout> <pattern>%d %p %c{1.} [%t] %m%n</pattern> </PatternLayout> </FastFile> Found it after re-reading this part of the documentation: http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution

How to Create a Custom Appender in log4j2?

This works quite differently in log4j2 than in log4j-1.2. In log4j2, you would create a plugin for this. The manual has an explanation with an example for a custom appender here: http://logging.apache.org/log4j/2.x/manual/extending.html#Appenders It may be convenient to extend org.apache.logging.log4j.core.appender.AbstractAppender, but this is not required. When you annotate your custom Appender class with @Plugin(name=”MyCustomAppender”, …., the … Read more

Caused by: org.apache.logging.log4j.LoggingException: log4j-slf4j-impl cannot be present with log4j-to-slf4j

According to Spring documentation (as pointed out by Simon), we want to exclude the “spring-boot-starter-logging” module from all libraries, not just from “spring-boot-starter-web”. configurations { … all { exclude group: ‘org.springframework.boot’, module: ‘spring-boot-starter-logging’ } } …instead of… dependencies { … implementation(‘org.springframework.boot:spring-boot-starter’) { exclude group: ‘org.springframework.boot’, module: ‘spring-boot-starter-logging’ } } I myself had the same problem … Read more

Is log4j2 compatible with Java 11?

If someone is using Maven and is having the same issue while assembling a flat jar, here is what I did to fix the same issue: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation=”org.apache.maven.plugins.shade.resource.ManifestResourceTransformer”> <mainClass>foo.bar.Generate</mainClass> <manifestEntries> <Multi-Release>true</Multi-Release> </manifestEntries> </transformer> <transformer implementation=”org.apache.maven.plugins.shade.resource.ServicesResourceTransformer”/> </transformers> </configuration> </execution> </executions> </plugin> The important part … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)