From 1ea711c69b3cf616ee8a1451e9d431a0a48fcb43 Mon Sep 17 00:00:00 2001 From: Anton Iliashenko Date: Mon, 6 Jan 2025 08:11:07 +0100 Subject: [PATCH 1/2] solved task jv-logger --- .gitignore | 1 + pom.xml | 5 ++++ src/main/java/mate/academy/Main.java | 8 +++++-- .../service/AuthenticationServiceImpl.java | 6 ++++- .../academy/service/OrderServiceImpl.java | 6 ++++- src/main/resources/log4j2.xml | 23 +++++++++++++++++++ 6 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 src/main/resources/log4j2.xml diff --git a/.gitignore b/.gitignore index 6366460f0..bb04aae6e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea/* *.iml target/* +logs/* diff --git a/pom.xml b/pom.xml index d524faa08..4f5efc0bb 100644 --- a/pom.xml +++ b/pom.xml @@ -24,6 +24,11 @@ 4.12 test + + org.apache.logging.log4j + log4j-core + 2.14.1 + diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index 085758fbf..fdbf706d7 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.Logger; public class Main { + private static final Logger logger = LogManager.getLogger(Main.class); + public static void main(String[] args) { AuthenticationService authenticationService = new AuthenticationServiceImpl(); User user; try { - user = authenticationService.login("bob", "1234"); + user = authenticationService.login("bob", "4124235"); } catch (AuthenticationException e) { - e.printStackTrace(); + logger.error("Can't login", e); 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..58772c1ed 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.info("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..c87f2699b 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. Params: 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 diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml new file mode 100644 index 000000000..92b7ca4e3 --- /dev/null +++ b/src/main/resources/log4j2.xml @@ -0,0 +1,23 @@ + + + + + + + + + %d %p %c:%L %m%n + + + + + + + + + + + + + + \ No newline at end of file From c1b820840d1807142eab1687e4055e43af01cdf2 Mon Sep 17 00:00:00 2001 From: Anton Iliashenko Date: Mon, 6 Jan 2025 08:17:16 +0100 Subject: [PATCH 2/2] fixed-1 --- src/main/java/mate/academy/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index fdbf706d7..afaa13929 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -16,7 +16,7 @@ public static void main(String[] args) { AuthenticationService authenticationService = new AuthenticationServiceImpl(); User user; try { - user = authenticationService.login("bob", "4124235"); + user = authenticationService.login("bob", "1234"); } catch (AuthenticationException e) { logger.error("Can't login", e); return;