-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Added Logger #1238
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 #1238
Changes from 2 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 |
|---|---|---|
|
|
@@ -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; | ||
| 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("Wrong password"); | ||
|
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 isn't very informative. It's a good practice to include the exception details in the log to make debugging easier. This violates checklist item #3: 'Let's make our log messages informative'. You could pass the exception object |
||
| return; | ||
| } | ||
| OrderService orderService = new OrderServiceImpl(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,14 +2,19 @@ | |
|
|
||
| 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 login:{}", login); | ||
| User user = findByLogin(login); | ||
| if (!user.getPassword().equals(password)) { | ||
| throw new AuthenticationException("Username or password are incorrect"); | ||
| logger.warn("Username or password are incorrect for user: {}", login); | ||
| } | ||
|
Comment on lines
15
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. When the password doesn't match, you should throw an |
||
| return user; | ||
| } | ||
|
|
||
| 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 | ||
|
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.' Please remove this comment now that you've implemented the logging. |
||
| logger.info("Method completeOrder was called for userId:{}", userId); | ||
| 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 +30,7 @@ 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 #1: 'Let's remove all TODO comments before submitting solution.' Please remove this comment now that you've implemented the logging. |
||
| logger.info("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 #3: 'Let's make our log messages informative'. This log message is not very informative. It would be better to include the |
||
| return products; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <Configuration status="info"> | ||
| <Appenders> | ||
| <Console name="LogToConsole" 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{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern> | ||
| </PatternLayout> | ||
| </File> | ||
| </Appenders> | ||
| <Loggers> | ||
| <Logger name="mate.academy" level="info" additivity="false"> | ||
| <AppenderRef ref="LogToFile"/> | ||
| <AppenderRef ref="LogToConsole"/> | ||
| </Logger> | ||
| <Root level="error"> | ||
| <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.
This log message isn't very informative. Per checklist item #3, it's better to include context, such as the username for which the login failed. Also, it's a good practice to pass the exception object
eas a second argument tologger.error()to log the stack trace.