diff --git a/.gitignore b/.gitignore index 6366460f0..3abc7b8b4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea/* *.iml target/* +logs/app.log diff --git a/pom.xml b/pom.xml index d524faa08..21f07d7ae 100644 --- a/pom.xml +++ b/pom.xml @@ -24,6 +24,11 @@ 4.12 test + + org.apache.logging.log4j + log4j-core + 2.24.1 + diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index 085758fbf..16ba831b3 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -6,18 +6,24 @@ 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"); } catch (AuthenticationException e) { - e.printStackTrace(); + logger.error("Authentication failed for user 'bob'. Reason: {}", e.getMessage()); + logger.debug("Exception details:", e); return; } OrderService orderService = new OrderServiceImpl(); orderService.completeOrder(user.getUserId()); + logger.info("Current working directory: {}", System.getProperty("user.dir")); } } diff --git a/src/main/java/mate/academy/service/AuthenticationServiceImpl.java b/src/main/java/mate/academy/service/AuthenticationServiceImpl.java index f9fe83b70..e9fd1accf 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 for user with 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..712a5e5d1 100644 --- a/src/main/java/mate/academy/service/OrderServiceImpl.java +++ b/src/main/java/mate/academy/service/OrderServiceImpl.java @@ -4,11 +4,16 @@ 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 for 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 +29,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("Method getAllProductsFromShoppingCart was called for userId: {}", userId); return products; } } diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml new file mode 100644 index 000000000..5b6752a80 --- /dev/null +++ b/src/main/resources/log4j2.xml @@ -0,0 +1,32 @@ + + + + + + + %d{HH:mm:ss.SS} %-5level %logger{36} - %msg%n + + + + + + %d %p %c:%L %m%n + + + + + + + + + + + + + + + + + + +