-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a1cc95
commit a2d2487
Showing
7 changed files
with
206 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
src/main/java/io/github/kev1nst/jenkins/common/Log4j2Adapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package io.github.kev1nst.jenkins.common; | ||
|
||
/** | ||
* @author kevinzhao | ||
* @since 23/07/2019 | ||
*/ | ||
|
||
import org.apache.logging.log4j.Level; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.nutz.log.Log; | ||
import org.nutz.log.LogAdapter; | ||
import org.nutz.log.impl.AbstractLog; | ||
import org.nutz.plugin.Plugin; | ||
|
||
public class Log4j2Adapter implements LogAdapter, Plugin { | ||
|
||
@Override | ||
public boolean canWork() { | ||
try { | ||
org.apache.logging.log4j.Logger.class.getName(); | ||
return true; | ||
} | ||
catch (Throwable e) {} | ||
return false; | ||
} | ||
|
||
@Override | ||
public Log getLogger(String className) { | ||
return new Log4j2Adapter.Log4JLogger(className); | ||
} | ||
|
||
static class Log4JLogger extends AbstractLog { | ||
|
||
public static final String SUPER_FQCN = AbstractLog.class.getName(); | ||
public static final String SELF_FQCN = Log4j2Adapter.class.getName(); | ||
|
||
private Logger logger; | ||
|
||
private static boolean hasTrace; | ||
|
||
static { | ||
try { | ||
Level.class.getDeclaredField("TRACE"); | ||
hasTrace = true; | ||
} | ||
catch (Throwable e) {} | ||
} | ||
|
||
Log4JLogger(String className) { | ||
logger = LogManager.getLogger(className); | ||
|
||
isFatalEnabled = logger.isFatalEnabled(); | ||
isErrorEnabled = logger.isErrorEnabled(); | ||
isWarnEnabled = logger.isWarnEnabled(); | ||
isInfoEnabled = logger.isInfoEnabled(); | ||
isDebugEnabled = logger.isDebugEnabled(); | ||
if (hasTrace){ | ||
isTraceEnabled = logger.isTraceEnabled(); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void debug(Object message, Throwable t) { | ||
if (isDebugEnabled()) { | ||
logger.debug(message,t); | ||
} | ||
} | ||
|
||
@Override | ||
public void error(Object message, Throwable t) { | ||
if (isErrorEnabled()) { | ||
logger.error(message, t); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void fatal(Object message, Throwable t) { | ||
if (isFatalEnabled()) { | ||
logger.fatal(message, t); | ||
} | ||
} | ||
|
||
@Override | ||
public void info(Object message, Throwable t) { | ||
if (isInfoEnabled()) { | ||
logger.info(message, t); | ||
} | ||
} | ||
|
||
@Override | ||
public void trace(Object message, Throwable t) { | ||
if (isTraceEnabled()) { | ||
logger.trace( message, t); | ||
} else if ((!hasTrace) && isDebugEnabled()) { | ||
logger.debug(message, t); | ||
} | ||
} | ||
|
||
@Override | ||
public void warn(Object message, Throwable t) { | ||
if (isWarnEnabled()) { | ||
logger.warn(message, t); | ||
} | ||
} | ||
|
||
@Override | ||
protected void log(int level, Object message, Throwable tx) { | ||
switch (level) { | ||
case LEVEL_FATAL: | ||
fatal(message,tx); | ||
break; | ||
case LEVEL_ERROR: | ||
error(message,tx); | ||
break; | ||
case LEVEL_WARN: | ||
warn(message,tx); | ||
break; | ||
case LEVEL_INFO: | ||
info(message,tx); | ||
break; | ||
case LEVEL_DEBUG: | ||
debug(message,tx); | ||
break; | ||
case LEVEL_TRACE: | ||
if (hasTrace) { | ||
logger.trace(message, tx); | ||
} else { | ||
logger.debug(message, tx); | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isDebugEnabled() { | ||
return logger.isDebugEnabled(); | ||
} | ||
|
||
@Override | ||
public boolean isErrorEnabled() { | ||
return logger.isErrorEnabled(); | ||
} | ||
|
||
@Override | ||
public boolean isFatalEnabled() { | ||
return logger.isFatalEnabled(); | ||
} | ||
|
||
@Override | ||
public boolean isInfoEnabled() { | ||
return logger.isInfoEnabled(); | ||
} | ||
|
||
@Override | ||
public boolean isTraceEnabled() { | ||
if (!hasTrace) { | ||
return logger.isDebugEnabled(); | ||
} | ||
return logger.isTraceEnabled(); | ||
} | ||
|
||
@Override | ||
public boolean isWarnEnabled() { | ||
return logger.isWarnEnabled(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Configuration status="INFO"> | ||
<Appenders> | ||
<Console name="console" target="SYSTEM_OUT"> | ||
<PatternLayout | ||
pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" /> | ||
</Console> | ||
</Appenders> | ||
<Loggers> | ||
<Root level="debug" additivity="false"> | ||
<AppenderRef ref="console" /> | ||
</Root> | ||
</Loggers> | ||
</Configuration> |