diff --git a/core/src/main/groovy/com/muwire/core/Core.groovy b/core/src/main/groovy/com/muwire/core/Core.groovy index 60c6923f..c5a0d239 100644 --- a/core/src/main/groovy/com/muwire/core/Core.groovy +++ b/core/src/main/groovy/com/muwire/core/Core.groovy @@ -30,8 +30,10 @@ import com.muwire.core.search.SearchEvent import com.muwire.core.search.SearchManager import com.muwire.core.trust.TrustEvent import com.muwire.core.trust.TrustService +import com.muwire.core.util.MuWireLogManager import groovy.util.logging.Log +import net.i2p.I2PAppContext import net.i2p.client.I2PClientFactory import net.i2p.client.I2PSession import net.i2p.client.streaming.I2PSocketManager @@ -71,6 +73,10 @@ class Core { } } + log.info "Initializing I2P context" + I2PAppContext.getGlobalContext().logManager() + I2PAppContext.getGlobalContext()._logManager = new MuWireLogManager() + log.info("initializing I2P socket manager") def i2pClient = new I2PClientFactory().createClient() File keyDat = new File(home, "key.dat") diff --git a/core/src/main/groovy/com/muwire/core/util/JULLog.groovy b/core/src/main/groovy/com/muwire/core/util/JULLog.groovy new file mode 100644 index 00000000..81764603 --- /dev/null +++ b/core/src/main/groovy/com/muwire/core/util/JULLog.groovy @@ -0,0 +1,75 @@ +package com.muwire.core.util + +import java.util.logging.Level +import java.util.logging.LogManager +import java.util.logging.Logger + +import net.i2p.util.Log + +class JULLog extends Log { + + private static final Map I2P_TO_JUL = new HashMap<>() + static { + I2P_TO_JUL.put(Log.DEBUG, Level.FINE) + I2P_TO_JUL.put(Log.INFO, Level.INFO) + I2P_TO_JUL.put(Log.WARN, Level.WARNING) + I2P_TO_JUL.put(Log.ERROR, Level.SEVERE) + I2P_TO_JUL.put(Log.CRIT, Level.SEVERE) + } + + private final Logger delegate + private final Level level + + public JULLog(Class cls) { + super(cls) + delegate = Logger.getLogger(cls.getName()) + level = findLevel(delegate) + } + + public JULLog(String name) { + super(name); + delegate = Logger.getLogger(name) + level = findLevel(delegate) + } + + private static Level findLevel(Logger log) { + while (log.getLevel() == null) + log = log.getParent() + log.getLevel() + } + + @Override + public void log(int priority, String msg) { + delegate.log(I2P_TO_JUL.get(priority), msg) + } + + @Override + public void log(int priority, String msg, Throwable t) { + delegate.log(I2P_TO_JUL.get(priority), msg, t) + } + + @Override + public boolean shouldLog(int priority) { + delegate.isLoggable(I2P_TO_JUL.get(priority)) + } + + @Override + public boolean shouldDebug() { + level.intValue().intValue() <= Level.FINE.intValue() + } + + @Override + public boolean shouldInfo() { + level.intValue().intValue() <= Level.INFO.intValue() + } + + @Override + public boolean shouldWarn() { + level.intValue().intValue() <= Level.WARNING.intValue() + } + + @Override + public boolean shouldError() { + level.intValue().intValue() <= Level.SEVERE.intValue() + } +} diff --git a/core/src/main/groovy/com/muwire/core/util/MuWireLogManager.groovy b/core/src/main/groovy/com/muwire/core/util/MuWireLogManager.groovy new file mode 100644 index 00000000..f087c8f2 --- /dev/null +++ b/core/src/main/groovy/com/muwire/core/util/MuWireLogManager.groovy @@ -0,0 +1,21 @@ +package com.muwire.core.util + +import net.i2p.I2PAppContext +import net.i2p.util.Log +import net.i2p.util.LogManager + +class MuWireLogManager extends LogManager { + + MuWireLogManager() { + super(I2PAppContext.getGlobalContext()) + } + + + @Override + public Log getLog(Class cls, String name) { + if (cls != null) + return new JULLog(cls) + new JULLog(name) + } + +}