Skip to content

Commit

Permalink
Merge pull request #239 from noob-noob/yangminglang
Browse files Browse the repository at this point in the history
optimize DataLoggerManager and PLogger
  • Loading branch information
zxy0728 authored Mar 30, 2018
2 parents ed19f92 + 8974cf3 commit 55b7ebf
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ feature.healthmanager.http.bqsize=10
feature.healthmanager.lifekeeper.enable=true
feature.healthmanager.lifekeeper.interval=15000
feature.healthmanager.lifekeeper.locktimeout=30000
feature.healthmanager.lifekeeper.apptimeout=15000
feature.healthmanager.lifekeeper.apptimeout=22000
feature.healthmanager.store.addr=127.0.0.1:6379
feature.healthmanager.store.concurrent.min=10
feature.healthmanager.store.concurrent.max=20
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ feature.healthmanager.http.bqsize=20
feature.healthmanager.lifekeeper.enable=true
feature.healthmanager.lifekeeper.interval=15000
feature.healthmanager.lifekeeper.locktimeout=30000
feature.healthmanager.lifekeeper.apptimeout=15000
feature.healthmanager.lifekeeper.apptimeout=22000
feature.healthmanager.store.addr=127.0.0.1:26379
feature.healthmanager.store.concurrent.min=50
feature.healthmanager.store.concurrent.max=150
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ feature.healthmanager.http.bqsize=5
feature.healthmanager.lifekeeper.enable=true
feature.healthmanager.lifekeeper.interval=15000
feature.healthmanager.lifekeeper.locktimeout=30000
feature.healthmanager.lifekeeper.apptimeout=15000
feature.healthmanager.lifekeeper.apptimeout=22000
feature.healthmanager.store.addr=127.0.0.1:6379
feature.healthmanager.store.concurrent.min=50
feature.healthmanager.store.concurrent.max=200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,11 @@ private static Level getLevel(LogLevel level) {
private FileHandler fileHandler = null;
private MemoryHandler memHandler = null;
private Level level = Level.INFO;

private boolean isEnableFileOutSus = false;
private boolean isEnableConsoleOutSus = false;

public PLogger(String name) {

log = Logger.getLogger(name);
log.setUseParentHandlers(false);
}
Expand All @@ -146,7 +149,15 @@ public void setLogLevel(LogLevel level) {
@Override
public void log(LogLevel level, String info, Object... objects) {

/**
* NOTE: only when enable is OK to record the log
*/
if (isEnableFileOutSus == false && isEnableConsoleOutSus == false) {
return;
}

Level l = getLevel(level);

log.log(l, info, objects);
}

Expand Down Expand Up @@ -187,7 +198,7 @@ public void finer(String info, Object... objects) {
}

@Override
public void enableConsoleOut(boolean check) {
public boolean enableConsoleOut(boolean check) {

if (check == true) {
if (this.consoleHandler == null) {
Expand All @@ -196,16 +207,20 @@ public void enableConsoleOut(boolean check) {
this.consoleHandler.setFormatter(new DefaultLogFormatter());
}
log.addHandler(this.consoleHandler);
isEnableConsoleOutSus = true;
}
else {
if (this.consoleHandler != null) {
log.removeHandler(this.consoleHandler);
}
isEnableConsoleOutSus = false;
}

return isEnableConsoleOutSus;
}

@Override
public void enableFileOut(String filepattern, boolean check, int bufferSize, int fileSize, int fileCount,
public boolean enableFileOut(String filepattern, boolean check, int bufferSize, int fileSize, int fileCount,
boolean isAppend, Formatter format) {

if (check == true) {
Expand All @@ -222,16 +237,20 @@ public void enableFileOut(String filepattern, boolean check, int bufferSize, int
/**
* NOTE: we use async log buffer
*/
if (this.memHandler == null) {
if (this.memHandler == null&& this.fileHandler != null) {
this.memHandler = new MemoryHandler(this.fileHandler, bufferSize, this.level);
this.log.addHandler(this.memHandler);
isEnableFileOutSus = true;
}
}
else {
if (this.memHandler != null) {
log.removeHandler(this.memHandler);
isEnableFileOutSus = false;
}
}

return isEnableFileOutSus;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public enum LogLevel {

public void setLogLevel(LogLevel level);

public void enableConsoleOut(boolean check);
public boolean enableConsoleOut(boolean check);

public void enableFileOut(String filepattern, boolean check, int bufferSize, int fileSize, int fileCount,
public boolean enableFileOut(String filepattern, boolean check, int bufferSize, int fileSize, int fileCount,
boolean isAppend, Formatter format);

public void log(LogLevel level, String info, Object... objects);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import com.creditease.agent.helpers.IOHelper;
Expand Down Expand Up @@ -314,17 +312,19 @@ private void startDefaultSupporters() {
*
* @param supporterClasses
* @param cl
*
* @return Supporter Status
*/
public Set<String> startSupporters(String[] supporterClasses, boolean needPersist) {
public Map<String, Object> startSupporters(String[] supporterClasses, boolean needPersist) {

ClassLoader cl = this.getClass().getClassLoader();

Set<String> existSupporters = new HashSet<String>();
Map<String, Object> supportersStatus = new HashMap<String, Object>();

for (String supporterClass : supporterClasses) {

if (supporters.containsKey(supporterClass)) {
existSupporters.add(supporterClass);
supportersStatus.put(supporterClass, "Exist");
continue;
}

Expand All @@ -346,21 +346,28 @@ public Set<String> startSupporters(String[] supporterClasses, boolean needPersis
if (log.isLogEnabled()) {
log.info("Supporter[" + supporterClass + "] starts SUCCESS");
}

supportersStatus.put(supporterClass, "OK");

if (needPersist) {
this.metaMgr.addSupporterMeta(supporterClasses);
}


}
catch (Exception e) {

supporters.remove(supporterClass);

supportersStatus.put(supporterClass, "Fail:" + e.toString());

log.error("Supporter[" + supporterClass + "] starts FAIL", e);
}
}

if (needPersist) {
this.metaMgr.addSupporterMeta(supporterClasses);
}


return existSupporters;
return supportersStatus;
}

public void stop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -79,9 +78,9 @@ private void controlSupporters(HttpServletRequest request, HttpServletResponse r

String retMsg = "OK";
if (isStart == true) {
Set<String> existSupporters = UAVServer.instance().startSupporters(supporterClasses, true);
if (existSupporters.size() > 0) {
retMsg = "已启动功能:" + JSONHelper.toString(existSupporters);
Map<String, Object> supportersStatus = UAVServer.instance().startSupporters(supporterClasses, true);
if (supportersStatus.size() > 0) {
retMsg = JSONHelper.toString(supportersStatus);
}

String tag = request.getParameter("tag");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class DataLogger {
private int fileCountLimit = 10;
private int logBufferSize = 200;
private String logRoot = ".";
private boolean isInitSus = true;

/**
*
Expand Down Expand Up @@ -69,15 +70,20 @@ public DataLogger(String name, String rootpath, String logFilePattern, int logBu
this.logRoot = rootpath;

if (!IOHelper.exists(logRoot)) {
IOHelper.createFolder(logRoot);
isInitSus = false;
return;
}

log.enableFileOut(this.logRoot + "/" + this.filePattern, true, this.logBufferSize, this.fileSizeLimit,
this.fileCountLimit, true, new SimpleLogFormatter());
isInitSus = log.enableFileOut(this.logRoot + "/" + this.filePattern, true, this.logBufferSize,
this.fileSizeLimit, this.fileCountLimit, true, new SimpleLogFormatter());
}

public void logData(String info) {


if (isInitSus == false) {
return;
}

this.log.info(info);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,33 @@ public class DataLoggerManager extends BaseComponent {
* uav.properties里面该DataLoggerManager所有配置项的前缀
*/
public DataLoggerManager(String logTypeName, String configPrefix) {

this.configPrefix = configPrefix;
this.logTypeName = logTypeName;
this.logTypeName = logTypeName;

String rootPath = getRootPath();
/**
* NOTE: check if DataLogger root path exists
*
* if not, try to create, if can't create, throw RuntimeException to stop Supporter
*
* This notify msg will be exposed to the user when he try to start this Supporter
*/
if (IOHelper.exists(rootPath)) {
return;
}

try {
int res = IOHelper.createFolder(rootPath);

if (res != 1) {
throw new RuntimeException("DataLogger RootPath No Auth to Create: rootpath=" + rootPath);
}
}
catch (Exception e) {
throw new RuntimeException(e);
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2981,8 +2981,8 @@ protected void loadCommonInfoFromWebXML(ProfileElement elem, Map<String, Descrip
inst.setValue("webapproot", webAppRoot);
// get the app Http URL
inst.setValue("appurl", getServiceURI(contextpath));
// get customized metrics
// getCustomizedMetrics(inst);
// get the vender
inst.setValue("vender", UAVServer.instance().getServerInfo(CaptureConstants.INFO_APPSERVER_VENDOR));
// get app group
getAppGroup(inst);
}
Expand All @@ -2999,47 +2999,6 @@ private void getAppGroup(ProfileElementInstance inst) {
inst.setValue("appgroup", JAppGroup);
}

// /**
// * getCustomizedMetrics
// *
// * @param inst
// */
// private void getCustomizedMetrics(ProfileElementInstance inst) {
//
// @SuppressWarnings("rawtypes")
// Map<String, Map> metrics = new HashMap<String, Map>();
//
// Enumeration<?> enumeration = System.getProperties().propertyNames();
//
// while (enumeration.hasMoreElements()) {
//
// String name = (String) enumeration.nextElement();
//
// int moIndex = name.indexOf("mo@");
//
// if (moIndex != 0) {
// continue;
// }
//
// try {
// String[] metricsArray = name.split("@");
//
// // add metricName to customizedMetrics
// if (metricsArray.length == 3) {
// metrics.put(metricsArray[1], JSONHelper.toObject(metricsArray[2], Map.class));
// }
// else {
// metrics.put(metricsArray[1], Collections.emptyMap());
// }
// }
// catch (Exception e) {
// logger.error("Parsing Custom Metrics[" + name + "] FAIL.", e);
// continue;
// }
// }
//
// inst.setValue("appmetrics", JSONHelper.toString(metrics));
// }

private String getServiceURI(String contextpath) {

Expand Down

0 comments on commit 55b7ebf

Please sign in to comment.