-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add logger #1242
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?
Add logger #1242
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 | ||
| logs/ | ||
| target/* |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,15 +6,18 @@ | |
| 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 { | ||
| public static void main(String[] args) { | ||
| Logger logger = LogManager.getLogger(Main.class); | ||
|
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: "the project does not contain a Log4j2 configuration file that defines both Console and File appenders [CHECKLIST ITEM #1]". Add a Log4j2 configuration (for example src/main/resources/log4j2.xml) that declares a Console appender and a File appender (e.g., logs/app.log) and configure the root logger or the mate.academy package logger to use both appenders so LogManager.getLogger(...) actually writes to console and the log file.
Author
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 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 the checklist: "Add a Log4j2 configuration file that declares both a Console appender and a File appender and configures the root logger or the |
||
| AuthenticationService authenticationService = new AuthenticationServiceImpl(); | ||
| User user; | ||
| try { | ||
| user = authenticationService.login("bob", "1234"); | ||
| } catch (AuthenticationException e) { | ||
| e.printStackTrace(); | ||
| logger.error("Authentication exception while logging in", e); | ||
| return; | ||
| } | ||
| OrderService orderService = new OrderServiceImpl(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,11 +2,15 @@ | |
|
|
||
| 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); | ||
|
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 project is missing the required Log4j2 configuration. Specifically: "the project does not contain a Log4j2 configuration file that defines both Console and File appenders [CHECKLIST ITEM #1]". Add a configuration file (e.g. src/main/resources/log4j2.xml) that declares a Console appender and a File appender (writing to something like logs/app.log) and configure the root logger or the mate.academy package logger to use both appenders so logs are output to console and file. |
||
|
|
||
| @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. Login: {}", login); | ||
| User user = findByLogin(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 violates checklist item #1: "Configure the logger in this project and add appenders for File and Console". I don't see a Log4j2 configuration (e.g., log4j2.xml or log4j2.properties) in the project. Add a logger configuration file (for example src/main/resources/log4j2.xml) that defines a Console appender and a File appender so logs are written to both console and a file. 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 the checklist item: "Configure the logger in this project. Add appenders for File and Console" — I cannot find a Log4j2 configuration file (for example src/main/resources/log4j2.xml) that declares a Console appender and a File appender and binds them to the root or mate.academy logger. Add such a config so LogManager.getLogger(...) actually writes to a log file (for example a File appender writing to logs/app.log). |
||
| 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,15 @@ | |
| 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) { | ||
|
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: "Configure the logger in this project and add appenders for File and Console." I do not see any Log4j2 configuration file (for example, src/main/resources/log4j2.xml or log4j2.properties) in the project, so Console and File appenders are not defined. Add a configuration file that declares both a Console and a File appender (writing to a file inside the ignored logs/ folder) so the logger behavior matches the task requirement. 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. Missing Log4j2 configuration file. This violates the requirement: "A Log4j2 configuration file must be present that defines both a Console appender and a File appender and binds them to the root logger or the |
||
| // TODO: add log message about method completeOrder was called | ||
| logger.info("method completeOrder was called. User id: {} ", 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 | ||
|
|
@@ -24,7 +28,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("Data from DB fetched successful. User id: {} ", userId); | ||
| 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="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 %p %c:%L %m%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="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.
This violates checklist item #1: "Configure the logger in this project. Add appenders for File and Console" — I don't see any Log4j2 configuration file (e.g.,
src/main/resources/log4j2.xmlorlog4j2.properties) in the project. Add a logger configuration that defines a Console appender and a File appender (writing to the ignoredlogs/folder), and configure a root logger or package logger to use them so logs are written both to console and to a file.