Skip to content

Commit

Permalink
Log environment variables (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
attiasas authored Nov 1, 2024
1 parent 7359709 commit 1fb820e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
14 changes: 13 additions & 1 deletion src/main/java/org/ois/core/utils/log/ILogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
* warn, and error.
*/
public interface ILogger {
/** Environment variable used by OIS products to set the log level **/
String ENV_LOG_LEVEL = "OIS_LOG_LEVEL";
/** Environment variable used by OIS products to set the log topics **/
String ENV_LOG_TOPICS = "OIS_LOG_TOPICS";
/** The default level of the logger **/
Level DEFAULT_LEVEL = Level.Info;

/**
* Enumeration representing the different log levels.
*/
Expand All @@ -22,15 +29,20 @@ enum Level {
* @return the corresponding {@link Level} enum value
*/
static Level toLogLevel(String logLevel) {
if (logLevel == null) {
return DEFAULT_LEVEL;
}
switch (logLevel.trim().toLowerCase()) {
case "debug":
return Level.Debug;
case "info":
return Level.Info;
case "warn":
return Level.Warn;
case "error":
return Level.Error;
default:
return Level.Info;
return DEFAULT_LEVEL;
}
}

Expand Down
14 changes: 11 additions & 3 deletions src/main/java/org/ois/core/utils/log/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.ois.core.runner.RunnerConfiguration;

import java.util.*;
import java.util.stream.Collectors;

/**
* Logger implementation for logging messages with various severity levels.
Expand All @@ -16,12 +17,19 @@
public class Logger<T> implements ILogger {
private static final Map<Class, Logger> logMap = new HashMap<>();

private static final Set<String> allowedTopics = new HashSet<>();
private static final ILogger.Level DEFAULT_LEVEL = Level.Info;
private static int minLogLevel = DEFAULT_LEVEL.ordinal();
private static final Set<String> allowedTopics = getInitialTopics();
private static int minLogLevel = ILogger.toLogLevel(System.getenv(ENV_LOG_LEVEL)).ordinal();

private final Class<T> logClass;

private static Set<String> getInitialTopics() {
String topics = System.getenv(ENV_LOG_TOPICS);
if (topics == null) {
return new HashSet<>();
}
return Arrays.stream(topics.split(";")).collect(Collectors.toSet());
}

/**
* Private constructor for creating a logger instance.
*
Expand Down

0 comments on commit 1fb820e

Please sign in to comment.