Question
Need help writing a java utility class that uses log methods to print info to the console. The logger class must be tested against the
Need help writing a java utility class that uses log methods to print info to the console. The logger class must be tested against the testlogger provided below and must print the output in the assignment.
The assignment is below:
Your assignment this week is to write a utility class which will provide the following four log methods that print information to the console:
public void logDebug(String strMessage)
public void logInfo(String strMessage)
public void logWarning(String strMessage)
public void logError(String strMessage)
Each of these methods will accept a String argument that will contain a useful message and print the message to the output window. In addition, you should have methods that allow the user to turn logging on and off:
public void enableLogging()
public void disableLogging()
Finally, you need a way to set the current log level. This level will determine what information gets logged.
public void setLogLevel(int logLevel) // log levels will be numeric values (1-error, 2- warning, 3-informational, 4-debug)
Here is a table that shows what will be printed depending on the log level set:
Debug | Output: Debug Info Warning Error |
Info | Output: Info Warning Error |
Warning | Output: Warning Error |
Error | Output: Error |
Notice that as the level approaches "Error" less information is actually logged. This allows you to control the detail of your logger output.
In order to create this class you will add a package to your project to contain the Logger class. You should name this package, "utilities". It should contain one class, "Logger.java", that contains all of the methods listed above.
Below is a test class that you can download to test your class.
If your class is written correctly, you should see the following output when you run the test class.
Testing disable/enable logging You should see no messages: Testing Debug Detail You should see four messages: DEBUG: Message #1 INFO: Message #2 WARNING: Message #3 ERROR: Message #4 Testing Info Detail You should see three messages: INFO: Message #1 WARNING: Message #2 ERROR: Message #3 Testing Warning Detail You should see two messages: WARNING: Message #1 ERROR: Message #2 Testing Error Detail You should see one messages: ERROR: Message #1
Here is the code for the testlogger to test the logger:
TestLogger.java:
/* * Test application to test using the Logger class */ package utilities;
public class TestLogger { public static void main(String[] args) { // Create a Logger object Logger myLogger = new Logger(); // Test #1 - Test disable/enable logging System.out.println("Testing disable/enable logging"); System.out.println("You should see no messages:"); myLogger.setLogLevel(4); myLogger.disableLogging(); myLogger.logDebug("Test failed, this should not print!"); myLogger.logInfo("Test failed, this should not print!"); myLogger.logWarning("Test failed, this should not print!"); myLogger.logError("Test failed, this should not print!");
myLogger.enableLogging(); // Test #2 - Test Debug Detail System.out.println("Testing Debug Detail"); System.out.println("You should see four messages:"); myLogger.setLogLevel(4); myLogger.logDebug("Message #1"); myLogger.logInfo("Message #2"); myLogger.logWarning("Message #3"); myLogger.logError("Message #4"); // Test #3 - Test Info Detail System.out.println("Testing Info Detail"); System.out.println("You should see three messages:"); myLogger.setLogLevel(3); myLogger.logDebug("Test failed, this should not print!"); myLogger.logInfo("Message #1"); myLogger.logWarning("Message #2"); myLogger.logError("Message #3"); // Test #4 - Test Warning Detail System.out.println("Testing Warning Detail"); System.out.println("You should see two messages:"); myLogger.setLogLevel(2); myLogger.logDebug("Test failed, this should not print!"); myLogger.logInfo("Test failed, this should not print!"); myLogger.logWarning("Message #1"); myLogger.logError("Message #2"); // Test #5 - Test Error Detail System.out.println("Testing Error Detail"); System.out.println("You should see one message:"); myLogger.setLogLevel(1); myLogger.logDebug("Test failed, this should not print!"); myLogger.logInfo("Test failed, this should not print!"); myLogger.logWarning("Test failed, this should not print!"); myLogger.logError("Message #1"); // ----------------------------------------------------------------
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started