-
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 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/* | ||
| 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 |
|---|---|---|
|
|
@@ -6,16 +6,23 @@ | |
| import mate.academy.service.AuthenticationServiceImpl; | ||
| import mate.academy.service.OrderService; | ||
| import mate.academy.service.OrderServiceImpl; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class Main { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(Main.class); | ||
|
|
||
| public static void main(String[] args) { | ||
|
|
||
| AuthenticationService authenticationService = new AuthenticationServiceImpl(); | ||
| User 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. The failure log in |
||
| try { | ||
| user = authenticationService.login("bob", "1234"); | ||
| logger.info("User bob tries to 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. This log message hard-codes |
||
| } catch (AuthenticationException e) { | ||
|
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: |
||
| e.printStackTrace(); | ||
| return; | ||
| logger.error("Authentification failed for user bob"); | ||
|
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 to the info log above, this error message hard-codes |
||
| throw new RuntimeException("Authentification failed for user bob"); | ||
| } | ||
| OrderService orderService = new OrderServiceImpl(); | ||
| orderService.completeOrder(user.getUserId()); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,38 @@ | ||
| 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)) { | ||
| throw new AuthenticationException("Username or password are incorrect"); | ||
|
|
||
|
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.error("Failed login attempt for user: {}", 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. The failure log here does not include the exception object, which was requested in the previous review: |
||
| throw new AuthenticationException("Failed login attempt for 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 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,27 +4,30 @@ | |
| 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 | ||
| 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 | ||
| // For test purpose we simplify this and return dummy data | ||
| order.setOrderId(1L); | ||
|
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 failure log currently does not include the exception object as requested in the previous review (e.g. |
||
| return order; | ||
| } | ||
|
|
||
| private List<Product> getAllProductsFromShoppingCart(Long userId) { | ||
| // NOTE: In production ready code this method should fetch data from DB | ||
| // For test purpose we simplify this method and return dummy data | ||
|
|
||
|
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 error log violates the logging checklist and the previous high‑priority requirement: failure logging should include the exception object and use parameter placeholders instead of hard‑coded values. Consider something like |
||
| Product iphone = new Product("iPhone X", BigDecimal.valueOf(1199)); | ||
|
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, throwing a |
||
| 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("Successfully fetched data from DB for user with 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. The info log is parameterized and informative, which is good, but the spacing around the placeholder is slightly inconsistent ( |
||
| 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.
The log message has no space after the comma in
{},userId, and previous review feedback asked to tidy up such minor formatting issues; adding the space ({}, userId) will make the output cleaner and consistent.