java.lang.Object
com.example.project.services.Logger
com.example.project.services.Logger for Application and to show up in test and build CI workflow.
-
Constructor Summary
ConstructorsConstructorDescriptionLogger()
Default constructor writes to console does not capture.Logger
(ByteArrayOutputStream mockCapturedErrorLog, ByteArrayOutputStream mockCapturedStdOutLog) Constructor for unit tests. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Clears captured logs use in tests teardown.getLogs()
void
Logs an error message to standard error.void
logMessage
(String formattedMessage) Log message to System.out.void
setPrintToConsole
(boolean value)
-
Constructor Details
-
Logger
public Logger()Default constructor writes to console does not capture. For the project files. Use below for unit tests. -
Logger
public Logger(ByteArrayOutputStream mockCapturedErrorLog, ByteArrayOutputStream mockCapturedStdOutLog) Constructor for unit tests. Logger with constructor to input the byte array output stream to write to. (for mocking a log to check get methods.)- Parameters:
mockCapturedErrorLog
- byte array to store error logs.mockCapturedStdOutLog
- byte array to store standard output logs.
-
-
Method Details
-
setPrintToConsole
public void setPrintToConsole(boolean value) - Parameters:
value
- if this logger will also print to the console.
-
getErrorLogs
- Returns:
- Gets the error log messages.
-
getLogs
- Returns:
- returns the standard log messages.
-
logError
Logs an error message to standard error. And adds a newline.The message can include format specifiers like in
String.format(String, Object...)
.- Parameters:
message
- the error message format string (e.g., "Failed to connect to %s")
-
clearLogs
public void clearLogs()Clears captured logs use in tests teardown. -
logMessage
Log message to System.out.- Parameters:
formattedMessage
- message.
-