diff --git a/.gitignore b/.gitignore index 6366460f0..42c2b28af 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea/* *.iml target/* +src/main/resources/log4j2.xml diff --git a/logs/app.log b/logs/app.log new file mode 100644 index 000000000..7d7ea30d2 --- /dev/null +++ b/logs/app.log @@ -0,0 +1,9 @@ +pattern 2025-01-06 00:34:46,015 INFO mate.academy.service.AuthenticationServiceImpl Method login was called : bob +pattern 2025-01-06 00:34:46,023 INFO mate.academy.service.OrderServiceImpl Method completeOrder was called : 2 +pattern 2025-01-06 00:34:46,026 INFO mate.academy.service.OrderServiceImpl Data was successfully fetched from DB +pattern 2025-01-06 00:41:54,983 INFO mate.academy.service.AuthenticationServiceImpl Method login was called : bob +pattern 2025-01-06 00:41:54,992 INFO mate.academy.service.OrderServiceImpl Method completeOrder was called : 2 +pattern 2025-01-06 00:41:54,995 INFO mate.academy.service.OrderServiceImpl Data was successfully fetched from DB +pattern 2025-01-06 00:50:08,730 INFO mate.academy.service.AuthenticationServiceImpl Method login was called : bob +pattern 2025-01-06 00:50:08,752 INFO mate.academy.service.OrderServiceImpl Method completeOrder was called : 2 +pattern 2025-01-06 00:50:08,756 INFO mate.academy.service.OrderServiceImpl Data was successfully fetched from DB diff --git a/pom.xml b/pom.xml index d524faa08..5712598e1 100644 --- a/pom.xml +++ b/pom.xml @@ -3,11 +3,9 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - mate-academy jv-logger 1.0-SNAPSHOT - 17 UTF-8 @@ -18,6 +16,17 @@ + + org.apache.logging.log4j + log4j-api + 2.20.0 + + + org.apache.logging.log4j + log4j-core + 2.20.0 + + junit junit @@ -25,6 +34,7 @@ test + @@ -63,4 +73,3 @@ - diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index 085758fbf..704651256 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -6,15 +6,19 @@ import mate.academy.service.AuthenticationServiceImpl; import mate.academy.service.OrderService; import mate.academy.service.OrderServiceImpl; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.Logger; public class Main { + private static final Logger logger = (Logger) LogManager.getLogger(Main.class); + public static void main(String[] args) { AuthenticationService authenticationService = new AuthenticationServiceImpl(); User user; try { user = authenticationService.login("bob", "1234"); } catch (AuthenticationException e) { - e.printStackTrace(); + logger.error("Failed login attempt for user: bob"); return; } OrderService orderService = new OrderServiceImpl(); diff --git a/src/main/java/mate/academy/service/AuthenticationServiceImpl.java b/src/main/java/mate/academy/service/AuthenticationServiceImpl.java index f9fe83b70..dfd14f2b8 100644 --- a/src/main/java/mate/academy/service/AuthenticationServiceImpl.java +++ b/src/main/java/mate/academy/service/AuthenticationServiceImpl.java @@ -2,11 +2,16 @@ import mate.academy.exception.AuthenticationException; import mate.academy.model.User; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; public class AuthenticationServiceImpl implements AuthenticationService { + private static final Logger logger = + LogManager.getLogger(AuthenticationServiceImpl.class); + @Override public User login(String login, String password) throws AuthenticationException { - //TODO: add corresponding log message about method login was called + logger.info("Method login was called : {}", login); User user = findByLogin(login); if (!user.getPassword().equals(password)) { throw new AuthenticationException("Username or password are incorrect"); diff --git a/src/main/java/mate/academy/service/OrderServiceImpl.java b/src/main/java/mate/academy/service/OrderServiceImpl.java index eeabe1555..f6db264f5 100644 --- a/src/main/java/mate/academy/service/OrderServiceImpl.java +++ b/src/main/java/mate/academy/service/OrderServiceImpl.java @@ -4,11 +4,15 @@ import java.util.List; import mate.academy.model.Order; import mate.academy.model.Product; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; public class OrderServiceImpl implements OrderService { + private static final Logger logger = LogManager.getLogger(OrderServiceImpl.class); + @Override public Order completeOrder(Long userId) { - // TODO: add log message about method completeOrder was called + logger.info("Method completeOrder was called : {}", userId); List products = getAllProductsFromShoppingCart(userId); Order order = new Order(products, userId); // NOTE: In production ready code this order identifier should be generated by DB @@ -24,7 +28,7 @@ private List getAllProductsFromShoppingCart(Long userId) { Product macBook = new Product("MacBook Air 2020", BigDecimal.valueOf(1399)); Product xiaomi = new Product("Xiaomi 12", BigDecimal.valueOf(499)); List products = List.of(iphone, macBook, xiaomi); - // TODO: add log message about successful fetched data from DB + logger.info("Data was successfully fetched from DB"); return products; } } diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml new file mode 100644 index 000000000..1b22e4a40 --- /dev/null +++ b/src/main/resources/log4j2.xml @@ -0,0 +1,23 @@ + + + + + + + + + pattern %d %p %c %m%n + + + + + + + + + + + + + +