Skip to content

Commit

Permalink
Flexible loggers' support through SPI mechanism with name `com.alibab…
Browse files Browse the repository at this point in the history
…a.csp.sentinel.log.Logger` (#1265)

* There are two types of logger for command center and common modules specified by  annotation of `LogTarget`
* Add implementing examples in `sentinel-demo/sentinel-demo-log-logback`
* All implementations should support placeholder '{}'
  • Loading branch information
xue8 authored Feb 21, 2020
1 parent c1ff913 commit 5f203aa
Show file tree
Hide file tree
Showing 20 changed files with 1,476 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,77 @@
*/
package com.alibaba.csp.sentinel.log;

import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Iterator;
import java.util.ServiceLoader;

/**
* Logger for command center.
*/
public class CommandCenterLog extends LogBase {

private static final Logger heliumRecordLog = Logger.getLogger("cspCommandCenterLog");
private static final String FILE_NAME = "command-center.log";
private static Handler logHandler = null;
public class CommandCenterLog {
private static com.alibaba.csp.sentinel.log.Logger log = null;

static {
logHandler = makeLogger(FILE_NAME, heliumRecordLog);
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;
}
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();
}
}

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

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

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

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

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

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

public static void info(String detail, Object... params) {
log(heliumRecordLog, logHandler, Level.INFO, detail, params);
public static void debug(String format, Object... arguments) {
log.debug(format, arguments);
}

public static void info(String detail, Throwable e) {
log(heliumRecordLog, logHandler, Level.INFO, detail, e);
public static void debug(String msg, Throwable e) {
log.debug(msg, e);
}

public static void warn(String detail, Object... params) {
log(heliumRecordLog, logHandler, Level.WARNING, detail, params);
public static void error(String format, Object... arguments) {
log.error(format, arguments);
}

public static void warn(String detail, Throwable e) {
log(heliumRecordLog, logHandler, Level.WARNING, detail, e);
public static void error(String msg, Throwable e) {
log.error(msg, e);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 1999-2019 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.log;

import java.util.logging.Handler;
import java.util.logging.Logger;

/**
* Default logger implementation.
* @author xue8
*/
public class CommandCenterLogLogging extends LogBase implements com.alibaba.csp.sentinel.log.Logger {
private final Logger heliumRecordLog = Logger.getLogger("cspCommandCenterLog");
private final String FILE_NAME = "command-center.log";
private final Handler logHandler = makeLogger(FILE_NAME, heliumRecordLog);

@Override
public void info(String format, Object... arguments) {
log(heliumRecordLog, logHandler, Level.INFO, format, arguments);
}

@Override
public void info(String msg, Throwable e) {
log(heliumRecordLog, logHandler, Level.INFO, msg, e);
}

@Override
public void warn(String format, Object... arguments) {
log(heliumRecordLog, logHandler, Level.WARNING, format, arguments);
}

@Override
public void warn(String msg, Throwable e) {
log(heliumRecordLog, logHandler, Level.WARNING, msg, e);
}

@Override
public void trace(String format, Object... arguments) {
log(heliumRecordLog, logHandler, Level.TRACE, format, arguments);
}

@Override
public void trace(String msg, Throwable e) {
log(heliumRecordLog, logHandler, Level.TRACE, msg, e);
}

@Override
public void debug(String format, Object... arguments) {
log(heliumRecordLog, logHandler, Level.DEBUG, format, arguments);
}

@Override
public void debug(String msg, Throwable e) {
log(heliumRecordLog, logHandler, Level.DEBUG, msg, e);
}

@Override
public void error(String format, Object... arguments) {
log(heliumRecordLog, logHandler, Level.ERROR, format, arguments);
}

@Override
public void error(String msg, Throwable e) {
log(heliumRecordLog, logHandler, Level.ERROR, msg, e);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Copyright notice
* This code copy from SLF4J which licensed under the MIT License.
*
*/
package com.alibaba.csp.sentinel.log;

/**
* Holds the results of formatting done by {@link MessageFormatter}.
*
* @author Joern Huxhorn
*/
public class FormattingTuple {

static public FormattingTuple NULL = new FormattingTuple(null);

private String message;
private Throwable throwable;
private Object[] argArray;

public FormattingTuple(String message) {
this(message, null, null);
}

public FormattingTuple(String message, Object[] argArray, Throwable throwable) {
this.message = message;
this.throwable = throwable;
this.argArray = argArray;
}

public String getMessage() {
return message;
}

public Object[] getArgArray() {
return argArray;
}

public Throwable getThrowable() {
return throwable;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 1999-2019 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.log;

/**
* Logging levels
* @author xue8
*/
public class Level extends java.util.logging.Level {
private static final String defaultBundle = "sun.util.logging.resources.logging";

public static final Level ERROR = new Level("ERROR", 1000);
public static final Level WARNING = new Level("WARNING", 900);
public static final Level INFO = new Level("INFO", 800);
public static final Level DEBUG = new Level("DEBUG", 700);
public static final Level TRACE = new Level("TRACE", 600);

protected Level(String name, int value) {
super(name, value, defaultBundle);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,10 @@ protected static void log(Logger logger, Handler handler, Level level, String de
return;
}
LoggerUtils.disableOtherHandlers(logger, handler);
if (params.length == 0) {
logger.log(level, detail);
} else {
logger.log(level, detail, params);
}

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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 1999-2019 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.log;

import java.lang.annotation.*;

/**
* @author xue8
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface LogTarget {
/**
* Returns the kinds of log type.
* @return Returns the kinds of log type
*/
LogType value() default LogType.RECORD_LOG;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 1999-2019 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.log;

/**
* An enum marks log type.
* @author xue8
*/
public enum LogType {
COMMAND_CENTER_LOG,
RECORD_LOG,
}
Loading

0 comments on commit 5f203aa

Please sign in to comment.