Add cached writer. (#3287)

* Add cached writer so we need not call whole method.

* Make writer volatile.

* Make writer to WRITER.

* Rewrite the writeFactory for add the cached writer and keep the unit tests stable.

* Give the default writer.

* Remove volatile.
This commit is contained in:
Alan Lau 2019-08-22 10:03:54 +08:00 committed by 吴晟 Wu Sheng
parent c2663c08cc
commit a71c3abef6
1 changed files with 30 additions and 16 deletions

View File

@ -25,23 +25,37 @@ import org.apache.skywalking.apm.agent.core.conf.Config;
import org.apache.skywalking.apm.agent.core.conf.SnifferConfigInitializer;
import org.apache.skywalking.apm.util.StringUtil;
/**
* @author Alan Lau
*/
public class WriterFactory {
public static IWriter getLogWriter() {
if (!useConsole() && SnifferConfigInitializer.isInitCompleted() && AgentPackagePath.isPathFound()) {
if (StringUtil.isEmpty(Config.Logging.DIR)) {
try {
Config.Logging.DIR = AgentPackagePath.getPath() + "/logs";
} catch (AgentPackageNotFoundException e) {
e.printStackTrace();
}
}
return FileWriter.get();
} else {
return SystemOutWriter.INSTANCE;
}
}
private static boolean useConsole() {
return LogOutput.CONSOLE == Config.Logging.OUTPUT;
private static IWriter WRITER;
public static IWriter getLogWriter() {
switch (Config.Logging.OUTPUT) {
case FILE:
if (WRITER != null) {
return WRITER;
}
if (SnifferConfigInitializer.isInitCompleted() && AgentPackagePath.isPathFound()) {
if (StringUtil.isEmpty(Config.Logging.DIR)) {
try {
Config.Logging.DIR = AgentPackagePath.getPath() + "/logs";
} catch (AgentPackageNotFoundException e) {
e.printStackTrace();
}
}
WRITER = FileWriter.get();
} else {
return SystemOutWriter.INSTANCE;
}
break;
default:
return SystemOutWriter.INSTANCE;
}
return WRITER;
}
}