-
Notifications
You must be signed in to change notification settings - Fork 116
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
Enhanced logger #502
Changes from 12 commits
2f0ac31
0d89dae
07c93e1
a2186ad
236dddf
0bdb509
67209d3
ba2050a
fda465f
651c408
79f5422
43d206b
1f0e586
6e70b12
b88dfa1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
|
@@ -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 */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you switch all the comments that use the java doc notation There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
@@ -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) { | ||
|
||
|
@@ -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) { | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed comment