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

Enhanced logger #502

Merged
merged 15 commits into from
Jun 1, 2018
8 changes: 8 additions & 0 deletions modApiServer/src/org/aion/api/server/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ public Map<String, CompiledContr> contract_compileSolidity(final String _contrac
Compiler.Result res = solc.compile(_contract.getBytes(), Compiler.Options.ABI, Compiler.Options.BIN);
if (res.isFailed()) {
LOG.info("contract compile error: [{}]", res.errors);

/**
* Enhance performance by separating the log threads and kernel
* TODO: Implement a queue for strings
* TODO: Put every LOG message onto the queue
* TODO: Use a thread service to process these message
*/

CompiledContr ret = new CompiledContr();
ret.error = res.errors;
compiledContracts.put("compile-error", ret);
Expand Down
42 changes: 26 additions & 16 deletions modBoot/src/org/aion/Aion.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
* If not, see <https://www.gnu.org/licenses/>.
*
* Contributors to the aion source files in decreasing order of code volume:
*
*
* Aion foundation.
*
*
******************************************************************************/

package org.aion;
Expand Down Expand Up @@ -74,31 +74,41 @@ public static void main(String args[]) throws InterruptedException {
if(cfg.fromXML())
cfg.toXML(new String[]{ "--id=" + cfg.getId() });


try {
ServiceLoader.load(AionLoggerFactory.class);
} catch (Exception e) {
System.out.println("load AionLoggerFactory service fail!" + e.toString());
throw e;
}


// If commit this out, the config setting will be ignore. all log module been set to "INFO" Level
AionLoggerFactory.init(cfg.getLog().getModules());
Logger LOG = AionLoggerFactory.getLogger(LogEnum.GEN.toString());

System.out.println(
" _____ \n" +
System.out.println(
" _____ \n" +
" .'. | .~ ~. |.. |\n" +
" .' `. | | | | ``.. |\n" +
" .''''''''`. | | | | ``.. |\n" +
".' `. | `._____.' | ``|\n\n" +
" NETWORK v" + KERNEL_VERSION +
"\n\n"
);
"\n\n"
);

/** Outputs relevant logger configuration */
if (!cfg.getLog().getLogFile()) {
System.out.println("Logger disabled; to enable please check log settings in config.xml\n");
} else if (!cfg.getLog().isValidPath() && cfg.getLog().getLogFile()) {
System.out.println("File path is invalid; please check log setting in config.xml\n");
return;
} else if (cfg.getLog().isValidPath() && cfg.getLog().getLogFile()) {
System.out.println("Logger file path: '" + cfg.getLog().getLogPath() + "'\n");
}

// If commit this out, the config setting will be ignore. all log module been set to "INFO" Level
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not clear what that comment is mean to say

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed comment

/** Changed INITIALIZE signature to include LOGFILE and LOGPATH*/
AionLoggerFactory.init(cfg.getLog().getModules(), cfg.getLog().getLogFile(), cfg.getLog().getLogPath());
Logger LOG = AionLoggerFactory.getLogger(LogEnum.GEN.toString());

IAionChain ac = AionFactory.create();

IMineRunner nm = null;

if (!cfg.getConsensus().isSeed()) {
Expand Down Expand Up @@ -145,7 +155,7 @@ class ShutdownThreadHolder {
final IMineRunner miner;
final ProtocolProcessor pp;
final NanoServer rpc;

private ShutdownThreadHolder(Thread zmqThread, IMineRunner nm, ProtocolProcessor pp, NanoServer rpc) {
this.zmqThread = zmqThread;
this.miner = nm;
Expand All @@ -155,7 +165,7 @@ private ShutdownThreadHolder(Thread zmqThread, IMineRunner nm, ProtocolProcessor
}

ShutdownThreadHolder holder = new ShutdownThreadHolder(zmqThread, nm, processor, rpcServer);

Runtime.getRuntime().addShutdownHook(new Thread(() -> {

LOG.info("Starting shutdown process...");
Expand Down Expand Up @@ -193,4 +203,4 @@ private ShutdownThreadHolder(Thread zmqThread, IMineRunner nm, ProtocolProcessor

}, "shutdown"));
}
}
}
146 changes: 106 additions & 40 deletions modLogger/src/org/aion/log/AionLoggerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,49 +19,54 @@
*
* Contributors:
* Aion foundation.
*
*
******************************************************************************/

package org.aion.log;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.ConsoleAppender;
import ch.qos.logback.core.rolling.RollingFileAppender;
import ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy;
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy;
import ch.qos.logback.core.rolling.helper.FileNamePattern;
import ch.qos.logback.core.util.FileSize;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.ConsoleAppender;
import ch.qos.logback.core.FileAppender;
import ch.qos.logback.core.rolling.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;

/**
* Used to override SimpleLogger current log level
*
* final public int TRACE_INT = 00; final public int DEBUG_INT = 10;
* finalConcurrentHashMap public int INFO_INT = 20; final public int WARN_INT =
* 30; final public int ERROR_INT = 40;
*
* Default set to 50 which ignore output
*
* <p>final public int TRACE_INT = 00; final public int DEBUG_INT = 10; finalConcurrentHashMap
* public int INFO_INT = 20; final public int WARN_INT = 30; final public int ERROR_INT = 40;
*
* <p>Default set to 50 which ignore output
*/

public class AionLoggerFactory {

/**
* Due to Cfg is abstract, use this static atribute to hold muti-chains
* config attribute List<CfgLogModule>, which is chain neural.
* Due to Cfg is abstract, use this static attribute to hold muti-chains config attribute
* List<CfgLogModule>, which is chain neural.
*/
private static Map<String, String> logModules;

private static LoggerContext loggerContext;
private static ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<>();
private final static PatternLayoutEncoder encoder = new PatternLayoutEncoder();
private static final PatternLayoutEncoder encoder = new PatternLayoutEncoder();

/** Static declaration of logFile */
private static boolean logFile;
private static String logPath;
private static RollingFileAppender fileAppender;

static {
logModules = new HashMap<>();
String level = LogLevels.INFO.name();
Expand All @@ -70,12 +75,67 @@ public class AionLoggerFactory {
}
}

/** Change INITIALIZE signature to include LOGFILE and LOGPATH */
public static void init(final Map<String, String> _logModules) {
init(_logModules, false, "log");
}

public static void init(final Map<String, String> _logModules, boolean _logToFile, String _logToPath) {

logModules = _logModules;
logFile = _logToFile;
logPath = _logToPath;

loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();

/** Toggles file appending configurations */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you switch all the comments that use the java doc notation /** to the comment notation /* throughout all the files of the PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the comment notation and added the license headers to relevant files

if (logFile) {
/** Initialize Rolling-File-Appender */
String fileName = logPath + "/aionCurrentLog.dat";
fileAppender = new RollingFileAppender();
fileAppender.setContext(loggerContext);
fileAppender.setName("aionlogger");
fileAppender.setFile(fileName);

/** Initialize Triggering-Policy (CONDITION) */
SizeBasedTriggeringPolicy tp = new SizeBasedTriggeringPolicy();
tp.setContext(loggerContext);
tp.start();

/** Initialize Rolling-Policy (BEHAVIOUR) */
SizeAndTimeBasedRollingPolicy rp = new SizeAndTimeBasedRollingPolicy();
rp.setContext(loggerContext);

/**
* To modify period of each rollover;
* https://logback.qos.ch/manual/appenders.html#TimeBasedRollingPolicy
* (Currently set to PER DAY)
*/
FileNamePattern fnp =
new FileNamePattern(
logPath + "/%d{yyyy/MM, aux}/aion.%d{yyyy-MM-dd}.%i.log", loggerContext);
rp.setFileNamePattern(fnp.getPattern());

/**
* To modify size of each rollover file;
* https://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
* (Currently set to 100MB)
*/
rp.setMaxFileSize(new FileSize(100 * 1000 * 1000));
rp.setParent(fileAppender);
rp.start();

/** Sets TRIGGER & ROLLING policy */
fileAppender.setTriggeringPolicy(tp);
fileAppender.setRollingPolicy(rp);

/** Set fileAppender configurations */
fileAppender.setContext(loggerContext);
fileAppender.setEncoder(encoder);
fileAppender.setAppend(true);
fileAppender.start();
}

encoder.setContext(loggerContext);
encoder.setPattern("%date{yy-MM-dd HH:mm:ss.SSS} %-5level %-4c [%thread]: %message%n");
encoder.start();
Expand All @@ -88,7 +148,8 @@ public static void init(final Map<String, String> _logModules) {
rootlogger.detachAndStopAllAppenders();
}

private static ConcurrentMap<String, Logger> loggerMap = new ConcurrentHashMap<String, Logger>();
private static ConcurrentMap<String, Logger> loggerMap =
new ConcurrentHashMap<String, Logger>();

public static Logger getLogger(String label) {

Expand All @@ -101,40 +162,45 @@ private static Logger newLogger(String label) {
if (loggerContext == null) {
// System.out.println("If you see this line, meaning you are under
// the unit test!!! If you are not. should report an issue.");
// init(new HashMap<>(), false);
init(new HashMap<>());
}

ch.qos.logback.classic.Logger newlogger = loggerContext.getLogger(label);
newlogger.addAppender(appender);

/** Toggles file appending */
if (logFile) {
newlogger.addAppender(fileAppender);
}

boolean flag = false;
Iterator<Entry<String, String>> it = logModules.entrySet().iterator();
while (it.hasNext()) {
Entry<String, String> logModule = it.next();
if (logModule.getKey().equals(label)) {
LogLevels logLevel = LogLevels.valueOf(logModule.getValue());
switch (logLevel) {
case TRACE:
newlogger.setLevel(Level.TRACE);
flag = true;
break;
case ERROR:
newlogger.setLevel(Level.ERROR);
flag = true;
break;
case INFO:
newlogger.setLevel(Level.INFO);
flag = true;
break;
case DEBUG:
newlogger.setLevel(Level.DEBUG);
flag = true;
break;
case TRACE:
newlogger.setLevel(Level.TRACE);
flag = true;
break;
case ERROR:
newlogger.setLevel(Level.ERROR);
flag = true;
break;
case INFO:
newlogger.setLevel(Level.INFO);
flag = true;
break;
case DEBUG:
newlogger.setLevel(Level.DEBUG);
flag = true;
break;
}
}

if (flag)
break;
if (flag) break;
}

if (!flag) {
Expand Down
Loading