Log4j2 Arguments given for element Logger are invalid - java

I am facing below error on upgradation of log4j 1.x to log4j2.
As per documentation on log4j2 site I have prepared below log4j2 file, but still I am getting this exception.
Log4j2 version: 2.6.1, Weblogic: 10.3.6, jdk 1.7.x
Please help with this exception:
2016-06-25 05:49:36,002 [ACTIVE] ExecuteThread: '70' for queue: 'weblogic.kernel.Default (self-tuning)' ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.config.LoggerConfig for element Logger. org.apache.logging.log4j.core.config.ConfigurationException: Arguments given for element Logger are invalid
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.generateParameters(PluginBuilder.java:270)
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:131)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:918)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:858)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:850)
Log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG" >
<Properties>
<Property name="theHostName">${hostName}</Property>
</Properties>
<!-- bufferedIO=true by default, bufferSize=8192bytes
-->
<Appenders>
<RollingFile name="FILE" filename="${sys:weblogic.Name}.log" filepattern="${sys:weblogic.Name}.log.%i" append="true" >
<PatternLayout pattern="[%-5p][%d{yyyy-MM-dd HH:mm:ss,SSS}][${sys:weblogic.Name}:${hostName}][%t][%X{MessageInfo}][%c{1}:%M:%L][%msg]%n" />
<Policies>
<SizeBasedTriggeringPolicy size="50 MB" />
</Policies>
<DefaultRolloverStrategy max="100" fileIndex="min"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger level="DEBUG" includeLocation="true">
<AppenderRef ref="FILE"/>
</Logger>
<Root level="INFO" includeLocation="true">
<AppenderRef ref="FILE"/>
</Root>
<!-- Package specific log level defines -->
<logger level="WARN" name="org.springframework" />
<logger level="WARN" name="org.jboss" />
<logger level="OFF" name="org.hibernate" />
<logger level="WARN" name="com.company.project.eligibility" />
</Loggers>
</Configuration>

Only the root logger can have no name, so this Logger needs to have a name:
<Logger level="DEBUG" includeLocation="true">
<AppenderRef ref="FILE"/>
</Logger>

I guess package specific levels should be commented out or declared like this:
<Logger level="WARN" name="org.springframework" />
<Logger level="WARN" name="org.jboss" />
<Logger level="OFF" name="org.hibernate" />
<Logger level="WARN" name="com.company.project.eligibility" />

Related

How to configure log4j2.xml from file to stdout?

Here is my current log4j2.xml file :
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info">
<Properties>
<Property name="logFilename">abc</Property>
<Property name="metricsLogFilename">abc-metrics</Property>
<Property name="pattern">%d [%X{idreq} %X{iduser} %X{codeenv}] %-5p
[%c{1}] : %m%n</Property>
<Property name="metricsPattern">%d : %m%n</Property>
<Property name="level">${sys:LOG4J_LEVEL:-info}</Property>
</Properties>
<Appenders>
<Console name="console" target="SYSTEM_OUT" follow="true" >
<PatternLayout pattern="${pattern}" />
</Console>
<RollingRandomAccessFile name="file"
fileName="${tex:appLogFilePath}${logFilename}.log"
filePattern="${tex:appLogFilePath}${logFilename}-%i.log.gz">
<PatternLayout pattern="${pattern}" />
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy max="1" />
</RollingRandomAccessFile>
<RollingRandomAccessFile name="metricsFile"
fileName="${tex:appLogFilePath}${metricsLogFilename}.log"
filePattern="${tex:appLogFilePath}${metricsLogFilename}-%i.log.gz"
immediateFlush="false">
<PatternLayout pattern="${metricsPattern}" />
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy max="1" />
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<Logger name="com.myClass.common" level="info" additivity="false">
<AppenderRef ref="console" />
<AppenderRef ref="file" />
</Logger>
<Logger name="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" level="${level}" additivity="false">
<AppenderRef ref="console" />
<AppenderRef ref="file" />
</Logger>
<Logger name="metrics" level="info" additivity="false">
<AppenderRef ref="metricsFile" />
</Logger>
<Root level="${level}">
<AppenderRef ref="console" />
<AppenderRef ref="file" />
</Root>
</Loggers>
I want to switch to stdout instead of file logs. What should I change ? (I've never used log4j)
The reason why I want to have it stdout is because I'll need to deploy my app on heroku, and on heroku we need to use system out logging.
As per current configuration, except metrics, your are logging at both places - in a file as well as on console. For metrices, you are logging in file only.
If you can want to log only at console, remove other appender and their references from logger configuration. For metrices, you have to refer console appender.
Update file may look like -
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info">
<Properties>
<Property name="pattern">%d [%X{idreq} %X{iduser} %X{codeenv}] %-5p
[%c{1}] : %m%n</Property>
<Property name="level">${sys:LOG4J_LEVEL:-info}</Property>
</Properties>
<Appenders>
<Console name="console" target="SYSTEM_OUT" follow="true" >
<PatternLayout pattern="${pattern}" />
</Console>
</Appenders>
<Loggers>
<Logger name="com.myClass.common" level="info" additivity="false">
<AppenderRef ref="console" />
</Logger>
<Logger name="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" level="${level}" additivity="false">
<AppenderRef ref="console" />
</Logger>
<Logger name="metrics" level="info" additivity="false">
<AppenderRef ref="console" />
</Logger>
<Root level="${level}">
<AppenderRef ref="console" />
</Root>
</Loggers>

How to setup log4j2 asyncQueueFullPolicy

I am using log4j-core-2.5 for my logging and I would like to Discard items when the logging queue is full. I have determined that this could be applied by using the log4j2.asyncQueueFullPolicy configuration property in log4j.
Could somone please help me with how to apply this configuration in my application.
I am using a file configuration like so...
Configurator.initialize("", "log4j-node.xml");
My config file looks somthing like this
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="[%d{dd-MMM HH:mm:ss.SSS} %logger{1}.%t %-5level] %msg%n"/>
</Console>
<RollingFile name="DefaultFile" fileName="../logs/app.log"
filePattern="../logs/app-%d{MM-dd-yyyy}-%i.log" bufferedIO="true" bufferedSize="32000" immediateFlush="false">
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
...
</RollingFile>
<Async name="AsyncFile">
<AppenderRef ref="DefaultFile"/>
<LinkedTransferQueue/>
</Async>
<Async name="AsyncConsole">
<AppenderRef ref="Console"/>
<LinkedTransferQueue/>
</Async>
</Appenders>
<logger name="org.hibernate">
<level value="debug" />
</logger>
<Loggers>
<Root level="TRACE">
<AppenderRef ref="AsyncConsole"/>
<AppenderRef ref="AsyncFile"/>
</Root>
<Logger name="org.hibernate.SQL" level="error" />
<!-- Log all JDBC parameters -->
<Logger name="org.hibernate.type" level="error" />
<!-- Log all SQL DDL statements as they are executed -->
<Logger name="org.hibernate.tool.hbm2ddl" level="error" />
<!-- Log the state of all entities (max 20 entities) associated with the session at flush time -->
<Logger name="org.hibernate.pretty" level="error" />
<!-- Log all second-level cache activity -->
<Logger name="org.hibernate.cache" level="error" />
<!-- Log transaction related activity -->
<Logger name="org.hibernate.transaction" level="error" />
<!-- Log all JDBC resource acquisition -->
<Logger name="org.hibernate.jdbc" level="error" />
<!-- Log HQL and SQL ASTs during query parsing -->
<Logger name="org.hibernate.hql.ast.AST" level="error" />
<!-- Log all JAAS authorization requests -->
<Logger name="org.hibernate.secure" level="error" />
<!-- Log everything (a lot of information, but very useful for troubleshooting) -->
<Logger name="org.hibernate" level="error" />
</Loggers>
</Configuration>
Cheers
First, please upgrade to Log4j 2.10, which fixed an important issue in this area where log events no longer appear out of order in the log file when the queue is full.
You can configure Log4j2's behaviour via system property log4j2.AsyncQueueFullPolicy. See AsyncQueueFullPolicy and AsyncQueueFullPolicyFactory. The "Discard" policy with some log level may be of interest.
Property log4j2.AsyncQueueFullPolicy controls the routing
behaviour. If this property is not specified or has value "Default",
this factory creates DefaultAsyncQueueFullPolicy objects.
If this property has value "Discard", this factory creates
DiscardingAsyncQueueFullPolicy objects. By default, this router
discards events of level INFO, DEBUG and TRACE if the queue is full.
This can be adjusted with property log4j2.DiscardThreshold (name of
the level at which to start discarding).

Unable to turn off/restrict logging for one of the ApacheDS class

I am using the following log4j2 configuration to restrict the logging from one of the ApacheDS classes to ERROR and lesser.
<logger level="error" name="org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmIndex">
<appender-ref ref="ARTFile" />
</logger>
The messages from this particular class are still getting logged at DEBUG level. A similar setting for the following package worked.
org.apache.directory.server.core
Please help.
Here is the complete configuration.
<?xml version="1.0" encoding="UTF-8"?>
<configuration name="defaultConfiguration" strict="true" monitorInterval="5">
<properties>
<property name="patternlayout">%-5p %d{ISO8601} [%t][%4C][%L] - %m%n</property>
<property name="filename">C:\\Releases\\RCL\\RLKSAdmin\\work\\rcladmin\\logs\\RLKS_ART.log</property>
<property name="filenamePattern">C:\\Releases\\RCL\\RLKSAdmin\\work\\rcladmin\\logs\\RLKS_ART_%i.log</property>
</properties>
<appenders>
<RollingFile name="ARTFile" fileName="${filename}" filePattern="${filenamePattern}" append="true">
<PatternLayout pattern="${patternlayout}" charset="UTF-8" />
<Policies>
<SizeBasedTriggeringPolicy size="3 MB" />
</Policies>
<DefaultRolloverStrategy max="5" />
</RollingFile>
</appenders>
<loggers>
<root level="trace">
<appender-ref ref="ARTFile" />
</root>
<logger level="error" name="org.springframework.core">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.springframework.beans">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.springframework.context">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.springframework.web">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.springframework.security">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.springframework.ldap">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.springframework.jndi">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.springframework.validation">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.springframework.ui">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.springframework.util">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.apache.directory.server.schema">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmIndex"/>
<logger level="error" name="org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmTable"/>
<logger level="error" name="org.apache.directory.server.core">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.apache.directory.server.core.partition">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.apache.directory.server.protocol">
<appender-ref ref="ARTFile" />
</logger>
<logger level="error" name="org.apache.directory.shared">
<appender-ref ref="ARTFile" />
</logger>
</loggers>
</configuration>
If your goal is to restrict the log level it may be better not to specify the appender-ref: that way the restriction applies to the root logger and all associated appenders. In the configuration snippet you showed, the restriction only applies to the "ARTFile" appender.

Log file not being generated/written to

I previously had this working where it would log to a file on my C: drive however trying to get this working into a logs folder using the CATALINA_HOME environment variable doesn't seem to work.
CATALINA_HOME environment variable is set to C:\apache-tomcat\bin and I was hoping to store the logs in C:\apache-tomcat\logs
Have I done something silly with my configuration file?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="LOG_DIR">${sys:CATALINA_HOME}../logs</Property>
<Property name="ARCHIVE">${LOG_DIR}/archive</Property>
<Property name="PATTERN">%d{dd/MMM/yyyy HH:mm:ss.SSS} [%t] %X{id} %X{username} %-5level %c{36} %l: %msg%n</Property>
</Properties>
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="${PATTERN}" />
</Console>
<RollingFile name="FileAppender" fileName="${LOG_DIR}/application.log"
filePattern="${ARCHIVE}/application.log.%d{yyyy-MM-dd-hh-mm}.gz">
<PatternLayout pattern="${PATTERN}" />
<TimeBasedTriggeringPolicy />
</RollingFile>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="FileAppender" />
<AppenderRef ref="CONSOLE" />
</Root>
<Logger name="uk.co" level="INFO" additivity="false">
<appender-ref ref="FileAppender" />
<appender-ref ref="CONSOLE" />
</Logger>
</Loggers>
<Logger name="org.hibernate" level="INFO" additivity="false">
<appender-ref ref="FileAppender" />
<appender-ref ref="CONSOLE" />
</Logger>
<Logger name="org.apache" level="INFO" additivity="false">
<appender-ref ref="FileAppender" />
<appender-ref ref="CONSOLE" />
</Logger>
<Logger name="org.springframework" level="INFO" additivity="false">
<appender-ref ref="FileAppender" />
<appender-ref ref="CONSOLE" />
</Logger>
</Configuration>
Resolved by changing ${sys:CATALINA_HOME} to ${env:CATALINA_HOME}
Also added a missing / thank to RC

Loggers to Multiple Appenders at Different Thresholds by Logger

I have two appenders. One is attached to the console and one is to a log file. But that's not really that important. The issue I have is I want everything to go to the file at INFO level. The console is different however. There a bunch of loggers I don't want touching the console at WARN or INFO level because they spray a lot more information than the user needs.
So lets say I have three loggers A, B, C. A, B, and C should all go to the file appenders at INFO level. A should go to the console for INFO, B should go to the console for WARN and C should go to the console for ERROR. What does a log4j config to do this look like?
Something like this should work:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz"
ignoreExceptions="false">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
<Console name="STDOUT" target="SYSTEM_OUT" ignoreExceptions="false">
<PatternLayout pattern="%m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="A" level="trace" additivity="false">
<AppenderRef ref="STDOUT" level="info" />
<AppenderRef ref="RollingFile" level="info"/>
</Logger>
<Logger name="B" level="trace" additivity="false">
<AppenderRef ref="STDOUT" level="warn" />
<AppenderRef ref="RollingFile" level="info"/>
</Logger>
<Logger name="C" level="trace" additivity="false">
<AppenderRef ref="STDOUT" level="error" />
<AppenderRef ref="RollingFile" level="info"/>
</Logger>
<Root level="trace">
<AppenderRef ref="STDOUT" level="trace" />
<AppenderRef ref="RollingFile" level="trace"/>
</Root>
</Loggers>

Categories

Resources