Skip to content

Commit

Permalink
Release 1.1 version
Browse files Browse the repository at this point in the history
  • Loading branch information
Amerousful committed May 20, 2020
1 parent c0656a6 commit 1a87b08
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 42 deletions.
4 changes: 4 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Changelog 1.1 (20-05-2020)
----------------------------
* Added single_pattern in properties
* Added support custom pattern for initialization before tests. log("[%p] %d{HH:mm:ss} custom - %m%n");
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.bairov</groupId>
<groupId>io.github.amerousful</groupId>
<artifactId>parallel-test-logger</artifactId>
<version>1.0</version>
<version>1.1</version>

<properties>
<jdk.source.version>1.8</jdk.source.version>
Expand Down
35 changes: 2 additions & 33 deletions src/main/java/listeners/ParallelListener.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,21 @@
package listeners;


import io.qameta.allure.Attachment;
import logger.LoggerFactory;
import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestListener;
import org.testng.ITestResult;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Logger;

import static logger.Props.properties;

public class ParallelListener implements IInvokedMethodListener, ITestListener {

public static final Logger LOGGER = Logger.getLogger(ParallelListener.class.getName());

private final static Properties properties;

static {
properties = loadProperties();
}

public static Properties loadProperties() {
String propertiesFileName = "parallel_log.properties";

Properties mainProperties = new Properties();
try (InputStream input = LoggerFactory.class.getClassLoader().getResourceAsStream(propertiesFileName)) {

if (input != null) {
mainProperties.load(input);
} else {
LOGGER.info("Sorry, unable to find " + propertiesFileName);
}

} catch (IOException e) {
e.printStackTrace();
}
return mainProperties;
}

@Override
public void beforeInvocation(IInvokedMethod method, ITestResult result) {
boolean isParallel = result.getTestContext().getCurrentXmlTest().getParallel().isParallel();
if (isParallel) {
LoggerFactory.init(method.getTestMethod().getMethodName(), properties);
LoggerFactory.init(method.getTestMethod().getMethodName());
}
}

Expand Down
27 changes: 20 additions & 7 deletions src/main/java/logger/LoggerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import org.apache.log4j.*;

import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static logger.Props.properties;
import static org.apache.log4j.Logger.getLogger;

public class LoggerFactory {
Expand All @@ -16,7 +16,7 @@ public class LoggerFactory {
private static final String CONSOLE_LAYOUT = "[%p] %d{HH:mm:ss} [%l]- %m%n";
private static final String PARALLEL_LAYOUT = "t:%X{threadId} [%p] %d{HH:mm:ss} [%l] - %m%n";

public static Logger init(String testName, Properties properties) {
public static Logger init(String testName) {
Long threadId = Thread.currentThread().getId();

MDC.put("threadId", threadId);
Expand All @@ -37,30 +37,43 @@ public static Logger init(String testName, Properties properties) {
return logger;
}

public static Logger log() {
public static Logger log(String customPattern) {
Long threadId = Thread.currentThread().getId();
Logger logger = byThreadIdLogs.get(threadId);
if (threadId == 1 || Objects.isNull(logger)) {
return setConsoleAppender(3);
return setConsoleAppender(3, customPattern);
}
return byThreadIdLogs.get(threadId);
}

public static Logger log() {
return log("");
}

public static Logger log(int nestedLevel) {
Long threadId = Thread.currentThread().getId();
Logger logger = byThreadIdLogs.get(threadId);
if (threadId == 1 || Objects.isNull(logger)) {
return setConsoleAppender(nestedLevel);
return setConsoleAppender(nestedLevel, "");
}
return byThreadIdLogs.get(threadId);
}

private static Logger setConsoleAppender(int nestedLevel) {
private static Logger setConsoleAppender(int nestedLevel, String customPattern) {
String className = Thread.currentThread().getStackTrace()[nestedLevel].getClassName();

Logger logger = byClassLogs.get(className);
if (Objects.isNull(logger)) {
final ConsoleAppender consoleAppender = new ConsoleAppender(new PatternLayout(CONSOLE_LAYOUT), "System.out");

String pattern;

String patternProps = properties.getProperty("single_pattern");

pattern = customPattern.isEmpty()
? patternProps == null ? CONSOLE_LAYOUT : patternProps
: customPattern;

final ConsoleAppender consoleAppender = new ConsoleAppender(new PatternLayout(pattern), "System.out");

logger = Logger.getLogger(className);
logger.setLevel(Level.DEBUG);
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/logger/Props.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package logger;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Logger;

public class Props {

public static final Logger LOGGER = Logger.getLogger(Props.class.getName());

public final static Properties properties;

static {
properties = loadProperties();
}

public static Properties loadProperties() {
String propertiesFileName = "parallel_log.properties";

Properties mainProperties = new Properties();
try (InputStream input = LoggerFactory.class.getClassLoader().getResourceAsStream(propertiesFileName)) {

if (input != null) {
mainProperties.load(input);
} else {
LOGGER.info("Sorry, unable to find " + propertiesFileName);
}

} catch (IOException e) {
e.printStackTrace();
}
return mainProperties;
}

}

0 comments on commit 1a87b08

Please sign in to comment.