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

logger configured #1052

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/app.log
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.24.1</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
8 changes: 7 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,24 @@
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;
Comment on lines +9 to +10

Choose a reason for hiding this comment

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

The logger is imported and used, but ensure that it is configured with appenders for both File and Console as per the task requirements .


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("Authentication failed for user 'bob'. Reason: {}", e.getMessage());

Choose a reason for hiding this comment

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

Be cautious when logging sensitive information such as usernames or passwords. In this case, logging the username 'bob' might be acceptable, but ensure that no sensitive data like passwords are logged.

Choose a reason for hiding this comment

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

Be cautious about logging sensitive information like usernames. Consider logging a user ID or anonymized identifier instead to comply with privacy and security policies .

logger.debug("Exception details:", e);
return;
}
OrderService orderService = new OrderServiceImpl();
orderService.completeOrder(user.getUserId());
logger.info("Current working directory: {}", System.getProperty("user.dir"));
}
}
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(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 for user with login: {}", login);

Choose a reason for hiding this comment

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

Be cautious when logging sensitive information such as usernames. Ensure that logging the username is compliant with your application's privacy and security policies.

Choose a reason for hiding this comment

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

Logging the username directly can be a security risk. Consider logging anonymized data or a user ID instead to comply with privacy and security policies.

Choose a reason for hiding this comment

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

it's better to use debug level when methods are called

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

Choose a reason for hiding this comment

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

Be cautious when logging user identifiers. Ensure that logging user IDs is compliant with your application's privacy and security policies.

Choose a reason for hiding this comment

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

Logging user IDs can be sensitive. Ensure that this practice complies with your application's privacy and security policies.

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,7 @@ 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("Method getAllProductsFromShoppingCart was called for userId: {}", userId);

Choose a reason for hiding this comment

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

Logging user IDs can be sensitive. Ensure that this practice complies with your application's privacy and security policies.

return products;
}
}
32 changes: 32 additions & 0 deletions src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info">
<Appenders>
<!-- Console Appender -->
<Console name="LogToConsole" target="SYSTEM_OUT">
<PatternLayout>
<Pattern>%d{HH:mm:ss.SS} %-5level %logger{36} - %msg%n</Pattern>
</PatternLayout>
</Console>
<!-- File Appender: Automatically creates the 'logs' directory and 'app.log' file -->
<File name="LogToFile" fileName="logs/app.log">
<PatternLayout>
<Pattern>%d %p %c:%L %m%n</Pattern>
</PatternLayout>
</File>
</Appenders>

<Loggers>
<!-- Define logger for specific package -->
<Logger name="mate.academy" level="info" additivity="false">
<AppenderRef ref="LogToFile"/>
<AppenderRef ref="LogToConsole"/>
</Logger>

<!-- Root Logger -->
<Root level="error">
<AppenderRef ref="LogToFile"/>
<AppenderRef ref="LogToConsole"/>
</Root>
</Loggers>
</Configuration>

Loading