Skip to content

Commit

Permalink
Fix log level not affecting loggers
Browse files Browse the repository at this point in the history
Log handlers being set prior to init causes their log level to
remain at INFO rather than updaing to the latest log level.
This causes changing the log level in config to not affect any logging.

As the root logger defaults at INFO, this causes all loggers created to
be at INFO so finer log levels will never be reflected.
As there are many other packages that depends on the root logger,
a separate base logger for the application should be created so that
all loggers created in the application derive from it.
This enables finer control over log levels of logger in the application
without affecting loggers from other packages.

Let's
- Set the log level of handlers in init() to allow log level changes
to be reflected in the log file
- Create a base logger to enable finer log level to be available to
the user
  • Loading branch information
Eclipse-Dominator committed Jun 9, 2023
1 parent 2215c93 commit 0c8720c
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/main/java/seedu/address/commons/core/LogsCenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,31 @@
public class LogsCenter {
private static final int MAX_FILE_COUNT = 5;
private static final int MAX_FILE_SIZE_IN_BYTES = (int) (Math.pow(2, 20) * 5); // 5MB
private static final String BASE_LOGGER_NAME = "seedu.address";
private static final String LOG_FILE = "addressbook.log";
private static Level currentLogLevel = Level.INFO;
private static final Logger logger = LogsCenter.getLogger(LogsCenter.class);
private static FileHandler fileHandler;
private static ConsoleHandler consoleHandler;
private static final Logger baseLogger = Logger.getLogger(BASE_LOGGER_NAME);

/**
* Initializes with a custom log level (specified in the {@code config} object)
* Loggers obtained *AFTER* this initialization will have their logging level changed<br>
* Logging levels for existing loggers will only be updated if the logger with the same name
* is requested again from the LogsCenter.
* Initializes with a custom log level (specified in the {@code config} object). All Loggers
* created using {@link #getLogger(String)} and {@link #getLogger(Class)} will have the same log
* level as the one specified in the {@code config} object.
*/
public static void init(Config config) {
currentLogLevel = config.getLogLevel();
logger.info("currentLogLevel: " + currentLogLevel);
baseLogger.setLevel(currentLogLevel);

// Set the log level of log handlers to the current log level if they are not null
if (fileHandler != null) {
fileHandler.setLevel(currentLogLevel);
}
if (consoleHandler != null) {
consoleHandler.setLevel(currentLogLevel);
}
}

/**
Expand All @@ -56,7 +66,15 @@ public static Logger getLogger(String name) {
*/
public static <T> Logger getLogger(Class<T> clazz) {
requireNonNull(clazz);
return getLogger(clazz.getSimpleName());
// The BASE_LOGGER_NAME acts as a common prefix for all application loggers,
// organizing them based on functionality or module.
// The dot (".") serves as a separator, reflecting the package structure and providing a
// hierarchical representation.

// Changing the BASE_LOGGER_NAME affects all related loggers, enabling centralized control
// over their configuration and behavior. It allows for easy adjustment of multiple loggers
// simultaneously.
return getLogger(BASE_LOGGER_NAME + "." + clazz.getSimpleName());
}

/**
Expand Down

0 comments on commit 0c8720c

Please sign in to comment.