CodeGym /Courses /Java Collections /Tasks | Level 4 | Lesson 10

Tasks | Level 4 | Lesson 10

Java Collections
Level 4 , Lesson 10
Available
14
Task
Java Collections, level 4, lesson 10
Locked
Setting up a logger
You've been given a log4j.properties file that contains logger settings for developers. The logging requirements for the production environment have changed.
14
Task
Java Collections, level 4, lesson 10
Locked
Adding logging to a class
In Intellij IDEA, press Alt+Ctrl+Shift+S -> Global Libraries -> New Global Library -> From Maven... In the window that opens, enter the following in the search bar: org.slf4j:slf4j-api:1.7.23 -> Search (Shift+Enter). Choose where to download the logging library.
Comments (9)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Lisa Level 41
12 January 2022
I've described how to add the libs to IntelliJ using Maven repos in the comments section here
Skynet Level 40, USA
26 September 2021
In the first task, you need to update/change lines 4-8 and 14. Information on RollingFileAppender can be found here. If you want to test your logs from task 2, you will need to: 1) create a log4j.properties file and save it in the root of the project folder. 2) copy/paste log4j file info from task 1. 3) in line 1 set the rootLogger to =TRACE, stdout. 4) in line 5 set the threshold to DEBUG 5) in line 6 set the desired path to the .log file 6) in line 14 set the direct log message to stdout threshold to ALL 7) delete the ConversionPattern line 8) set the three framework levels to DEBUG 9) in the Solution class add import org.apache.log4j.PropertyConfigurator; 10) in main() add the following test code:

String logProperties = "log4j.properties";
PropertyConfigurator.configure(logProperties);

Path path = Paths.get(logProperties).toAbsolutePath();
try (InputStream is = new FileInputStream(path.toFile())) {
    Properties properties = new Properties();
    properties.load(is);
    System.out.println("See detailed log in file: " + properties.getProperty("log4j.appender.file.File"));
} catch (IOException e) { e.printStackTrace(); }

System.out.println("\nLOG STARTED");
System.out.println("=====================");
Solution solution = new Solution(150, "Skynet", new Date());
solution.calculateAndSetValue3(5555L);
solution.printString();
solution.printDateAsLong();
solution.divide(10, 5);
solution.divide(10, 0);
solution.setValue1(999);
solution.setValue2("Test string");
solution.setValue3(new Date());
11) You will need to comment out/remove the PropertyConfigurator import before sending your code for validation.
Lisa Level 41
12 January 2022
the current 1.7 documentation can be found here
Juan Ma Level 41, Arauca, Colombia
4 June 2020
Here is how slf4j-log4j works for me into Idea: 1) Download 3 files: slf4j-log4j12-1.7.12 and slf4j-api-1.7.12 jar files, and log4j-1.2.17 zip file, in the last one extract and get the jar file. 2) In Intellij: Go to File -> Project Sturcture -> libraries; Find "+" button and add downloaded files 3) You can set the location of your log4j.properties from inside your java app by using: org.apache.log4j.PropertyConfigurator.configure(file/location/log4j.properties)
cute potato Level 46, Poland, Poland
6 March 2023
Thank you ;)
Elvis Lee Level 41, Aurora, United States
4 February 2020
set the appender.file to RollingFileAppender
Marcin Olech Level 41, Kraków, Poland
8 January 2020
2nd was fun ;p
Zoltán R Level 26, Budapest, Hungary
30 January 2020
what did you set the backupindex to pass the est? O_o why is 6 a wrong answer?
Alexandre Lalancette Level 41, Quebec, Canada
5 February 2020
Got it with log4j.appender.file=org.apache.log4j.RollingFileAppender