Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@
<version>4.12</version>
<scope>test</scope>
</dependency>

<!-- Source: https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.22.0</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.22.0</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/mate/academy/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
} catch (AuthenticationException e) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This log message isn't very informative. Per checklist item #3, it's better to include context, such as the username for which the login failed. Also, it's a good practice to pass the exception object e as a second argument to logger.error() to log the stack trace.

e.printStackTrace();
logger.error("Wrong password");
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This log message isn't very informative. It's a good practice to include the exception details in the log to make debugging easier. This violates checklist item #3: 'Let's make our log messages informative'.

You could pass the exception object e as the second argument to the logger method, like logger.error("Authentication failed", e); or include its message like logger.error("Authentication failed: {}", e.getMessage());.

return;
}
OrderService orderService = new OrderServiceImpl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

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 login:{}", login);
User user = findByLogin(login);
if (!user.getPassword().equals(password)) {
throw new AuthenticationException("Username or password are incorrect");
logger.warn("Username or password are incorrect for user: {}", login);
}
Comment on lines 15 to 17
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When the password doesn't match, you should throw an AuthenticationException as indicated by the method signature. Currently, the code only logs a warning and then proceeds to return the user, which would allow a successful login with an incorrect password.

return user;
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/mate/academy/service/OrderServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This violates checklist item #1: 'Let's remove all TODO comments before submitting solution.' Please remove this comment now that you've implemented the logging.

logger.info("Method completeOrder was called for 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
Expand All @@ -25,6 +30,7 @@ private List<Product> getAllProductsFromShoppingCart(Long userId) {
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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This violates checklist item #1: 'Let's remove all TODO comments before submitting solution.' Please remove this comment now that you've implemented the logging.

logger.info("Successful fetched data from DB");
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This violates checklist item #3: 'Let's make our log messages informative'. This log message is not very informative. It would be better to include the userId to specify for which user the data was fetched.

return products;
}
}
22 changes: 22 additions & 0 deletions src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info">
<Appenders>
<Console name="LogToConsole" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name ="LogToFile" fileName ="logs/app.log">
<PatternLayout>
<Pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Logger name="mate.academy" level="info" additivity="false">
<AppenderRef ref="LogToFile"/>
<AppenderRef ref="LogToConsole"/>
</Logger>
<Root level="error">
<AppenderRef ref="LogToConsole"/>
</Root>
</Loggers>
</Configuration>
Loading