From 2cf386136f5b0bb3241db2ee6eb9a05c3eabcbd5 Mon Sep 17 00:00:00 2001 From: "d.nosenko" Date: Mon, 18 Nov 2024 21:26:46 +0100 Subject: [PATCH] solution --- .gitignore | 2 ++ pom.xml | 11 ++++--- src/main/java/mate/academy/Main.java | 5 ++- .../java/mate/academy/resources/log4j2.xml | 31 +++++++++++++++++++ .../service/AuthenticationServiceImpl.java | 6 +++- .../academy/service/OrderServiceImpl.java | 8 +++-- 6 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 src/main/java/mate/academy/resources/log4j2.xml diff --git a/.gitignore b/.gitignore index 6366460f0..5d6302400 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .idea/* *.iml target/* +logs/app.log + diff --git a/pom.xml b/pom.xml index d524faa08..6f588f1db 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 @@ -16,7 +14,6 @@ https://raw.githubusercontent.com/mate-academy/style-guides/master/java/checkstyle.xml - junit @@ -24,6 +21,11 @@ 4.12 test + + org.apache.logging.log4j + log4j-core + 2.14.1 + @@ -62,5 +64,4 @@ - - + \ No newline at end of file diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index 085758fbf..1b077cc07 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -6,15 +6,18 @@ 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.Logger; public class Main { public static void main(String[] args) { + final Logger logger = LogManager.getLogger(Main.class); AuthenticationService authenticationService = new AuthenticationServiceImpl(); User user; try { user = authenticationService.login("bob", "1234"); } catch (AuthenticationException e) { - e.printStackTrace(); + logger.error("Authentication failed for user. Reason: {}", e.getMessage()); return; } OrderService orderService = new OrderServiceImpl(); diff --git a/src/main/java/mate/academy/resources/log4j2.xml b/src/main/java/mate/academy/resources/log4j2.xml new file mode 100644 index 000000000..87b994b3d --- /dev/null +++ b/src/main/java/mate/academy/resources/log4j2.xml @@ -0,0 +1,31 @@ + + + + + + + %d{HH:mm:ss.SS} %-5level %logger{36} - %msg%n + + + + + + %d %p %c:%L %m%n + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/mate/academy/service/AuthenticationServiceImpl.java b/src/main/java/mate/academy/service/AuthenticationServiceImpl.java index f9fe83b70..5465b30e6 100644 --- a/src/main/java/mate/academy/service/AuthenticationServiceImpl.java +++ b/src/main/java/mate/academy/service/AuthenticationServiceImpl.java @@ -2,11 +2,15 @@ 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.debug("Login method was called. Params: login ={}", 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..65afc5b94 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.debug("completeOrder method was called. UserID = {}", 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.debug("Method getAllProductsFromShoppingCart was called. UserID = {}",userId); return products; } }