Skip to content
Open

Develop #1253

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea/*
*.iml
target/*
logs/
*.log
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<maven.checkstyle.plugin.configLocation>
https://raw.githubusercontent.com/mate-academy/style-guides/master/java/checkstyle.xml
</maven.checkstyle.plugin.configLocation>
<log4j.version>2.20.0</log4j.version>
</properties>

<dependencies>
Expand All @@ -24,6 +25,17 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/mate/academy/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,32 @@
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();
AuthenticationService authenticationService =
new AuthenticationServiceImpl();

User user;

String login = "bob";

try {
user = authenticationService.login("bob", "1234");
user = authenticationService.login(login, "1234");
} catch (AuthenticationException e) {
e.printStackTrace();
logger.error("Authentication failed for user with login={}", login, e);
return;
}

OrderService orderService = new OrderServiceImpl();

orderService.completeOrder(user.getUserId());

logger.info("Program finished successfully");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@

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("Method login was called. Params: login={}", login);

User user = findByLogin(login);

if (!user.getPassword().equals(password)) {
logger.warn("Authentication failed for user {}", login);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to write a message about incorrect credentials:

logger.warn("Authentication failed. Incorrect credentials for login={}", login);

throw new AuthenticationException("Username or password are incorrect");
}

logger.info("User logged in successfully. Params: login={}, userId={}",
login, user.getUserId());
return user;
}

Expand Down
21 changes: 15 additions & 6 deletions src/main/java/mate/academy/service/OrderServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,36 @@
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);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be debug, not info:

logger.debug("Method completeOrder was called. Params: userId={}", userId);

List<Product> products = getAllProductsFromShoppingCart(userId);
Order order = new Order(products, userId);
// NOTE: In production ready code this order identifier should be generated by DB
// For test purpose we simplify this and return dummy data
order.setOrderId(1L);
logger.info("Order {} was completed for user {}", order.getOrderId(), userId);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would look better that way:

logger.info("Order was completed. Params: orderId={}, userId={}, productsCount={}",
                order.getOrderId(), userId, products.size());

return order;
}
Comment thread
slawomir-pacek marked this conversation as resolved.

private List<Product> getAllProductsFromShoppingCart(Long userId) {
// NOTE: In production ready code this method should fetch data from DB
// For test purpose we simplify this method and return dummy data
logger.info("Fetching products from shopping cart for user {}",
userId);

Product iphone = new Product("iPhone X", BigDecimal.valueOf(1199));
Product macBook = new Product("MacBook Air 2020", BigDecimal.valueOf(1399));
Product xiaomi = new Product("Xiaomi 12", BigDecimal.valueOf(499));

List<Product> products = List.of(iphone, macBook, xiaomi);
// TODO: add log message about successful fetched data from DB

logger.debug("Successfully fetched products from DB. Params: userId={}, productsCount={}",
userId, products.size());

return products;
}
}
25 changes: 25 additions & 0 deletions src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">

<Appenders>

<Console name="ConsoleAppender" target="SYSTEM_OUT">
<PatternLayout
pattern="%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n"/>
</Console>

<File name="FileAppender" fileName="logs/app.log">
<PatternLayout
pattern="%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n"/>
</File>

</Appenders>

<Loggers>
<Root level="info">
<AppenderRef ref="ConsoleAppender"/>
<AppenderRef ref="FileAppender"/>
</Root>
</Loggers>

</Configuration>
Loading