parent
6a53cfdd7d
commit
668fc24fdd
@ -0,0 +1,29 @@
|
||||
package com.tencent.cloud.polaris.config.logger;
|
||||
|
||||
/**
|
||||
* @author juanyinyang
|
||||
*/
|
||||
public enum Level {
|
||||
|
||||
TRACE("TRACE"), DEBUG("DEBUG"), INFO("INFO"), WARN("WARN"), ERROR("ERROR"),FATAL("FATAL"),OFF("OFF");
|
||||
|
||||
private String level;
|
||||
|
||||
Level(String level) {
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
public String getLevel() {
|
||||
return level;
|
||||
}
|
||||
|
||||
|
||||
public static Level levelOf(String level) {
|
||||
for (Level l : Level.values()) {
|
||||
if (l.level.equalsIgnoreCase(level)) {
|
||||
return l;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.tencent.cloud.polaris.config.logger;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
||||
import org.springframework.boot.logging.LoggingSystem;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
|
||||
/**
|
||||
* @author juanyinyang
|
||||
*/
|
||||
public class PolarisConfigLoggerApplicationListener implements ApplicationListener<ApplicationEvent> {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(PolarisConfigLoggerApplicationListener.class);
|
||||
|
||||
/**
|
||||
* @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
|
||||
*/
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationEvent event) {
|
||||
try {
|
||||
// Initialize application loggingSystem.
|
||||
if (event instanceof ApplicationStartedEvent) {
|
||||
ApplicationStartedEvent startedEvent = (ApplicationStartedEvent) event;
|
||||
ClassLoader classLoader = startedEvent.getSpringApplication().getClassLoader();
|
||||
LoggingSystem loggingSystem = LoggingSystem.get(classLoader);
|
||||
LOGGER.info("PolarisConfigLoggerApplicationListener onApplicationEvent init loggingSystem:{}", loggingSystem);
|
||||
PolarisConfigLoggerContext.setLogSystem(loggingSystem);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("PolarisConfigLoggerApplicationListener onApplicationEvent exception:", e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright (c) 2018 www.tencent.com.
|
||||
* All Rights Reserved.
|
||||
* This program is the confidential and proprietary information of
|
||||
* www.tencent.com ("Confidential Information"). You shall not disclose such
|
||||
* Confidential Information and shall use it only in accordance with
|
||||
* the terms of the license agreement you entered into with www.tencent.com.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.polaris.config.logger;
|
||||
|
||||
import static org.springframework.boot.logging.LoggingSystem.ROOT_LOGGER_NAME;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.logging.LogLevel;
|
||||
import org.springframework.boot.logging.LoggingSystem;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
|
||||
/**
|
||||
* @author juanyinyang
|
||||
*/
|
||||
public final class PolarisConfigLoggerContext {
|
||||
|
||||
private static LoggingSystem loggingSystem;
|
||||
|
||||
private PolarisConfigLoggerContext() {
|
||||
|
||||
}
|
||||
|
||||
protected static void setLogSystem(LoggingSystem logSystem) {
|
||||
Assert.notNull(logSystem,"Logging System should not be null");
|
||||
PolarisConfigLoggerContext.loggingSystem = logSystem;
|
||||
}
|
||||
|
||||
public static void setLevel(String loggerName, String level) {
|
||||
if (loggingSystem == null) {
|
||||
printLog("[SCT Config] PolarisConfigLoggerContext logger: ["+loggerName+"] change to target level fail. caused by internal exception:" + level,Level.WARN);
|
||||
return;
|
||||
}
|
||||
Level loggerLevel = Level.levelOf(level);
|
||||
if (loggerLevel == null) {
|
||||
printLog("[SCT Config] PolarisConfigLoggerContext logger: ["+loggerName+"] change to target level fail. caused by level is not support, level:" + level,Level.WARN);
|
||||
return;
|
||||
}
|
||||
LogLevel logLevel = null;
|
||||
switch (loggerLevel) {
|
||||
case TRACE:
|
||||
logLevel = LogLevel.TRACE;
|
||||
break;
|
||||
case DEBUG:
|
||||
logLevel = LogLevel.DEBUG;
|
||||
break;
|
||||
case OFF:
|
||||
logLevel = LogLevel.OFF;
|
||||
break;
|
||||
case INFO:
|
||||
logLevel = LogLevel.INFO;
|
||||
break;
|
||||
case WARN:
|
||||
logLevel = LogLevel.WARN;
|
||||
break;
|
||||
case ERROR:
|
||||
logLevel = LogLevel.ERROR;
|
||||
break;
|
||||
case FATAL:
|
||||
logLevel = LogLevel.FATAL;
|
||||
break;
|
||||
default:
|
||||
printLog("[SCT Config] PolarisConfigLoggerContext logger: ["+loggerName+"] setLevel fail. caused by level is not support, level: " + level,Level.WARN);
|
||||
}
|
||||
loggingSystem.setLogLevel(loggerName,logLevel);
|
||||
printLog("[SCT Config] PolarisConfigLoggerContext logger: ["+loggerName+"] changed to level:" + level,Level.INFO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 打印日志
|
||||
* @param message
|
||||
* @param level
|
||||
*/
|
||||
private static void printLog(String message, Level level){
|
||||
Logger logger = LoggerFactory.getLogger(ROOT_LOGGER_NAME);
|
||||
if (level.ordinal() <= Level.INFO.ordinal() ) {
|
||||
if (logger != null) {
|
||||
logger.info(message);
|
||||
} else {
|
||||
StdLog.info(message);
|
||||
}
|
||||
} else {
|
||||
if (logger != null) {
|
||||
logger.warn(message);
|
||||
} else {
|
||||
StdLog.warn(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
package com.tencent.cloud.polaris.config.logger;
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.util.Calendar;
|
||||
|
||||
/**
|
||||
* internal output logger
|
||||
* @author juanyinyang
|
||||
*/
|
||||
public class StdLog {
|
||||
|
||||
private static final String CLASS_INFO = StdLog.class.getName();
|
||||
|
||||
|
||||
protected static boolean debugEnabled = false;
|
||||
|
||||
/**
|
||||
* enable info level log
|
||||
*/
|
||||
protected static boolean infoEnabled = true;
|
||||
|
||||
/**
|
||||
* quite mode will out put nothing
|
||||
*/
|
||||
protected static boolean quietMode = false;
|
||||
|
||||
|
||||
private static final String DEBUG_FIX = "StdLog:DEBUG: ";
|
||||
|
||||
private static final String INFO_FIX = "StdLog:INFO: ";
|
||||
|
||||
private static final String WARN_FIX = "StdLog:WARN: ";
|
||||
|
||||
public static void setQuietMode(boolean quietMode) {
|
||||
StdLog.quietMode = quietMode;
|
||||
}
|
||||
|
||||
public static void setInfoEnabled(boolean infoEnabled ){
|
||||
StdLog.infoEnabled = infoEnabled;
|
||||
}
|
||||
|
||||
public static void setDebugEnabled(boolean debugEnabled) {
|
||||
StdLog.debugEnabled = debugEnabled;
|
||||
}
|
||||
|
||||
public static void debug(String msg) {
|
||||
|
||||
if (debugEnabled && !quietMode) {
|
||||
println(System.out,DEBUG_FIX + msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void info(String msg) {
|
||||
if (infoEnabled && !quietMode) {
|
||||
println(System.out,INFO_FIX + msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void warn(String msg) {
|
||||
if (infoEnabled && !quietMode) {
|
||||
println(System.err,WARN_FIX + msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void warn(String msg, Throwable t) {
|
||||
if (quietMode) {
|
||||
return;
|
||||
}
|
||||
println(System.err,WARN_FIX + msg);
|
||||
if (t != null) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void println(PrintStream out, String msg) {
|
||||
out.println(Calendar.getInstance().getTime().toString() + " " + CLASS_INFO + " " + msg);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue