Skip to content

Commit

Permalink
updated log levels
Browse files Browse the repository at this point in the history
  • Loading branch information
anandbagmar committed Nov 5, 2024
1 parent 1584647 commit 7c11730
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 52 deletions.
66 changes: 51 additions & 15 deletions src/main/java/com/znsio/teswiz/aspect/AspectJMethodLoggers.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,69 @@
package com.znsio.teswiz.aspect;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.JoinPoint;

import java.lang.reflect.Array;
import java.util.stream.IntStream;

import static org.apache.logging.log4j.Level.*;

public class AspectJMethodLoggers {
private static final Logger LOGGER = LogManager.getLogger(AspectJMethodLoggers.class.getName());

private AspectJMethodLoggers() {
}

public static void beforeAnyMethod(JoinPoint joinPoint) {
LOGGER.info(String.format("\t<<<%s>>>",
generateBeforeMethodAspectJLogger(joinPoint.getSignature().getDeclaringType().getSimpleName(),
joinPoint.getSignature().getName(),joinPoint.getSourceLocation().getLine(),
joinPoint.getArgs())));
public static void beforeAnyMethod(JoinPoint joinPoint, Level level) {
try {
String className = joinPoint.getSignature().getDeclaringType().getSimpleName();
String methodName = joinPoint.getSignature().getName();
Integer lineNumber = (joinPoint.getSourceLocation() != null) ? joinPoint.getSourceLocation().getLine() : -1;
Object[] methodArgs = joinPoint.getArgs();

String message = String.format("\t<<<%s>>>",
generateBeforeMethodAspectJLogger(className, methodName, lineNumber, methodArgs));

logAtLevel(level, message);
} catch (Exception e) {
LOGGER.warn("Failed to log before method: " + e.getMessage());
}
}

private static void logAtLevel(Level level, String message) {
if (level.equals(DEBUG)) {
LOGGER.debug(message);
} else if (level.equals(TRACE)) {
LOGGER.trace(message);
} else if (level.equals(WARN)) {
LOGGER.warn(message);
} else if (level.equals(ERROR)) {
LOGGER.error(message);
} else if (level.equals(FATAL)) {
LOGGER.fatal(message);
} else {
LOGGER.info(message); // Fallback to INFO for unhandled levels
}
}

public static void afterAnyMethod(JoinPoint joinPoint) {
LOGGER.debug(String.format("\t<<<%s>>>",
generateAfterMethodAspectJLogger(joinPoint.getSignature().getDeclaringType().getSimpleName(),
joinPoint.getSignature().getName())));
public static void afterAnyMethod(JoinPoint joinPoint, Level level) {
try {
String className = joinPoint.getSignature().getDeclaringType().getSimpleName();
String methodName = joinPoint.getSignature().getName();

String message = String.format("\t<<<%s>>>", generateAfterMethodAspectJLogger(className, methodName));

logAtLevel(level, message);
} catch (Exception e) {
LOGGER.warn("Failed to log after method: " + e.getMessage());
}
}

public static String generateBeforeMethodAspectJLogger(String simpleClassName,
String methodName,
int lineNumber, Object[] arguments) {
String methodName,
int lineNumber, Object[] arguments) {
StringBuilder loggerMessage = new StringBuilder();
loggerMessage.append(String.format("Entering method: '%s.%s:%d'%n", simpleClassName, methodName, lineNumber));

Expand All @@ -44,23 +79,24 @@ private static void addParameterInfoToLoggerMessage(Object[] arguments, StringBu
loggerMessage.append(String.format("Value: \"null\"%n"));
} else if (argument.getClass().isArray()) {
addArrayParameteInfoToLoggerMessage(loggerMessage, argument);
} else
} else {
loggerMessage.append(String.format("Type: '%s', Value: \"%s\"%n", argument.getClass().getSimpleName(), argument));
}
}
}

private static void addArrayParameteInfoToLoggerMessage(StringBuilder loggerMessage,
Object argument) {
Object argument) {
StringBuilder arrayMessage = new StringBuilder();
arrayMessage.append("[");
IntStream.range(0, Array.getLength(argument)).mapToObj(arrayIndex -> String.format("%s, ", Array.get(argument, arrayIndex))).forEach(arrayMessage::append);
arrayMessage.replace(arrayMessage.length() - 2, arrayMessage.length(), "]");
loggerMessage.append(String.format("Type: '%s', Value: \"%s\"%n",
argument.getClass().getSimpleName(), arrayMessage));
argument.getClass().getSimpleName(), arrayMessage));
}

public static String generateAfterMethodAspectJLogger(String simpleClassName,
String methodName) {
String methodName) {
return String.format("Exiting method: '%s'", simpleClassName + "." + methodName);
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/znsio/teswiz/aspect/AspectLogging.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.znsio.teswiz.aspect;

import org.apache.logging.log4j.Level;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
Expand All @@ -18,12 +19,12 @@ public void executionScope(){

@Before("executionScope()")
public void beforeAnyMethod(JoinPoint joinPoint) {
AspectJMethodLoggers.beforeAnyMethod(joinPoint);
AspectJMethodLoggers.beforeAnyMethod(joinPoint, Level.DEBUG);
}

@After("executionScope()")
public void afterAnyMethod(JoinPoint joinPoint) {
AspectJMethodLoggers.afterAnyMethod(joinPoint);
AspectJMethodLoggers.afterAnyMethod(joinPoint, Level.DEBUG);
}

}
16 changes: 7 additions & 9 deletions src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static Driver createWebDriverForUser(String userPersona, String browserName,
LOGGER.info(String.format(
"createWebDriverForUser: begin: userPersona: '%s', browserName: '%s', Platform: " + "'%s', Number of WebDrivers: '%d'%n",
userPersona, browserName, forPlatform.name(), numberOfWebDriversUsed));
LOGGER.info("Active thread count: " + Thread.activeCount());
LOGGER.debug("Active thread count: " + Thread.activeCount());

String baseUrl = getBaseUrl(userPersona);
String appName = Drivers.getAppNamefor(userPersona);
Expand Down Expand Up @@ -113,7 +113,7 @@ private static JSONObject getBrowserConfig(TestExecutionContext context) {
String updatedBrowserConfigFileForThisTest = context.getTestStateAsString(TEST_CONTEXT.UPDATED_BROWSER_CONFIG_FILE_FOR_THIS_TEST);
if (null != updatedBrowserConfigFileForThisTest) {
browserConfigFile = updatedBrowserConfigFileForThisTest;
LOGGER.info("Using UPDATED_BROWSER_CONFIG_FILE_FOR_THIS_TEST (instead of default BROWSER_CONFIG_FILE): " + browserConfigFile);
LOGGER.debug("Using UPDATED_BROWSER_CONFIG_FILE_FOR_THIS_TEST (instead of default BROWSER_CONFIG_FILE): " + browserConfigFile);
}
LOGGER.info("Using BROWSER_CONFIG_FILE: " + browserConfigFile);
JSONObject browserConfig = Runner.getBrowserConfigFileContents(browserConfigFile);
Expand Down Expand Up @@ -155,7 +155,6 @@ private static WebDriver createNewWebDriver(String forUserPersona, String browse
JSONObject browserConfig = getBrowserConfig(testExecutionContext);
LOGGER.info(String.format("Create new webdriver instance for: %s, on: %s, with browserConfig: %s", forUserPersona, browserName, browserConfig));

LOGGER.info(BrowserDriverManager.class.getName() + "-createNewWebDriver: " + browserName.toLowerCase());
JSONObject browserConfigForBrowserType = browserConfig.getJSONObject(browserName.toLowerCase());
WebDriver driver = createWebDriver(forUserPersona, testExecutionContext, browserName, browserConfigForBrowserType);

Expand Down Expand Up @@ -203,7 +202,7 @@ private static WebDriver createChromeDriver(String forUserPersona,
Runner.isRunningInCI() ? ((RemoteWebDriver) driver).getCapabilities()
: ((ChromeDriver) driver).getCapabilities();
Drivers.addUserPersonaDriverCapabilities(forUserPersona, capabilities);
LOGGER.info("Chrome driver capabilities extracted for further use");
LOGGER.debug("Chrome driver capabilities extracted for further use");
manageWindowSizeAndHeadlessMode(driver);
return driver;
}
Expand Down Expand Up @@ -251,7 +250,7 @@ private static WebDriver createFirefoxDriver(String forUserPersona,
Runner.isRunningInCI() ? ((RemoteWebDriver) driver).getCapabilities()
: ((FirefoxDriver) driver).getCapabilities();
Drivers.addUserPersonaDriverCapabilities(forUserPersona, capabilities);
LOGGER.info("Firefox driver capabilities extracted for further use");
LOGGER.debug("Firefox driver capabilities extracted for further use");
manageWindowSizeAndHeadlessMode(driver);
return driver;
}
Expand Down Expand Up @@ -431,7 +430,7 @@ private static WebDriver createSafariDriver(String forUserPersona,
Runner.isRunningInCI() ? ((RemoteWebDriver) driver).getCapabilities()
: ((SafariDriver) driver).getCapabilities();
Drivers.addUserPersonaDriverCapabilities(forUserPersona, capabilities);
LOGGER.info("Safari driver capabilities extracted for further use");
LOGGER.debug("Safari driver capabilities extracted for further use");
// webpush notifications are disabled bydefault in safari , headless is not supported by
// safari browser and user profiles cannot be set in safari
manageWindowSizeAndHeadlessMode(driver);
Expand Down Expand Up @@ -568,7 +567,7 @@ static Driver createElectronDriverForUser(String userPersona, String browserName
LOGGER.info(String.format(
"createElectronDriverForUser: begin: userPersona: '%s', browserName: '%s', Platform: " + "'%s', Number of ElectronDrivers: '%d'%n",
userPersona, browserName, forPlatform.name(), numberOfWebDriversUsed));
LOGGER.info("Active thread count: " + Thread.activeCount());
LOGGER.debug("Active thread count: " + Thread.activeCount());

String baseUrl = getBaseUrl(userPersona);
String appName = Drivers.getAppNamefor(userPersona);
Expand All @@ -580,7 +579,6 @@ static Driver createElectronDriverForUser(String userPersona, String browserName
context.addTestState(TEST_CONTEXT.ELECTRON_BROWSER_ON, runningOn);
JSONObject browserConfig = getBrowserConfig(context);
LOGGER.info(String.format("Create new electrondriver instance for: %s, on: %s, with browserConfig: %s", userPersona, browserName, browserConfig));
LOGGER.info(BrowserDriverManager.class.getName() + "-createNewElectronDriver: " + browserName.toLowerCase());
JSONObject browserConfigForBrowserType = browserConfig.getJSONObject(browserName.toLowerCase());
ChromeOptions chromeOptions = getChromeOptions(userPersona, context, browserConfigForBrowserType);
addWindowSizeToChromeOptions(browserConfigForBrowserType, chromeOptions);
Expand All @@ -605,7 +603,7 @@ static Driver createElectronDriverForUser(String userPersona, String browserName
Runner.isRunningInCI() ? ((RemoteWebDriver) driver).getCapabilities()
: ((ChromeDriver) driver).getCapabilities();
Drivers.addUserPersonaDriverCapabilities(userPersona, capabilities);
LOGGER.info("Electron driver capabilities extracted for further use");
LOGGER.debug("Electron driver capabilities extracted for further use");

if (browserConfigForBrowserType.getBoolean("electronAppLoadingPage")) {
handleWindowForElectronApplication(driver, browserConfigForBrowserType);
Expand Down
33 changes: 16 additions & 17 deletions src/main/java/com/znsio/teswiz/runner/Visual.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class Visual {
public Visual(String driverType, Platform platform, WebDriver innerDriver, String testName,
String userPersona, String appName) {
boolean isVisualTestingEnabled = Runner.isVisualTestingEnabled();
LOGGER.info(format(
LOGGER.debug(format(
"Visual constructor: Driver type: %s, platform: %s, testName: %s, " +
"isVisualTestingEnabled: %s",
driverType, platform.name(), testName, isVisualTestingEnabled));
Expand Down Expand Up @@ -116,7 +116,7 @@ private com.applitools.eyes.appium.Eyes instantiateAppiumEyes(String driverType,
if(driverType.equals(Driver.WEB_DRIVER)) {
isVisualTestingEnabled = false;
}
LOGGER.info(format("instantiateAppiumEyes: isVisualTestingEnabled: %s",
LOGGER.debug(format("instantiateAppiumEyes: isVisualTestingEnabled: %s",
isVisualTestingEnabled));
com.applitools.eyes.appium.Eyes appEyes = new com.applitools.eyes.appium.Eyes();

Expand All @@ -127,7 +127,7 @@ private com.applitools.eyes.appium.Eyes instantiateAppiumEyes(String driverType,
try {
setProxyForAppExecution(appEyes);
appEyes.open(innerDriver, appName, testName);
LOGGER.info(format("instantiateAppiumEyes: Is Applitools Visual Testing enabled? - %s", !appEyes.getIsDisabled()));
LOGGER.debug(format("instantiateAppiumEyes: Is Applitools Visual Testing enabled? - %s", !appEyes.getIsDisabled()));
} catch(IllegalArgumentException e) {
String message = format(
"Exception in instantiating Applitools for App: '%s', Closing driver instance",
Expand All @@ -146,7 +146,7 @@ private void setProxyForAppExecution(com.applitools.eyes.appium.Eyes appEyes) {
LOGGER.info(format("Set proxyUrl for appEyes: %s", proxyUrl));
appEyes.setProxy(new ProxySettings(proxyUrl));
} else {
LOGGER.info("proxyUrl is null. No proxy set for appEyes");
LOGGER.debug("proxyUrl is null. No proxy set for appEyes");
}
}

Expand Down Expand Up @@ -192,7 +192,7 @@ private com.applitools.eyes.selenium.Eyes instantiateWebEyes(String driverType,
if(driverType.equals(Driver.APPIUM_DRIVER)) {
isVisualTestingEnabled = false;
}
LOGGER.info(format("instantiateWebEyes: isVisualTestingEnabled: %s",
LOGGER.debug(format("instantiateWebEyes: isVisualTestingEnabled: %s",
isVisualTestingEnabled));
boolean isUFG = getValueFromConfig(APPLITOOLS.USE_UFG, false);

Expand All @@ -210,13 +210,12 @@ private com.applitools.eyes.selenium.Eyes instantiateWebEyes(String driverType,
addCustomPropertiesInWebTestExecution(platform, webEyes);

RectangleSize setBrowserViewPortSize = getBrowserViewPortSize(driverType, innerDriver);
LOGGER.info(format("Using browser dimensions for Applitools: %s",
setBrowserViewPortSize));
LOGGER.info(format("Using browser dimensions for Applitools: %s", setBrowserViewPortSize));

try {
setProxyForWebExecution(webEyes);
webEyes.open(innerDriver, appName, testName, setBrowserViewPortSize);
LOGGER.info(format("instantiateWebEyes: Is Applitools Visual Testing enabled? - %s", !webEyes.getIsDisabled()));
LOGGER.debug(format("instantiateWebEyes: Is Applitools Visual Testing enabled? - %s", !webEyes.getIsDisabled()));
} catch(IllegalArgumentException | EyesException e) {
String message = format(
"Exception in instantiating Applitools for Web: '%s', Closing Web-driver " +
Expand All @@ -242,7 +241,7 @@ private void setProxyForWebExecution(Eyes webEyes) {
LOGGER.info(format("Set proxyUrl for webEyes: %s", proxyUrl));
webEyes.setProxy(new ProxySettings(proxyUrl));
} else {
LOGGER.info("proxyUrl is null. No proxy set for webEyes");
LOGGER.debug("proxyUrl is null. No proxy set for webEyes");
}
}

Expand Down Expand Up @@ -394,9 +393,9 @@ public Visual checkWindow(String fromScreen, String tag) {
String formattedTagName = getFormattedTagName(fromScreen, tag);
LOGGER.info(format("checkWindow: fromScreen: %s, tag: %s", fromScreen,
formattedTagName));
LOGGER.info(format("checkWindow: eyesOnWeb.getIsDisabled(): %s",
LOGGER.debug(format("checkWindow: eyesOnWeb.getIsDisabled(): %s",
eyesOnWeb.getIsDisabled()));
LOGGER.info(format("checkWindow: eyesOnApp.getIsDisabled(): %s",
LOGGER.debug(format("checkWindow: eyesOnApp.getIsDisabled(): %s",
eyesOnApp.getIsDisabled()));

LocalDateTime webStart = LocalDateTime.now();
Expand Down Expand Up @@ -430,9 +429,9 @@ private String getFormattedTagName(String fromScreen, String tag) {
public Visual check(String fromScreen, String tag, SeleniumCheckSettings checkSettings) {
String formattedTagName = getFormattedTagName(fromScreen, tag);
LOGGER.info(format("check: fromScreen: %s, tag: %s", fromScreen, formattedTagName));
LOGGER.info(
LOGGER.debug(
format("check: eyesOnWeb.getIsDisabled(): %s", eyesOnWeb.getIsDisabled()));
LOGGER.info(
LOGGER.debug(
format("check: eyesOnApp.getIsDisabled(): %s", eyesOnApp.getIsDisabled()));

LocalDateTime webStart = LocalDateTime.now();
Expand Down Expand Up @@ -460,9 +459,9 @@ public Visual check(String fromScreen, String tag, SeleniumCheckSettings checkSe
public Visual check(String fromScreen, String tag, AppiumCheckSettings checkSettings) {
String formattedTagName = getFormattedTagName(fromScreen, tag);
LOGGER.info(format("check: fromScreen: %s, tag: %s", fromScreen, formattedTagName));
LOGGER.info(
LOGGER.debug(
format("check: eyesOnWeb.getIsDisabled(): %s", eyesOnWeb.getIsDisabled()));
LOGGER.info(
LOGGER.debug(
format("check: eyesOnApp.getIsDisabled(): %s", eyesOnApp.getIsDisabled()));

LocalDateTime webStart = LocalDateTime.now();
Expand Down Expand Up @@ -492,9 +491,9 @@ public Visual checkWindow(String fromScreen, String tag, MatchLevel level) {
LOGGER.info(
format("checkWindow: fromScreen: %s, MatchLevel: %s, tag: %s", fromScreen,
level, formattedTagName));
LOGGER.info(format("checkWindow: eyesOnWeb.getIsDisabled(): %s",
LOGGER.debug(format("checkWindow: eyesOnWeb.getIsDisabled(): %s",
eyesOnWeb.getIsDisabled()));
LOGGER.info(format("checkWindow: eyesOnApp.getIsDisabled(): %s",
LOGGER.debug(format("checkWindow: eyesOnApp.getIsDisabled(): %s",
eyesOnApp.getIsDisabled()));


Expand Down
Loading

0 comments on commit 7c11730

Please sign in to comment.