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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/*
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The log4j2 configuration file (log4j2.xml or log4j2.properties) with File and Console appenders is not included in the submitted files. Per requirement #1, the logger must be configured with both appenders for logging to function properly.

*.iml
target/*
logs/*
13 changes: 9 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.25.4</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.25.4</version>
</dependency>
</dependencies>
<build>
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();

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("User authentication was failed. Params: login - {}", "bob", e);
return;
}
OrderService orderService = new OrderServiceImpl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

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();

@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. Params: login - {}", login);

User user = findByLogin(login);
if (!user.getPassword().equals(password)) {
throw new AuthenticationException("Username or password are incorrect");
Expand Down
11 changes: 9 additions & 2 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();

@Override
public Order completeOrder(Long userId) {
// TODO: add log message about method completeOrder was called
LOGGER.info("completeOrder method 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
Expand All @@ -24,7 +29,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("Data from DB was successfully fetched. Params: userId - {}", userId);

return products;
}
}
29 changes: 29 additions & 0 deletions src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://logging.apache.org/xml/ns"
xsi:schemaLocation="
https://logging.apache.org/xml/ns
https://logging.apache.org/xml/ns/log4j-config-2.xsd">

<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout>
<Pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36}:%L - %msg%n</Pattern>
</PatternLayout>
</Console>
<File name="FILE" fileName="logs/app.log" append="true">
<PatternLayout>
<Pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36}:%L - %msg%n</Pattern>
</PatternLayout>
</File>
</Appenders>

<Loggers>
<Root level="INFO">
<AppenderRef ref="CONSOLE"/>
<AppenderRef ref="FILE"/>
</Root>
</Loggers>

</Configuration>
Loading