From 7c117306941f8fee9ebfbd78389dc898267c1c9d Mon Sep 17 00:00:00 2001 From: Anand Bagmar Date: Tue, 5 Nov 2024 16:30:09 +0530 Subject: [PATCH] updated log levels --- .../teswiz/aspect/AspectJMethodLoggers.java | 66 ++++++++++++++----- .../znsio/teswiz/aspect/AspectLogging.java | 5 +- .../teswiz/runner/BrowserDriverManager.java | 16 ++--- .../java/com/znsio/teswiz/runner/Visual.java | 33 +++++----- .../znsio/teswiz/aspect/AspectLogging.java | 23 ++++--- 5 files changed, 91 insertions(+), 52 deletions(-) diff --git a/src/main/java/com/znsio/teswiz/aspect/AspectJMethodLoggers.java b/src/main/java/com/znsio/teswiz/aspect/AspectJMethodLoggers.java index dd73807b4..7de36875f 100644 --- a/src/main/java/com/znsio/teswiz/aspect/AspectJMethodLoggers.java +++ b/src/main/java/com/znsio/teswiz/aspect/AspectJMethodLoggers.java @@ -1,5 +1,6 @@ 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; @@ -7,28 +8,62 @@ 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)); @@ -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); } } diff --git a/src/main/java/com/znsio/teswiz/aspect/AspectLogging.java b/src/main/java/com/znsio/teswiz/aspect/AspectLogging.java index 0fd29ba03..c1676ab26 100644 --- a/src/main/java/com/znsio/teswiz/aspect/AspectLogging.java +++ b/src/main/java/com/znsio/teswiz/aspect/AspectLogging.java @@ -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; @@ -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); } } diff --git a/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java b/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java index ea7c9e3e6..f861dd095 100644 --- a/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java +++ b/src/main/java/com/znsio/teswiz/runner/BrowserDriverManager.java @@ -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); @@ -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); @@ -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); @@ -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; } @@ -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; } @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/src/main/java/com/znsio/teswiz/runner/Visual.java b/src/main/java/com/znsio/teswiz/runner/Visual.java index ad02f8846..c0586cc4b 100644 --- a/src/main/java/com/znsio/teswiz/runner/Visual.java +++ b/src/main/java/com/znsio/teswiz/runner/Visual.java @@ -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)); @@ -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(); @@ -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", @@ -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"); } } @@ -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); @@ -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 " + @@ -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"); } } @@ -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(); @@ -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(); @@ -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(); @@ -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())); diff --git a/src/test/java/com/znsio/teswiz/aspect/AspectLogging.java b/src/test/java/com/znsio/teswiz/aspect/AspectLogging.java index fe33799ab..6760b0abd 100644 --- a/src/test/java/com/znsio/teswiz/aspect/AspectLogging.java +++ b/src/test/java/com/znsio/teswiz/aspect/AspectLogging.java @@ -2,10 +2,14 @@ import com.context.SessionContext; import com.context.TestExecutionContext; -import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.aspectj.lang.JoinPoint; -import org.aspectj.lang.annotation.*; +import org.aspectj.lang.annotation.After; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Pointcut; @Aspect public class AspectLogging { @@ -16,22 +20,23 @@ public AspectLogging() { long threadId = Thread.currentThread().getId(); context = SessionContext.getTestExecutionContext(threadId); } + @Pointcut("execution(public * *.*.*.businessLayer.*.*.*(..))" + - "|| execution(public * *.*.*.screen.*.*.*.*(..))" + - "|| execution(public * *.*.*.entities.*.*(..))" + - "|| execution(public * *.*.*.runner.*.*(..))" + - "|| execution(public * *.*.*.steps.*Steps.*(..))") - public void executionScope(){ + "|| execution(public * *.*.*.screen.*.*.*.*(..))" + + "|| execution(public * *.*.*.entities.*.*(..))" + + "|| execution(public * *.*.*.runner.*.*(..))" + + "|| execution(public * *.*.*.steps.*Steps.*(..))") + public void executionScope() { } @Before("executionScope()") public void beforeAnyMethod(JoinPoint joinPoint) { - AspectJMethodLoggers.beforeAnyMethod(joinPoint); + AspectJMethodLoggers.beforeAnyMethod(joinPoint, Level.INFO); } @After("executionScope()") public void afterAnyMethod(JoinPoint joinPoint) { - AspectJMethodLoggers.afterAnyMethod(joinPoint); + AspectJMethodLoggers.afterAnyMethod(joinPoint, Level.INFO); } }