How to set log level in QAF / Qmetry framework? - qaf

I would like to reduce the log level displayed in the console during my automation run in QAF / QMetry framework. When I tried playing around in log4j.properties, it is not showing any difference in the console logging. Tried following in log4j.properties:
log4j.logger.com.qmetry.qaf=ERROR
log4j.rootCategory=off
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=ERROR
log4j.appender.CONSOLE.Follow=false
Note: Not seeing any ws.log file created as mentioned in the below property
log4j.logger.com.qmetry.qaf.automation.ws=DEBUG, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.file=${outputDir}/ws.log
log4j.appender.file.MaxFileSize=1GB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.conversionPattern=%d{[dd.MM.yyyy] [HH:mm:ss]} %p [%t] %c (%F:%L) - %m%n
Log4j is set in the Maven config: (I have also tried putting the log4j config in the src folder).
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>${testSuiteFile}</suiteXmlFile>
</suiteXmlFiles>
<reportsDirectory>${test.results.dir}/${run.time}</reportsDirectory>
<systemPropertyVariables>
<org.uncommons.reportng.xml-dialect>testng</org.uncommons.reportng.xml-dialect>
<org.uncommons.reportng.escape-output>false</org.uncommons.reportng.escape-output>
<log4j.configuration>file:///${resource.dir}/log4j.properties</log4j.configuration>
<outputDir>${output.dir}</outputDir>
<test.results.dir>${output.dir}/html</test.results.dir>
<json.report.root.dir>${test.results.dir}</json.report.root.dir>
<json.report.dir>${output.dir}/json</json.report.dir>
<selenium.screenshots.dir>${output.dir}/img</selenium.screenshots.dir>
<selenium.screenshots.relative.path>../img</selenium.screenshots.relative.path>
</systemPropertyVariables>
</configuration>
</plugin>

You need to set log4j properties file location. Refer sample ant or maven project. If you are not familiar, easiest way is to move log4j.properties file under src directory.

Related

Log4J2 StatusLogger messages are not logged in ODL

I have recently upgraded log4j2 in my application. It is running on top of the open day light controller. The pax-logging-log4j2 (version 1.11.3) bundle is added to the karaf start up bundles. With Log4J2, I have enabled the asynchronous logging as well. The below VM argument is added in the karaf start up script for enabling asynchronous logging.
-DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
-DAsyncLogger.WaitStrategy=Sleep
Application logging is working as expected but sometimes, the logs are not in order (timestamps are overlapped). To resolve this, I have tried to set AsyncQueueFullPolicy to ENQUEUE and RingBufferSize to 4098*1024 but no luck.
-Dlog4j2.AsyncQueueFullPolicy=Enqueue
-DAysncLogger.RingBufferSize=4194304
Tried to enable Log4J2 status logger (as like below) but the logs are not getting logged.
status=debug is added the "org.ops4j.apache.pax.logging.cfg" file.
Couple of issues, log4j2 status logger is not logged and application logs are not in order.
Any help here would be of great help.
EDIT:
Further analysis of the status logger shows asynchronous logging is reverted to synchronous with reason "com.lmax.disruptor.EventFactory cannot be found".
org.ops4j.pax.logging.pax-logging-api [log4j2] WARN : Asynchronous loggers defined, but the disruptor library is not available. Reverting to synchronous loggers. Ignored FQCN: org.apache.logging.log4j.spi.AbstractLogger
java.lang.ClassNotFoundException: com.lmax.disruptor.EventFactory cannot be found by org.ops4j.pax.logging.pax-logging-log4j2_1.11.3
at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:439)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:352)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:344)
at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:160)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.ops4j.pax.logging.log4j2.internal.PaxLoggingServiceImpl.updated(PaxLoggingServiceImpl.java:263)
at org.ops4j.pax.logging.log4j2.internal.PaxLoggingServiceImpl$1ManagedPaxLoggingService.updated(PaxLoggingServiceImpl.java:581)
at org.apache.felix.cm.impl.helper.ManagedServiceTracker.updated(ManagedServiceTracker.java:189)
at org.apache.felix.cm.impl.helper.ManagedServiceTracker.updateService(ManagedServiceTracker.java:152)
at org.apache.felix.cm.impl.helper.ManagedServiceTracker.provideConfiguration(ManagedServiceTracker.java:85)
at org.apache.felix.cm.impl.ConfigurationManager$ManagedServiceUpdate.provide(ConfigurationManager.java:1463)
at org.apache.felix.cm.impl.ConfigurationManager$ManagedServiceUpdate.run(ConfigurationManager.java:1419)
at org.apache.felix.cm.impl.UpdateThread.run0(UpdateThread.java:141)
at org.apache.felix.cm.impl.UpdateThread.run(UpdateThread.java:109)
at java.lang.Thread.run(Thread.java:748)
Added "pax-logging-log4j2-extra" in startup.properties and the dependency issues are resolved.
AsyncLoggerContext is created now.
org.ops4j.pax.logging.pax-logging-api [log4j2] DEBUG : Starting LoggerContext[name=pax-logging, org.apache.logging.log4j.core.async.AsyncLoggerContext#1eb9a021] with configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration#3810215b... Ignored FQCN: org.apache.logging.log4j.spi.AbstractLogger
but it is not picking the custom AsyncQueueFullPolicy added in the VM argument.
-Dlog4j2.AsyncQueueFullPolicy=a.b.c.d.BlockingQueueFullPolicy
Logs:
org.ops4j.pax.logging.pax-logging-api [log4j2] DEBUG : Using DefaultAsyncQueueFullPolicy. Could not create custom AsyncQueueFullPolicy 'a.b.c.d.BlockingQueueFullPolicy': java.lang.ClassNotFoundException: com.fujitsu.fnc.sdnfw.async.BlockingQueueFullPolicy cannot be found by org.ops4j.pax.logging.pax-logging-api_1.11.3 Ignored FQCN: org.apache.logging.log4j.spi.AbstractLogger
Created a fragment bundle with the below custom policy and installed it in karaf.
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.async.AsyncQueueFullPolicy;
import org.apache.logging.log4j.core.async.EventRoute;
public class BlockingQueueFullPolicy implements AsyncQueueFullPolicy {
public BlockingQueueFullPolicy() {
// Don't Delete. Default constructor is required
}
#Override
public EventRoute getRoute(long backgroundThreadId, Level level) {
return EventRoute.ENQUEUE;
}
}
and pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>a.b.c.d.fw-async-policy-api</Bundle-SymbolicName>
<Bundle-Name>Custom Async Queue Full Policy API</Bundle-Name>
<Bundle-Vendor>XYZ</Bundle-Vendor>
<Bundle-ActivationPolicy>eager</Bundle-ActivationPolicy>
<Embed-Transitive>
false
</Embed-Transitive>
<Export-Package>
a.b.c.d
</Export-Package>
<Fragment-Host>system.bundle; extension:=framework</Fragment-Host>
<Import-Package>*</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.slf4j.impl.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.slf4j.impl.version}</version>
</dependency>
</dependencies>

Log4j2 logging level becomes different in Java 8 and Java 11

Log4j2 release 2.11.0 / 2.11.1 worked fine in Java 8 applications but not in Java 11
Here is an example of log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="log-path">C:\\MyTestXXXX\\EnterpriseLogs</Property>
</Properties>
<Appenders>
<RollingFile name="MyTestOnly" fileName="${log-path}\ServerLog.log" filePattern="${log-path}\ServerLog_%d{yyyy-MM-dd}_%i.log" append="true">
<PatternLayout>
<Pattern>[#%d{HH:mm:ss}|%p|%m#]%n</Pattern>
</PatternLayout>
<Policies>
<CronTriggeringPolicy schedule="0 0 0 * * ?" evaluateOnStartup="true"/>
<SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
<DefaultRolloverStrategy/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="OnlyLOG" additivity="true">
<AppenderRef ref="MyTestOnly" level="ALL" />
<Logger>
<Root level="ALL" />
</Loggers>
</Configuration>
In the Java source code, I did this:
import org.apache.logging.log4j.*;
import org.apache.logging.log4j.core.Logger;
...
// Get org.apache.logging.log4j.core.Logger
Logger logger = (Logger) LogManager.getLogger("OnlyLOG");
logger.log(Level.forName("INFO", 600), "Log this text now!", new Exception("xxxxxx"));
Running in Java 8, I can see that serverLog.log has the content, which is good.
Running in Java 11, there is no log.
Through further study (debugging), I found this line interesting:
Logger logger = (Logger) LogManager.getLogger("OnlyLOG");
After execution, in Java 8, logger was something like "ALL in 2f333739", and log was fine.
But in Java 11, logger was something like "ERROR in 2f333739" and there was no log. Obviously the level became "ERROR" instead of "ALL".
So, I tries this in Java 11:
Level loggerLevel = logger.getLevel(); // Obviously it is ERROR level
logger.log(loggerLevel, "Log this text now!", new Exception("xxxxxx"));
The log happened in console (likely went through Root), not in the ServerLog.log that was configured in log4j2.xml for logger OnlyLOG.
So, what caused the level difference between Java 8 and Java 11? In other words, what should be modified so that in Java 11, log can still work the same way as in Java 8?
The issue is resolved. Here is the reason.
I used a new Eclipse IDE to run Java 11, when adding a project, I added lib folder to the class path, since log4j2.xml was in the lib folder. Now I inspected the IDE configuration a second time and found out that the lib folder was not in the class path. After a second attempt, the configuration is fine and logging becomes normal.
I made a second test by adding a brand new project into the Eclipse IDE. Interestingly enough, when adding lib folder to the class path, indeed I had to try twice, because the first time it was not added. By the way, the computer runs Windows 10. This did not happen when using a computer running in Windows 7.
Regardless, Log4j2 works fine in both Java 8 and Java 11.

Using a property from one property file into log4j2 properties file

I have one properties file, let's say abc.properties and a log4j2.properties file. I am unable to access the logs.dir property which is present in abc.properties file into log4j2.properties file. This is done in order to switch the log file location based on different environment. Thanks.
If abc.properties and log4j2.properties configuration file are on the same directory then access logs.dir using below way in log4j2.properties file -
property.fileName=${bundle:abc:logs.dir}
Adding more details about environment as asked in comments -
Project Structure - A simple Maven project.
Dependency - I tried with version log4j2 version 2.8.2 , below are dependencies in pom.xml -
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
Content of abc.properties -
logs.dir = ./logs/abcfile.log
Relevant content of log4j2 -
appender.rolling.type = RollingFile
appender.rolling.name = fileLogger
appender.rolling.fileName=${bundle:abc:logs.dir}
appender.rolling.filePattern=${basePath}app_%d{yyyyMMdd}.log.gz
As you can see in screenshot also, logs are getting generated on the path specified in abc.properties file.

Log file doesn't create in Log 4j with JSF 2

I have created Web Application by using JSF 2.0, Log 4j 1.2.14 and JBoss 7. When I run testcase, the log file is created. And the log file can't create when I run web application.
I there is anything I need to configur, please tell me.
Take a look at this maybe can help you.
The following filejboss-deployment-structure.xmlneeds to contain the following:
<jboss-deployment-structure>
<deployment>
<!-- Exclusions allow you to prevent the server from automatically adding some dependencies -->
<exclusions>
<module name="org.apache.log4j" />
</exclusions>
</deployment>
</jboss-deployment-structure>
Make sure the configuration file (log4j.xml or log4j.properties) is in the classpath of the web application (in this case, in the binaries).
WEB-INF/classes/log4j.properties
If you have both files (log4.properties, log4j.xml) only is considered log4j.xml. The first time you init or use some instance of org.apache.log4j.Logger, log4j search the configuration file in the classpath, then the configuration is loaded.
If you want to see this process of searching and loading more closely, add the following argument to the virtual machine:
-Dlog4j.debug

Tapestry getting started

All goes OK until mvn jetty:run which ends with [ERROR] Failed to execute goal org.mortbay.jetty:maven-jetty-plugin:6.1.16:run (default-cli) on project newapp: Failure: Address already in use: bind
That is no surprise as I have Tomcat running on 8080 with my own site (that can not be changed). Did mvn indeed install jetty and tries running it? May I configure Jetty to a different port or maybe use Tomcat instead?
You can configure Jetty to use another port by setting the system property jetty.port.
The jetty.port property can be set in the command line when running Maven:
mvn -Djetty.port=9090 jetty:run
Or it can be set in the project's pom.xml, as part of the maven-jetty-plugin configuration:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.16</version>
<configuration>
<systemProperties>
<systemProperty>
<name>tapestry.execution-mode</name>
<value>development</value>
</systemProperty>
<systemProperty>
<name>jetty.port</name>
<value>9090</value>
</systemProperty>
</systemProperties>
</configuration>
</plugin>
In both examples Jetty is configured to listen to port 9090.

Resources