diff --git a/mrchecker-playwright-framework/pom.xml b/mrchecker-playwright-framework/pom.xml index 757bd7fb..f0874890 100644 --- a/mrchecker-playwright-framework/pom.xml +++ b/mrchecker-playwright-framework/pom.xml @@ -6,7 +6,7 @@ com.capgemini.mrchecker playwright - 2024.03.19 + 2024.04.18 Capgemini - Nearshore Test Center - Poland @@ -47,9 +47,9 @@ 2.25.0 ${project.basedir}/target - 2.0.6 - 1.2.17 - 2.17.1 + + 2.20.0 + 1.7.30 1.1.1 2.1.0 1.9.3 @@ -86,6 +86,13 @@ allure-maven ${allure-maven.version} + + + org.slf4j + slf4j-simple + ${slf4j.version} + io.qameta.allure @@ -120,33 +127,16 @@ - - - org.slf4j - slf4j-simple - ${slf4j.version} - test - - - - - org.slf4j - slf4j-api - ${slf4j.version} - - - + - log4j - log4j + org.apache.logging.log4j + log4j-api ${log4j.version} - - org.apache.logging.log4j log4j-core - ${log4j-core.version} + ${log4j.version} diff --git a/mrchecker-playwright-framework/src/main/java/com/capgemini/framework/logger/LoggerConfiguration.java b/mrchecker-playwright-framework/src/main/java/com/capgemini/framework/logger/LoggerConfiguration.java new file mode 100644 index 00000000..864170d3 --- /dev/null +++ b/mrchecker-playwright-framework/src/main/java/com/capgemini/framework/logger/LoggerConfiguration.java @@ -0,0 +1,11 @@ +package com.capgemini.framework.logger; + +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.ConfigurationSource; +import org.apache.logging.log4j.core.config.xml.XmlConfiguration; + +public class LoggerConfiguration extends XmlConfiguration { + public LoggerConfiguration(LoggerContext context, ConfigurationSource configSource) { + super(context, configSource); + } +} diff --git a/mrchecker-playwright-framework/src/main/java/com/capgemini/framework/logger/LoggerConfigurationFactory.java b/mrchecker-playwright-framework/src/main/java/com/capgemini/framework/logger/LoggerConfigurationFactory.java new file mode 100644 index 00000000..c144b949 --- /dev/null +++ b/mrchecker-playwright-framework/src/main/java/com/capgemini/framework/logger/LoggerConfigurationFactory.java @@ -0,0 +1,28 @@ +package com.capgemini.framework.logger; + +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.ConfigurationFactory; +import org.apache.logging.log4j.core.config.ConfigurationSource; +import org.apache.logging.log4j.core.config.Order; +import org.apache.logging.log4j.core.config.plugins.Plugin; + +/** + * https://logging.apache.org/log4j/log4j-2.4/manual/customconfig.html Combining Configuration File with Programmatic + * Configuration + */ +@Plugin(name = "LoggerConfigurationFactory", category = "ConfigurationFactory") +@Order(10) +public class LoggerConfigurationFactory extends ConfigurationFactory { + public static final String[] SUFFIXES = new String[]{".xml", "*"}; + + @Override + public String[] getSupportedTypes() { + return SUFFIXES; + } + + @Override + public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource source) { + return new LoggerConfiguration(loggerContext, source); + } +} diff --git a/mrchecker-playwright-framework/src/main/java/com/capgemini/framework/logger/LoggerEnvironmentLevel.java b/mrchecker-playwright-framework/src/main/java/com/capgemini/framework/logger/LoggerEnvironmentLevel.java deleted file mode 100644 index 1bd91f5e..00000000 --- a/mrchecker-playwright-framework/src/main/java/com/capgemini/framework/logger/LoggerEnvironmentLevel.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.capgemini.framework.logger; - -import org.apache.log4j.Level; - -public class LoggerEnvironmentLevel extends Level { - public static final int ENVIRONMENT_INT = 40010; - public static final Level ENVIRONMENT = new LoggerEnvironmentLevel(40010, "ENVIRONMENT", 10); - - protected LoggerEnvironmentLevel(int arg0, String arg1, int arg2) { - super(arg0, arg1, arg2); - } - - public static Level toLevel(String logArgument) { - return toLevel(logArgument, Level.DEBUG); - } - - public static Level toLevel(String logArgument, Level defaultLevel) { - return logArgument != null && logArgument.equalsIgnoreCase("ENVIRONMENT") ? ENVIRONMENT : Level.toLevel(logArgument, defaultLevel); - } - - public static Level toLevel(int val) { - return toLevel(val, Level.DEBUG); - } - - public static Level toLevel(int val, Level defaultLevel) { - return val == 40010 ? ENVIRONMENT : Level.toLevel(val, defaultLevel); - } -} diff --git a/mrchecker-playwright-framework/src/main/java/com/capgemini/framework/logger/LoggerInstance.java b/mrchecker-playwright-framework/src/main/java/com/capgemini/framework/logger/LoggerInstance.java index d507bfff..3c69bd83 100644 --- a/mrchecker-playwright-framework/src/main/java/com/capgemini/framework/logger/LoggerInstance.java +++ b/mrchecker-playwright-framework/src/main/java/com/capgemini/framework/logger/LoggerInstance.java @@ -2,190 +2,100 @@ import com.google.common.base.Charsets; import com.google.common.io.Files; -import org.apache.log4j.*; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.ThreadContext; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; -import java.util.Arrays; +import java.util.Objects; public class LoggerInstance { - private static final String FBEGIN = "Function: "; - private static final String FEND = "END"; - private static final String logPattern = "%d{yyyy-MM-dd 'at' HH:mm:ss z} %M - %m%x%n"; - private static final Level loggerLevel; - private File logFile; - private String appenderName; - private org.apache.log4j.Logger logger; - private int logLevel = 0; - - protected LoggerInstance() { - } - - private AsyncAppender wrapAsync(Appender appender) { - var wrapper = new AsyncAppender(); - wrapper.addAppender(appender); - return wrapper; - } - - private org.apache.log4j.Logger getLogger() { - if (this.logger == null) { - this.logger = org.apache.log4j.Logger.getLogger(this.getAppenderName()); - this.logger.removeAllAppenders(); - this.logger.setLevel(loggerLevel); - this.logger.addAppender(this.createEnvFileAppender()); - this.logger.addAppender(this.createFileAppender()); - this.logger.addAppender(this.wrapAsync(this.createConsoleAppender())); - } - - return this.logger; - } - - private FileAppender createEnvFileAppender() { - var patternLayout = new PatternLayout("%d{yyyy-MM-dd 'at' HH:mm:ss z} %M - %m%x%n"); - - try { - var envLogName = this.getLogFile() - .getPath() - .replace(".log", "_env.log"); - var appender = new FileAppender(patternLayout, envLogName, true); - appender.setName("EnvRollingFile"); - appender.setThreshold(LoggerEnvironmentLevel.ENVIRONMENT); - appender.activateOptions(); - return appender; - } catch (Exception var4) { - System.out.println("Unable to create appender: " + var4); - return null; - } - } - - private FileAppender createFileAppender() { - var patternLayout = new PatternLayout("%d{yyyy-MM-dd 'at' HH:mm:ss z} %M - %m%x%n"); - - try { - var appender = new FileAppender(patternLayout, this.getLogFile() - .getPath(), true); - appender.setName(this.getAppenderName()); - appender.setThreshold(Level.DEBUG); - appender.activateOptions(); - return appender; - } catch (Exception var4) { - System.out.println("Unable to create appender: " + var4); - return null; - } + private org.apache.logging.log4j.Logger logger; + + private static final String FBEGIN = "Function: "; + private static final String FEND = "END"; + + public LoggerInstance() { + + ThreadContext.put("threadName", Thread.currentThread() + .getName()); + getLogger(); } - - private ConsoleAppender createConsoleAppender() { - var patternLayout = new PatternLayout("%d{yyyy-MM-dd 'at' HH:mm:ss z} %M - %m%x%n"); - - try { - var appender = new ConsoleAppender(patternLayout); - appender.setTarget("System.out"); - appender.setName("Console"); - appender.setThreshold(Level.DEBUG); - appender.activateOptions(); - return appender; - } catch (Exception var4) { - System.out.println("Unable to create appender: " + var4); - return null; + + private org.apache.logging.log4j.Logger getLogger() { + if (Objects.isNull(null)) { + logger = LogManager.getRootLogger(); } + return logger; } - - private File getLogFile() { - if (this.logFile == null) { - this.logFile = new File(this.getDirectory() - .getPath(), this.getAppenderName() + ".log"); - this.logFile.deleteOnExit(); - } - - return this.logFile; - } - - private File getDirectory() { - var directory = new File("./target/logs"); - if (!directory.exists()) { - directory.mkdir(); - } - - return directory; + + public void log(Level level, String message) { + logger.log(level, message); } - - private String getAppenderName() { - if (this.appenderName == null) { - this.appenderName = Thread.currentThread() - .getName(); - } - - return this.appenderName; + + public void logDebug(String message) { + log(Level.DEBUG, message); } - + public void logInfo(String message) { - this.getLogger() - .info(this.formatMessage(message)); + log(Level.INFO, message); } - - public void logEnv(String message) { - this.getLogger() - .log(LoggerEnvironmentLevel.ENVIRONMENT, message); - if (message.equals("END")) { - --this.logLevel; - } - + + public void logWarning(String message) { + log(Level.WARN, message); } - - public void logDebug(String message) { - var indent = new char[this.logLevel]; - Arrays.fill(indent, ' '); - this.getLogger() - .debug(this.formatMessage(new String(indent) + message)); + + public void logError(String message) { + log(Level.ERROR, message); } - + public void logFunctionBegin(String functionName) { - this.logDebug("Function: " + functionName); - ++this.logLevel; + log(Level.DEBUG, FBEGIN + functionName); } - + public void logFunctionEnd() { - --this.logLevel; - this.logDebug("END"); + log(Level.DEBUG, FEND); } - - public void logError(String message) { - this.getLogger() - .error(this.formatMessage(message)); - if (message.equals("END")) { - --this.logLevel; - } - + + public void logEnv(String message) { + log(Level.forName("ENV", 700), message); + } + + public void logAnalytics(String message) { + log(Level.forName("ANALYTICS", 800), message); + } + + private String getAppenderName() { + return ThreadContext.get("threadName"); } - - private String formatMessage(String message) { - return "[" + this.getAppenderName() + "] " + message; + + protected File getDirectory() { + return new File("./logs"); } - - protected void startSeparateLog() { + + private File getLogFile() { + return new File(getDirectory().getPath(), getAppenderName() + ".log"); + } + + public void startSeparateLog() { try { - var pw = new PrintWriter(this.getLogFile()); + PrintWriter pw = new PrintWriter(getLogFile()); pw.close(); - } catch (FileNotFoundException var2) { - var2.printStackTrace(); + } catch (FileNotFoundException e) { + e.printStackTrace(); } - } - - protected String dumpSeparateLog() { + + @SuppressWarnings("deprecation") + public String dumpSeparateLog() { try { - return Files.toString(this.getLogFile(), Charsets.UTF_8); - } catch (IOException var2) { - var2.printStackTrace(); + return Files.toString(getLogFile(), Charsets.UTF_8); + } catch (IOException e) { + e.printStackTrace(); return ""; } } - - static { - org.apache.log4j.Logger.getRootLogger() - .removeAppender("console"); - loggerLevel = Level.DEBUG; - } } diff --git a/mrchecker-playwright-framework/src/main/java/com/capgemini/framework/logger/LoggerLogLevelConversionChar.java b/mrchecker-playwright-framework/src/main/java/com/capgemini/framework/logger/LoggerLogLevelConversionChar.java new file mode 100644 index 00000000..7ecb246a --- /dev/null +++ b/mrchecker-playwright-framework/src/main/java/com/capgemini/framework/logger/LoggerLogLevelConversionChar.java @@ -0,0 +1,29 @@ +package com.capgemini.framework.logger; + +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.pattern.ConverterKeys; +import org.apache.logging.log4j.core.pattern.LogEventPatternConverter; +import org.apache.logging.log4j.core.pattern.PatternConverter; + +@Plugin(name = "LogLevelConversionChar", category = PatternConverter.CATEGORY) +@ConverterKeys({"LL", "LoggerLevel"}) +public class LoggerLogLevelConversionChar extends LogEventPatternConverter { + private LoggerLogLevelConversionChar(final String name, final String key) { + super(name, key); + } + + public static LoggerLogLevelConversionChar newInstance(String[] options) { + return new LoggerLogLevelConversionChar("router", "LL"); + } + + @Override + public void format(LogEvent logEvent, StringBuilder stringBuilder) { + String logLevel = logEvent.getLevel() + .name() + .toLowerCase(); + String formattedLogLevel = "log" + logLevel.substring(0, 1) + .toUpperCase() + logLevel.substring(1); + stringBuilder.append(formattedLogLevel); + } +} diff --git a/mrchecker-playwright-framework/src/test/resources/log4j2.xml b/mrchecker-playwright-framework/src/test/resources/log4j2.xml new file mode 100644 index 00000000..02aee316 --- /dev/null +++ b/mrchecker-playwright-framework/src/test/resources/log4j2.xml @@ -0,0 +1,53 @@ + + + + + + + + + %d{yyyy-MM-dd 'at' HH:mm:ss z} %LL - %m%n + + + + + + + + + + + + %d{yyyy-MM-dd 'at' HH:mm:ss z} %LL - %m%n + + + + + + + + + + %d{yyyy-MM-dd 'at' HH:mm:ss z} [%t] %highlight{%LL}{FATAL=red, ERROR=red, WARN=yellow, + INFO=green, DEBUG=blue, TRACE=black} - %m%n + + + + + + + + + + + + + + + + + + + \ No newline at end of file