org.zodiac.sdk.log.LogLog Maven / Gradle / Ivy
package org.zodiac.sdk.log;
import java.util.HashMap;
import java.util.Map;
import org.zodiac.sdk.log.constants.LogConstants;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;
/**
* This class used to output log statements from within the platform log package.
*
* And also it is used to output log statements to console appender.
*
*/
public class LogLog {
private static final String DEBUG_PREFIX = "Platform-Log:DEBUG ";
private static final String INFO_PREFIX = "Platform-Log:INFO ";
private static final String ERR_PREFIX = "Platform-Log:ERROR ";
private static final String WARN_PREFIX = "Platform-Log:WARN ";
private static final Map LEVELS = new HashMap<>();
private volatile static Level consoleLogLevel = Level.WARN;
static {
LEVELS.put("DEBUG", Level.DEBUG);
LEVELS.put("INFO", Level.INFO);
LEVELS.put("WARN", Level.WARN);
LEVELS.put("ERROR", Level.ERROR);
}
public static void setConsoleLevel(String level) {
Level value = LEVELS.get(level.toUpperCase());
if (StrUtil.isNotBlank(level) && value != null) {
consoleLogLevel = value;
}
}
public static void debug(String msg) {
if (isDebug()) {
System.out.println(DEBUG_PREFIX + msg);
}
}
public static void info(String msg) {
if (isInfo()) {
System.out.println(INFO_PREFIX + msg);
}
}
public static void warn(String msg) {
if (isWarn()) {
System.err.println(WARN_PREFIX + msg);
}
}
public static void warn(String msg, Throwable e) {
if (isWarn()) {
System.err.println(WARN_PREFIX + msg);
if (e != null) {
e.printStackTrace();
}
}
}
public static void error(String msg, Throwable throwable) {
if (isError()) {
System.err.println(ERR_PREFIX + msg);
if (throwable != null) {
throwable.printStackTrace();
}
}
}
private static boolean isDebug() {
setConsoleLevel(System.getProperty(LogConstants.LOG_INTERNAL_LEVEL, "WARN"));
return consoleLogLevel.equals(Level.DEBUG);
}
private static boolean isInfo() {
return isDebug() || consoleLogLevel.equals(Level.INFO);
}
private static boolean isWarn() {
return isInfo() || consoleLogLevel.equals(Level.WARN);
}
private static boolean isError() {
return isWarn() || consoleLogLevel.equals(Level.ERROR);
}
enum Level {
DEBUG, INFO, WARN, ERROR
}
}