Created
December 1, 2014 15:51
-
-
Save golonzovsky/6a5fe2006aeb0e973613 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package com.services.util; | |
import java.io.PrintWriter; | |
import java.io.StringWriter; | |
import java.util.ArrayList; | |
import java.util.Enumeration; | |
import java.util.List; | |
import org.apache.log4j.Level; | |
import org.apache.log4j.LogManager; | |
import org.apache.log4j.Logger; | |
import org.apache.log4j.config.PropertyPrinter; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.jmx.export.annotation.ManagedOperation; | |
import org.springframework.jmx.export.annotation.ManagedResource; | |
import org.springframework.stereotype.Component; | |
/** | |
* JMX log configurer | |
* Created by golonzovsky | |
*/ | |
@Component | |
@ManagedResource(objectName = "com.config:type=core,name=LogConfigurer") | |
public class JmxLogConfigurer { | |
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(JmxLogConfigurer.class); | |
@ManagedOperation | |
public String[] getLoggers() { | |
Enumeration<Logger> loggers = LogManager.getLoggerRepository().getCurrentLoggers(); | |
List<String> loggersInfo = new ArrayList<String>(); | |
while (loggers.hasMoreElements()) { | |
Logger logger = loggers.nextElement(); | |
loggersInfo.add(logger.getName()); | |
} | |
return loggersInfo.toArray(new String[loggersInfo.size()]); | |
} | |
@ManagedOperation | |
public void assignInfoLevel(String target) { | |
assignLogLevel(target, Level.INFO); | |
} | |
@ManagedOperation | |
public void assignWarnLevel(String target) { | |
assignLogLevel(target, Level.WARN); | |
} | |
@ManagedOperation | |
public void assignErrorLevel(String target) { | |
assignLogLevel(target, Level.ERROR); | |
} | |
@ManagedOperation | |
public void assignDebugLevel(String target) { | |
assignLogLevel(target, Level.DEBUG); | |
} | |
@ManagedOperation | |
public void assignFatalLevel(String target) { | |
assignLogLevel(target, Level.FATAL); | |
} | |
@ManagedOperation | |
public void deactivateLogging(String target) { | |
assignLogLevel(target, Level.OFF); | |
} | |
@ManagedOperation | |
public void assignTraceLevel(String target) { | |
assignLogLevel(target, Level.TRACE); | |
} | |
private void assignLogLevel(String loggingTarget, Level level) { | |
LOG.info("changing log level for '{}' from '{}' to '{}'", | |
new Object[]{level.toString(), getExistingLevel(loggingTarget), loggingTarget}); | |
LogManager.getLogger(loggingTarget).setLevel(level); | |
} | |
private String getExistingLevel(String target) { | |
Logger existingLogger = LogManager.exists(target); | |
if (existingLogger == null || existingLogger.getLevel() == null) { | |
return null; | |
} | |
return existingLogger.getLevel().toString(); | |
} | |
@ManagedOperation | |
public String printLog4jConfig() { | |
StringWriter sw = new StringWriter(); | |
PrintWriter pw = new PrintWriter(sw); | |
PropertyPrinter pp = new PropertyPrinter(pw); | |
pp.print(pw); | |
return sw.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment