Skip to content
Open
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
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Logging -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -62,5 +69,4 @@
</plugins>
</pluginManagement>
</build>
</project>

</project>
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,18 +6,22 @@
import mate.academy.service.AuthenticationServiceImpl;
import mate.academy.service.OrderService;
import mate.academy.service.OrderServiceImpl;
import org.apache.log4j.Logger;

public class Main {
private static final Logger logger = Logger.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'", e);
return;
}
OrderService orderService = new OrderServiceImpl();
orderService.completeOrder(user.getUserId());
logger.info("Order processing finished for userId=" + user.getUserId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@

import mate.academy.exception.AuthenticationException;
import mate.academy.model.User;
import org.apache.log4j.Logger;

public class AuthenticationServiceImpl implements AuthenticationService {
private static final Logger logger = Logger.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() called 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.

This log message is informative, but it could be more structured to better align with the 'Good example' from the checklist. Consider including the parameter name in the message for clarity, for example: "login() method called with login='" + login + "'".

User user = findByLogin(login);
if (!user.getPassword().equals(password)) {
logger.warn("Invalid credentials 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.

This warning message is good because it includes the username. To make it even more helpful, you could be more specific about why it failed. For example: "Invalid password attempt for user: " + login.

throw new AuthenticationException("Username or password are incorrect");
}
logger.info("User authenticated: " + 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.

This is a good log entry for a successful event. To improve consistency with other log messages, consider a more structured format like: "User authenticated successfully: " + login.

return user;
}

private User findByLogin(String login) {
User user = new User(login, "1234");
// this user identifier should be set by DB. We will use dummy data for this example
user.setUserId(2L);
user.setUserId(2L); // dummy data
return user;
}
}
13 changes: 7 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,28 @@
import java.util.List;
import mate.academy.model.Order;
import mate.academy.model.Product;
import org.apache.log4j.Logger;

public class OrderServiceImpl implements OrderService {
private static final Logger logger = Logger.getLogger(OrderServiceImpl.class);

@Override
public Order completeOrder(Long userId) {
// TODO: add log message about method completeOrder was called
logger.info("completeOrder() 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
// For test purpose we simplify this and return dummy data
order.setOrderId(1L);
logger.info("Order created with id=" + order.getOrderId() + " for userId=" + userId);
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("Fetched " + products.size() + " products from shopping cart for userId="
+ userId);
return products;
}
}
14 changes: 14 additions & 0 deletions src/main/java/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
log4j.rootLogger=INFO, console, file

# Console appender
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{ISO8601} %-5p [%t] %c - %m%n

# File appender
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logs/app.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=3
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ISO8601} %-5p [%t] %c - %m%n
Loading