-
Notifications
You must be signed in to change notification settings - Fork 1k
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
base: master
Are you sure you want to change the base?
logger configured #1052
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.idea/* | ||
*.iml | ||
target/* | ||
logs/app.log |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
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> | ||
|
There was a problem hiding this comment.
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 .