-
Notifications
You must be signed in to change notification settings - Fork 1.2k
added logger to project #1263
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
base: master
Are you sure you want to change the base?
added logger to project #1263
Changes from 1 commit
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/* | ||
| log/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| 2026-07-07 19:10:23 [main] INFO mate.academy.service.AuthenticationServiceImpl - Method login was called for user: bob | ||
| 2026-07-07 19:10:23 [main] INFO mate.academy.service.AuthenticationServiceImpl - User bob successfully logged in. | ||
| 2026-07-07 19:10:23 [main] INFO mate.academy.service.OrderServiceImpl - Method completeOrder was called for user Id: 2 | ||
| 2026-07-07 19:12:49 [main] INFO mate.academy.service.AuthenticationServiceImpl - Method login was called for user: bob | ||
| 2026-07-07 19:12:49 [main] INFO mate.academy.service.AuthenticationServiceImpl - User bob successfully logged in. | ||
| 2026-07-07 19:12:49 [main] INFO mate.academy.service.OrderServiceImpl - Method completeOrder was called for user Id: 2 | ||
| 2026-07-07 19:12:49 [main] INFO mate.academy.service.OrderServiceImpl - Successfully fetched data from DB for user Id: 2. Retracted 3 products. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,39 @@ | ||
| package mate.academy.service; | ||
|
|
||
| import java.util.Objects; | ||
| import mate.academy.exception.AuthenticationException; | ||
|
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. The task requires a proper logger configuration with both File and Console appenders so logs go to console and a file under an ignored directory like |
||
| import mate.academy.model.User; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class AuthenticationServiceImpl implements AuthenticationService { | ||
|
|
||
| private static final Logger logger = LoggerFactory.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: {}", login); | ||
| User user = findByLogin(login); | ||
|
3xactCoder marked this conversation as resolved.
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. This log message is informative but could better follow the checklist guidance about parameterized messages. Also, you might want a small space after the comma for readability: 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. There is no space after the comma between |
||
| if (!user.getPassword().equals(password)) { | ||
|
|
||
|
Comment on lines
+10
to
+17
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. The task requires: "Configure the logger in this project. Add appenders for File and Console" and "If you have |
||
| if (user == null || !Objects.equals(user.getPassword(), password)) { | ||
|
|
||
| logger.warn("Failed login attempt for user: {}", login); | ||
| throw new AuthenticationException("Username or password are incorrect"); | ||
| } | ||
|
|
||
|
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. This log message does not follow checklist item |
||
| logger.info("User {} successfully logged in.", login); | ||
|
Comment on lines
+18
to
+23
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. This method currently never throws |
||
| return user; | ||
| } | ||
|
Comment on lines
24
to
25
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. When logging the authentication failure, you are not including the exception |
||
|
|
||
| private User findByLogin(String login) { | ||
| User user = new User(login, "1234"); | ||
| // this user identifier should be set by DB. We will use dummy data for this example | ||
| user.setUserId(2L); | ||
| return user; | ||
| if ("bob".equals(login)) { | ||
| User user = new User(login, "1234"); | ||
| user.setUserId(2L); | ||
|
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. This log message has a trailing space before the closing quote: |
||
| return user; | ||
|
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. This message has a trailing space inside the format string ( |
||
| } | ||
|
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. This violates checklist item #1: "Let's remove all TODO comments before submitting solution." The comment |
||
| return null; | ||
| } | ||
|
Comment on lines
+33
to
34
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. The log message concatenates strings and parameters incorrectly: |
||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,11 +4,17 @@ | |
| import java.util.List; | ||
|
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. The |
||
| import mate.academy.model.Order; | ||
| import mate.academy.model.Product; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class OrderServiceImpl implements OrderService { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class); | ||
|
|
||
| @Override | ||
| public Order completeOrder(Long 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. The info log for the login method is informative and parameterized, which satisfies the checklist, but you can improve readability by adding a space after the colon: |
||
| // TODO: add log message about method completeOrder was called | ||
|
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. This violates checklist item: "Let's remove all TODO comments before submitting solution." You’ve added the log call, but the TODO comment itself still needs to be removed. |
||
| logger.info("Method completeOrder was called for user Id: {}",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. Similarly, here you have no space after the comma between the literal and the placeholder ( |
||
| List<Product> products = getAllProductsFromShoppingCart(userId); | ||
| Order order = new Order(products, userId); | ||
| // NOTE: In production ready code this order identifier should be generated by DB | ||
|
|
@@ -25,6 +31,8 @@ private List<Product> getAllProductsFromShoppingCart(Long userId) { | |
| 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 | ||
|
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. This violates checklist item: "Let's remove all TODO comments before submitting solution." You added a log message, but the TODO comment is still present and should be deleted. |
||
| logger.info("Successfully fetched data from DB for user Id:" | ||
| + " {}. Retracted {} products.", userId, products.size()); | ||
| return products; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <Configuration status="WARN"> | ||
| <Properties> | ||
| <Property name="basePath">logs</Property> | ||
| </Properties> | ||
|
|
||
| <Appenders> | ||
| <Console name="ConsoleAppender" target="SYSTEM_OUT"> | ||
| <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/> | ||
| </Console> | ||
|
|
||
| <File name="FileAppender" fileName="log/app.log" append="true"> | ||
| <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/> | ||
| </File> | ||
| </Appenders> | ||
|
|
||
| <Loggers> | ||
| <Root level="info"> | ||
| <AppenderRef ref="ConsoleAppender"/> | ||
| <AppenderRef ref="FileAppender"/> | ||
| </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.
This violates checklist item #1:
Let's remove all TODO comments before submitting solution.You still have a TODO comment here; implement the logging as required and then remove the TODO line.