Skip to content
Open

done #1234

Show file tree
Hide file tree
Changes from 3 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/*
*.log

Comment thread
chupa-ilona marked this conversation as resolved.
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@
<version>4.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.23.1</version> <!-- 'x' should be changed to the most recent version number. -->
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.23.1</version> <!-- 'x' should be changed to the most recent version number. -->
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 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 with login: " + login + "");
User user = findByLogin(login);
if (!user.getPassword().equals(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 is an improvement, but it's still not quite right. According to checklist item #3, you should use parameterized logging for better performance and readability. Instead of string concatenation, use placeholders like this: logger.info("Method login was called. Params: login={}", login);

throw new AuthenticationException("Username or password are incorrect");
Expand All @@ -20,4 +24,12 @@ private User findByLogin(String login) {
user.setUserId(2L);
return user;
}

public void setLogger(Logger logger) {
this.logger = logger;
}

public Logger getLogger() {
return logger;
}
}
20 changes: 14 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,35 @@
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 Logger logger = LogManager.getLogger(OrderServiceImpl.class);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There should be a blank line after this field declaration to improve code readability. This was also mentioned in the previous review feedback.


@Override
public Order completeOrder(Long userId) {
// TODO: add log message about method completeOrder was called
logger.info("Method completeOrder was called with 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.

This violates checklist item #3: 'Let's make our log messages informative'. It's better to use parameterized logging for performance and readability, like this:

logger.info("Method completeOrder was called with 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);
return order;
}

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
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.info("Successfully 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 log message is not informative enough, which violates checklist item #3. Please include the userId to provide more context about which user's data was fetched.

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 would be more useful if it included the userId for which the products are being fetched. For example: logger.info("Successfully fetched data from DB for userId: {}", userId);

return products;
}

public void setLogger(Logger logger) {
this.logger = logger;
}

public Logger getLogger() {
return logger;
}
}
26 changes: 26 additions & 0 deletions src/main/resources/ log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<!-- Define the appenders -->
<Appenders>
<!-- Console Appender -->
<Console name="ConsoleAppender" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<!-- File Appender -->
<File name="FileAppender" fileName="applogs/application_logs.log">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<!-- Define the loggers -->
<Loggers>
<!-- Root Logger -->
<Root level="info">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="FileAppender" />
</Root>
<!-- Logger for specific package -->
<Logger name="com.example.myapp" level="debug">
<AppenderRef ref="FileAppender" />
</Logger>
</Loggers>
</Configuration>
Loading