Skip to content

Commit

Permalink
merge from master
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonjoo2010 authored and jason-joo committed Oct 9, 2018
2 parents 3df907e + a25f25b commit 6ba04b6
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ private T configLogFilePath(String filePathToConfig, String basePath) {
return (T)this;
}

@SuppressWarnings("unchecked")
public T configLogFilePath(String filePath) {
EagleEyeCoreUtils.checkNotNullEmpty(filePath, "filePath");
this.filePath = filePath;
return (T)this;
}

@SuppressWarnings("unchecked")
public T maxFileSizeMB(long maxFileSizeMB) {
if (maxFileSize < 10) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,14 @@
*/
public class CommandCenterLog extends LogBase {

private static final Logger heliumRecordLog = Logger.getLogger("cspMetricLog");
private static final String FILE_NAME = "metricStat.log";
private static final Logger heliumRecordLog = Logger.getLogger("cspCommandCenterLog");
private static final String FILE_NAME = "commandCenter.log";
private static Handler logHandler = null;

static {
logHandler = makeLogger(FILE_NAME, heliumRecordLog);
}

/**
* Change log dir, the dir will be created if not exits
*/
public static void resetLogBaseDir(String baseDir) {
setLogBaseDir(baseDir);
logHandler = makeLogger(FILE_NAME, heliumRecordLog);
}

public static void info(String detail, Object... params) {
log(heliumRecordLog, logHandler, Level.INFO, detail, params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,42 @@
import com.alibaba.csp.sentinel.util.PidUtil;

/**
*
* Default log base dir is ${user.home}, we can use {@link #LOG_DIR} System property to override it.
*
* @author leyou
*/
public class LogBase {
public static final String LOG_CHARSET = "utf-8";
private static final String DIR_NAME = "logs" + File.separator + "csp";
private static final String USER_HOME = "user.home";
public static final String LOG_DIR = "csp.sentinel.log.dir";
private static String logBaseDir;

static {
String userHome = System.getProperty(USER_HOME);
setLogBaseDir(userHome);
// first use -D, then use user home.
String logDir = System.getProperty(LOG_DIR);

if (logDir == null || logDir.isEmpty()) {
logDir = System.getProperty(USER_HOME);
logDir = addSeparator(logDir) + DIR_NAME + File.separator;
}
logDir = addSeparator(logDir);
File dir = new File(logDir);
if (!dir.exists()) {
if (!dir.mkdirs()) {
System.err.println("ERROR: create log base dir error: " + logDir);
}
}
// logBaseDir must end with File.separator
logBaseDir = logDir;
System.out.println("INFO: log base dir is: " + logBaseDir);
}

private static String addSeparator(String logDir) {
if (!logDir.endsWith(File.separator)) {
logDir += File.separator;
}
return logDir;
}

protected static void log(Logger logger, Handler handler, Level level, String detail, Object... params) {
Expand Down Expand Up @@ -67,23 +91,6 @@ public static String getLogBaseDir() {
return logBaseDir;
}

/**
* Change log dir, the dir will be created if not exits
*
* @param baseDir
*/
protected static void setLogBaseDir(String baseDir) {
if (!baseDir.endsWith(File.separator)) {
baseDir += File.separator;
}
String path = baseDir + DIR_NAME + File.separator;
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
logBaseDir = path;
}

protected static Handler makeLogger(String logName, Logger heliumRecordLog) {
CspFormatter formatter = new CspFormatter();
String fileName = LogBase.getLogBaseDir() + logName + ".pid" + PidUtil.getPid();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,26 @@
*/
package com.alibaba.csp.sentinel.slots.logger;

import java.io.File;

import com.alibaba.csp.sentinel.eagleeye.EagleEye;
import com.alibaba.csp.sentinel.eagleeye.StatLogger;
import com.alibaba.csp.sentinel.log.LogBase;

public class EagleEyeLogUtil {

private static final String DIR_NAME = "csp";
private static final String FILE_NAME = "sentinel-block.log";
public static final String FILE_NAME = "sentinel-block.log";

private static StatLogger statLogger;

static {
String path = DIR_NAME + File.separator + FILE_NAME;
String path = LogBase.getLogBaseDir() + FILE_NAME;

statLogger = EagleEye.statLoggerBuilder("sentinel-block-record")
statLogger = EagleEye.statLoggerBuilder("sentinel-block-log")
.intervalSeconds(1)
.entryDelimiter('|')
.keyDelimiter(',')
.valueDelimiter(',')
.maxEntryCount(6000)
.baseLogFilePath(path)
.configLogFilePath(path)
.maxFileSizeMB(300)
.maxBackupIndex(3)
.buildSingleton();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
*/
package com.alibaba.csp.sentinel;

import java.io.File;

import com.alibaba.csp.sentinel.log.LogBase;
import com.alibaba.csp.sentinel.log.RecordLog;

import org.junit.Test;

import static org.junit.Assert.assertTrue;

/**
* @author xuyue
*/
Expand All @@ -38,4 +43,22 @@ public void testLogRolling() {
}
}

@Test
public void testChangeLogBase() {
String userHome = System.getProperty("user.home");
String newLogBase = userHome + File.separator + "tmpLogDir" + System.currentTimeMillis();
System.setProperty(LogBase.LOG_DIR, newLogBase);

RecordLog.info("testChangeLogBase");
String logFileName = RecordLog.getLogBaseDir();
File[] files = new File(logFileName).listFiles();
assertTrue(files != null && files.length > 0);
}

@Test
public void testLogBaseDir() {
RecordLog.info("testLogBaseDir");
assertTrue(RecordLog.getLogBaseDir().startsWith(System.getProperty("user.home")));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.alibaba.csp.sentinel.slots.logger;

import java.io.File;

import com.alibaba.csp.sentinel.log.LogBase;
import com.alibaba.csp.sentinel.log.RecordLog;

import org.junit.Test;

import static org.junit.Assert.*;

/**
* @author Carpenter Lee
*/
public class EagleEyeLogUtilTest {

@Test
public void testWriteLog() throws Exception {
EagleEyeLogUtil.log("resourceName", "BlockException", "app1", "origin", 1);
Thread.sleep(1100);
String file = RecordLog.getLogBaseDir() + EagleEyeLogUtil.FILE_NAME;
assertTrue(new File(file).exists());
}

@Test
public void testChangeLogBase() throws Exception {
String userHome = System.getProperty("user.home");
String newLogBase = userHome + File.separator + "tmpLogDir" + System.currentTimeMillis();
System.setProperty(LogBase.LOG_DIR, newLogBase);

EagleEyeLogUtil.log("resourceName", "BlockException", "app1", "origin", 1);
Thread.sleep(1100);
String file = RecordLog.getLogBaseDir() + EagleEyeLogUtil.FILE_NAME;
assertTrue(new File(file).exists());
}
}
1 change: 1 addition & 0 deletions sentinel-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<module>sentinel-demo-apollo-datasource</module>
<module>sentinel-demo-annotation-spring-aop</module>
<module>sentinel-demo-parameter-flow-control</module>
<module>sentinel-demo-slot-chain-spi</module>
</modules>

<dependencies>
Expand Down
14 changes: 14 additions & 0 deletions sentinel-demo/sentinel-demo-slot-chain-spi/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>sentinel-demo</artifactId>
<groupId>com.alibaba.csp</groupId>
<version>0.2.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>sentinel-demo-slot-chain-spi</artifactId>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.
*/
package com.alibaba.csp.sentinel.demo.slot;

import com.alibaba.csp.sentinel.context.Context;
import com.alibaba.csp.sentinel.node.DefaultNode;
import com.alibaba.csp.sentinel.slotchain.AbstractLinkedProcessorSlot;
import com.alibaba.csp.sentinel.slotchain.ResourceWrapper;

/**
* An example slot that records current context and entry resource.
*
* @author Eric Zhao
*/
public class DemoSlot extends AbstractLinkedProcessorSlot<DefaultNode> {

@Override
public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, Object... args)
throws Throwable {
System.out.println("Current context: " + context.getName());
System.out.println("Current entry resource: " + context.getCurEntry().getResourceWrapper().getName());

fireEntry(context, resourceWrapper, node, count, args);
}

@Override
public void exit(Context context, ResourceWrapper resourceWrapper, int count, Object... args) {
System.out.println("Exiting for entry on DemoSlot: " + context.getCurEntry().getResourceWrapper().getName());

fireExit(context, resourceWrapper, count, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.
*/
package com.alibaba.csp.sentinel.demo.slot;

import com.alibaba.csp.sentinel.slotchain.ProcessorSlotChain;
import com.alibaba.csp.sentinel.slotchain.SlotChainBuilder;
import com.alibaba.csp.sentinel.slots.DefaultSlotChainBuilder;

/**
* An example slot chain builder. To activate this slot chain builder,
* add the class name to corresponding SPI file in `resource/META-INF/services` directory.
*
* @author Eric Zhao
*/
public class DemoSlotChainBuilder implements SlotChainBuilder {

@Override
public ProcessorSlotChain build() {
ProcessorSlotChain chain = new DefaultSlotChainBuilder().build();
chain.addLast(new DemoSlot());
return chain;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.
*/
package com.alibaba.csp.sentinel.demo.slot;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;

/**
* @author Eric Zhao
*/
public class SlotChainBuilderSpiDemo {

public static void main(String[] args) {
// You will see this in record.log, indicating that the custom slot chain builder is activated:
// [SlotChainProvider] Global slot chain builder resolved: com.alibaba.csp.sentinel.demo.slot.DemoSlotChainBuilder
Entry entry = null;
try {
entry = SphU.entry("abc");
} catch (BlockException ex) {
ex.printStackTrace();
} finally {
if (entry != null) {
entry.exit();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Custom slot chain builder
com.alibaba.csp.sentinel.demo.slot.DemoSlotChainBuilder

0 comments on commit 6ba04b6

Please sign in to comment.