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
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea/*
target/
.idea/
*.iml
target/*
logs/
*.log
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
1. Configure the logger in this project. Add appenders for File and Console
1. Edit the `.gitignore` file so that your file with logs is NOT pushed to GitHub
1. If you have `System.out.println()` then replace them all with corresponding logger methods
1. Complete all `TODO` statements
1.
1. Replace all `e.printStackTrace();` with corresponding logger methods

[Try to avoid these common mistakes while solving task](./checklist.md)
3 changes: 1 addition & 2 deletions checklist.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
### Common mistakes

* Let's remove all TODO comments before submitting solution.
* Don't forget about empty lines in the end of files.
* Don't forget about empty lines in the end of files.
* Let's make our log messages informative

Bad example:
Expand Down
5 changes: 5 additions & 0 deletions logs/application.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
2026-07-02 19:51:40 INFO AuthenticationServiceImpl - Login method was called. Login: bob
2026-07-02 19:51:40 INFO AuthenticationServiceImpl - User bob successfully authenticated
2026-07-02 19:51:40 INFO OrderServiceImpl - Method completeOrder() was called for userId=2
2026-07-02 19:51:40 INFO OrderServiceImpl - Successfully fetched products from DB for userId=2
2026-07-02 19:51:40 INFO OrderServiceImpl - Order with id 1 was successfully created
17 changes: 15 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,20 @@
<version>4.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.24.3</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.24.3</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand All @@ -47,6 +60,7 @@
</configuration>
</plugin>
</plugins>

<pluginManagement>
<plugins>
<plugin>
Expand All @@ -62,5 +76,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,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) {
e.printStackTrace();
logger.error("Can't login user", e);
return;
}
OrderService orderService = new OrderServiceImpl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@

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

User user = findByLogin(login);

if (!user.getPassword().equals(password)) {
logger.error("Authentication failed for user: {}", login);
throw new AuthenticationException("Username or password are incorrect");
}

logger.info("User {} successfully authenticated", login);
return user;
}

Expand Down
15 changes: 13 additions & 2 deletions src/main/java/mate/academy/service/OrderServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@
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<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 with id {} was successfully created", order.getOrderId());
return order;
}

Expand All @@ -24,7 +33,9 @@ private List<Product> getAllProductsFromShoppingCart(Long userId) {
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 products from DB for userId={}", userId);

return products;
}
}
23 changes: 23 additions & 0 deletions src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n"/>
</Console>

<File name="File"
fileName="logs/application.log"
append="true">
<PatternLayout
pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n"/>
</File>
</Appenders>

<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
</Root>
</Loggers>
</Configuration>
Loading