Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solved task jv-logger #1070

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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/*
*.iml
target/*
logs/*
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.1</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;

Choose a reason for hiding this comment

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

Ensure that the logger is configured with appenders for both File and Console as required by the task description .

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", e);
return;
}
OrderService orderService = new OrderServiceImpl();
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 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. Params: login = {}", login);
User user = findByLogin(login);

Choose a reason for hiding this comment

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

Ensure that the findByLogin method correctly retrieves a User object. Currently, it returns a dummy user with a hardcoded password. In a real application, this should query a database or another data source.

Choose a reason for hiding this comment

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

Verify that the User class has a getPassword() method, as this is being called here. Ensure that this method is implemented correctly.

if (!user.getPassword().equals(password)) {

Choose a reason for hiding this comment

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

Verify that the User class has a getPassword() method. This method is used to compare the provided password with the user's stored password.

throw new AuthenticationException("Username or password are incorrect");
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/mate/academy/service/OrderServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
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. Params: userId = {}", userId);
List<Product> products = getAllProductsFromShoppingCart(userId);
Order order = new Order(products, userId);

Choose a reason for hiding this comment

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

Ensure that the Order class has a constructor that accepts a list of Product objects and a userId. This is necessary for creating an Order instance with the provided products and user ID.

Choose a reason for hiding this comment

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

Ensure that the Order class has a constructor that accepts a list of Product objects and a userId, as this is being used here.

// NOTE: In production ready code this order identifier should be generated by DB
Expand Down
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="info">
<Appenders>
<Console name="Console" 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 %p %c:%L %m%n</Pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Logger name ="mate.academy" level="info" additivity="false">
<AppenderRef ref="LogToFile"/>
<AppenderRef ref="Console"/>
</Logger>
<Root level="error">
<AppenderRef ref="LogToFile"/>
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Loading