Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polish logging SPI related code and add general JUL adapter for Logger SPI #1338

Merged
merged 1 commit into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,77 +15,73 @@
*/
package com.alibaba.csp.sentinel.log;

import java.util.Iterator;
import java.util.ServiceLoader;
import com.alibaba.csp.sentinel.log.jul.JavaLoggingAdapter;

/**
* Logger for command center.
*
* @author Eric Zhao
*/
public class CommandCenterLog {
private static com.alibaba.csp.sentinel.log.Logger log = null;

public static final String LOGGER_NAME = "sentinelCommandCenterLogger";
public static final String DEFAULT_LOG_FILENAME = "command-center.log";

private static com.alibaba.csp.sentinel.log.Logger logger = null;

static {
ServiceLoader<Logger> load = ServiceLoader.load(Logger.class);
Logger logger = null;
Iterator<Logger> iterator = load.iterator();
while (iterator.hasNext()) {
Logger next = iterator.next();
LogTarget annotation = next.getClass().getAnnotation(LogTarget.class);
if (annotation == null) {
continue;
try {
// Load user-defined logger implementation first.
logger = LoggerSpiProvider.getLogger(LOGGER_NAME);
if (logger == null) {
// If no customized loggers are provided, we use the default logger based on JUL.
logger = new JavaLoggingAdapter(LOGGER_NAME, DEFAULT_LOG_FILENAME);
}
String value = annotation.value().name();
if (value.equals(LogType.COMMAND_CENTER_LOG.name())) {
logger = next;
break;
}
}
// Use user implementations.
if (logger != null) {
log = logger;
} else {
// Use default implementations.
log = new CommandCenterLogLogging();
} catch (Throwable t) {
System.err.println("Error: failed to initialize Sentinel CommandCenterLog");
t.printStackTrace();
}
}

public static void info(String format, Object... arguments) {
log.info(format, arguments);
logger.info(format, arguments);
}

public static void info(String msg, Throwable e) {
log.info(msg, e);
logger.info(msg, e);
}

public static void warn(String format, Object... arguments) {
log.warn(format, arguments);
logger.warn(format, arguments);
}

public static void warn(String msg, Throwable e) {
log.warn(msg, e);
logger.warn(msg, e);
}

public static void trace(String format, Object... arguments) {
log.trace(format, arguments);
logger.trace(format, arguments);
}

public static void trace(String msg, Throwable e) {
log.trace(msg, e);
logger.trace(msg, e);
}

public static void debug(String format, Object... arguments) {
log.debug(format, arguments);
logger.debug(format, arguments);
}

public static void debug(String msg, Throwable e) {
log.debug(msg, e);
logger.debug(msg, e);
}

public static void error(String format, Object... arguments) {
log.error(format, arguments);
logger.error(format, arguments);
}

public static void error(String msg, Throwable e) {
log.error(msg, e);
logger.error(msg, e);
}

private CommandCenterLog() {}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,14 @@
*/
package com.alibaba.csp.sentinel.log;

import com.alibaba.csp.sentinel.util.PidUtil;

import java.io.File;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

import static com.alibaba.csp.sentinel.util.ConfigUtil.addSeparator;

/**
* <p>The base class for logging.</p>
* <p>The base config class for logging.</p>
*
* <p>
* The default log base directory is {@code ${user.home}/logs/csp/}. We can use the {@link #LOG_DIR}
Expand All @@ -36,7 +31,8 @@
* In this case, {@link #LOG_NAME_USE_PID} property could be configured as "true" to turn on this switch.
* </p>
*
* @author leyou
* @author Carpenter Lee
* @author Eric Zhao
*/
public class LogBase {

Expand Down Expand Up @@ -66,15 +62,15 @@ public class LogBase {

static {
try {
initialize();
initializeDefault();
loadProperties();
} catch (Throwable t) {
System.err.println("[LogBase] FATAL ERROR when initializing log class");
System.err.println("[LogBase] FATAL ERROR when initializing logging config");
t.printStackTrace();
}
}

private static void initialize() {
private static void initializeDefault() {
logNameUsePid = false;
logOutputType = LOG_OUTPUT_TYPE_FILE;
logBaseDir = addSeparator(System.getProperty(USER_HOME)) + DIR_NAME + File.separator;
Expand Down Expand Up @@ -104,7 +100,6 @@ private static void loadProperties() {
}
System.out.println("INFO: log base dir is: " + logBaseDir);


String usePid = properties.getProperty(LOG_NAME_USE_PID);
logNameUsePid = "true".equalsIgnoreCase(usePid);
System.out.println("INFO: log name use pid is: " + logNameUsePid);
Expand Down Expand Up @@ -147,64 +142,4 @@ public static String getLogCharset() {
return logCharSet;
}

protected static void log(Logger logger, Handler handler, Level level, String detail, Object... params) {
if (detail == null) {
return;
}
LoggerUtils.disableOtherHandlers(logger, handler);

FormattingTuple formattingTuple = MessageFormatter.arrayFormat(detail, params);
String message = formattingTuple.getMessage();
logger.log(level, message);
}

protected static void log(Logger logger, Handler handler, Level level, String detail, Throwable throwable) {
if (detail == null) {
return;
}
LoggerUtils.disableOtherHandlers(logger, handler);
logger.log(level, detail, throwable);
}


protected static Handler makeLogger(String logName, Logger heliumRecordLog) {
CspFormatter formatter = new CspFormatter();

Handler handler = null;

// Create handler according to logOutputType, set formatter to CspFormatter, set encoding to LOG_CHARSET
switch (logOutputType) {
case LOG_OUTPUT_TYPE_FILE:
String fileName = LogBase.getLogBaseDir() + logName;
if (isLogNameUsePid()) {
fileName += ".pid" + PidUtil.getPid();
}
try {
handler = new DateFileLogHandler(fileName + ".%d", 1024 * 1024 * 200, 4, true);
handler.setFormatter(formatter);
handler.setEncoding(logCharSet);
} catch (IOException e) {
e.printStackTrace();
}
break;
case LOG_OUTPUT_TYPE_CONSOLE:
try {
handler = new ConsoleHandler();
handler.setFormatter(formatter);
handler.setEncoding(logCharSet);
} catch (IOException e) {
e.printStackTrace();
}
break;
default:
break;
}

if (handler != null) {
LoggerUtils.disableOtherHandlers(heliumRecordLog, handler);
}
heliumRecordLog.setLevel(Level.ALL);
return handler;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@

/**
* @author xue8
* @since 1.7.2
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface LogTarget {
/**
* Returns the kinds of log type.
* @return Returns the kinds of log type
* Returns the logger name.
*
* @return the logger name. Record logger by default
*/
LogType value() default LogType.RECORD_LOG;
String value() default RecordLog.LOGGER_NAME;
}

This file was deleted.

Loading