本文整理汇总了Java中org.apache.logging.log4j.core.LoggerContext类的典型用法代码示例。如果您正苦于以下问题:Java LoggerContext类的具体用法?Java LoggerContext怎么用?Java LoggerContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
LoggerContext类属于org.apache.logging.log4j.core包,在下文中一共展示了LoggerContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: changeLogLevel
import org.apache.logging.log4j.core.LoggerContext; //导入依赖的package包/类
@PUT
@Path("/log/change-level/{loggerName}/{newLevel}")
public Response changeLogLevel(@PathParam("loggerName") String loggerName,
@PathParam("newLevel") String newLevel) {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
if (loggerConfig.getName().equals(LogManager.ROOT_LOGGER_NAME)) {
return Response.ok("Not found", MediaType.TEXT_PLAIN).build();
}
loggerConfig.setLevel(Level.valueOf(newLevel));
ctx.updateLoggers(); // This causes all Loggers to refetch information from their LoggerConfig.
return Response.ok("Done", MediaType.TEXT_PLAIN).build();
}
示例2: toggleCleanLogging
import org.apache.logging.log4j.core.LoggerContext; //导入依赖的package包/类
private void toggleCleanLogging() {
this.cleanLogging = !cleanLogging;
interactionHandler.sendText("clean logging: " + cleanLogging);
String appenderToAdd = cleanLogging ? "Clean" : "Console";
String appenderToRemove = cleanLogging ? "Console" : "Clean";
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
for (org.apache.logging.log4j.core.Logger logger : ctx.getLoggers()) {
logger.removeAppender(config.getAppender(appenderToRemove));
config.addLoggerAppender(logger, config.getAppender(appenderToAdd));
}
ctx.updateLoggers();
}
示例3: start
import org.apache.logging.log4j.core.LoggerContext; //导入依赖的package包/类
@Setup
public void start() {
counter = 0;
if (log == null) {
System.setProperty(
"Log4jContextSelector",
"org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
LoggerContext context = LoggerContext.getContext();
log = context.getLogger("Log4j2");
log.setAdditive(false);
ArrayList<Appender> list = new ArrayList<Appender>();
list.addAll(log.getAppenders().values());
for (Appender a : list) {
log.removeAppender(a);
}
}
}
示例4: buildLoggerContext
import org.apache.logging.log4j.core.LoggerContext; //导入依赖的package包/类
/**
* Build logger context logger context.
*
* @param environment the environment
* @param resourceLoader the resource loader
* @return the logger context
*/
public static Pair<Resource, LoggerContext> buildLoggerContext(final Environment environment, final ResourceLoader resourceLoader) {
try {
final String logFile = environment.getProperty("logging.config", "classpath:/log4j2.xml");
LOGGER.debug("Located logging configuration reference in the environment as [{}]", logFile);
if (ResourceUtils.doesResourceExist(logFile, resourceLoader)) {
final Resource logConfigurationFile = resourceLoader.getResource(logFile);
LOGGER.debug("Loaded logging configuration resource [{}]. Initializing logger context...", logConfigurationFile);
final LoggerContext loggerContext = Configurator.initialize("CAS", null, logConfigurationFile.getURI());
LOGGER.debug("Installing log configuration listener to detect changes and update");
loggerContext.getConfiguration().addListener(reconfigurable -> loggerContext.updateLoggers(reconfigurable.reconfigure()));
return Pair.of(logConfigurationFile, loggerContext);
}
LOGGER.warn("Logging configuration cannot be found in the environment settings");
} catch (final Exception e) {
throw Throwables.propagate(e);
}
return null;
}
示例5: watch
import org.apache.logging.log4j.core.LoggerContext; //导入依赖的package包/类
public void watch(Class<?> loggerClass, Level level) {
this.loggerClass = loggerClass;
Appender appender = new AbstractAppender(APPENDER_NAME, null, PatternLayout.createDefaultLayout()) {
@Override
public void append(LogEvent event) {
logEvents.add(event);
}
};
appender.start();
final LoggerContext ctx = getLoggerContext();
LoggerConfig loggerConfig = ctx.getConfiguration().getLoggerConfig(loggerClass.getName());
oldLevel = loggerConfig.getLevel();
loggerConfig.setLevel(level);
loggerConfig.addAppender(appender, level, null);
ctx.updateLoggers();
}
示例6: getOrCreateLoggerConfig
import org.apache.logging.log4j.core.LoggerContext; //导入依赖的package包/类
public static LoggerConfig getOrCreateLoggerConfig(String name) {
LoggerContext context = (LoggerContext) LogManager.getContext(false);
Configuration config = context.getConfiguration();
LoggerConfig logConfig = config.getLoggerConfig(name);
boolean update = false;
if (!logConfig.getName().equals(name)) {
List<AppenderRef> appenderRefs = logConfig.getAppenderRefs();
Map<Property, Boolean> properties = logConfig.getProperties();
Set<Property> props = properties == null ? null : properties.keySet();
logConfig = LoggerConfig.createLogger(String.valueOf(logConfig.isAdditive()),
logConfig.getLevel(), name, String.valueOf(logConfig.isIncludeLocation()),
appenderRefs == null ? null : appenderRefs.toArray(new AppenderRef[appenderRefs.size()]),
props == null ? null : props.toArray(new Property[props.size()]), config, null);
config.addLogger(name, logConfig);
update = true;
}
if (update) {
context.updateLoggers();
}
return logConfig;
}
示例7: tearDownAfterClass
import org.apache.logging.log4j.core.LoggerContext; //导入依赖的package包/类
@AfterClass
public static void tearDownAfterClass() throws Exception {
for (Handler handler : saveHandlers) {
julLogger.addHandler(handler);
}
if (saveLog4j2Config == null) {
System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
} else {
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, saveLog4j2Config);
((LoggerContext) LogManager.getContext(false)).reconfigure();
}
if (saveUserDir == null) {
System.clearProperty("user.dir");
} else {
System.setProperty("user.dir", saveUserDir);
}
if (saveUserHome == null) {
System.clearProperty("user.home");
} else {
System.setProperty("user.home", saveUserHome);
}
}
示例8: addSuspectFileAppender
import org.apache.logging.log4j.core.LoggerContext; //导入依赖的package包/类
/**
* Add an appender to Log4j which sends all INFO+ messages to a separate file which will be used
* later to scan for suspect strings. The pattern of the messages conforms to the original log
* format so that hydra will be able to parse them.
*/
private static void addSuspectFileAppender(final String workspaceDir) {
final String suspectFilename = new File(workspaceDir, SUSPECT_FILENAME).getAbsolutePath();
final LoggerContext appenderContext =
((org.apache.logging.log4j.core.Logger) LogManager.getLogger(LogService.BASE_LOGGER_NAME))
.getContext();
final PatternLayout layout = PatternLayout.createLayout(
"[%level{lowerCase=true} %date{yyyy/MM/dd HH:mm:ss.SSS z} <%thread> tid=%tid] %message%n%throwable%n",
null, null, null, Charset.defaultCharset(), true, false, "", "");
final FileAppender fileAppender = FileAppender.createAppender(suspectFilename, "true", "false",
DUnitLauncher.class.getName(), "true", "false", "false", "0", layout, null, null, null,
appenderContext.getConfiguration());
fileAppender.start();
LoggerConfig loggerConfig =
appenderContext.getConfiguration().getLoggerConfig(LogService.BASE_LOGGER_NAME);
loggerConfig.addAppender(fileAppender, Level.INFO, null);
}
示例9: main
import org.apache.logging.log4j.core.LoggerContext; //导入依赖的package包/类
public static void main(String[] args) {
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
config.getLoggerConfig(strategy.Portfolio.class.getName()).setLevel(Level.WARN);
ctx.updateLoggers(config);
final CommonParam cp = ParamManager.getCommonParam("i", TIME_FRAME.MIN15, "20080101 000000", "20160101 170000");
StrategyOptimizer so = new StrategyOptimizer(tester.RealStrategyTester.class);
so.setInstrumentParam(cp.instrument, cp.tf);
so.setTestDateRange((int) DateTimeHelper.Ldt2Long(cp.start_date), (int) DateTimeHelper.Ldt2Long(cp.end_date));
int num = so.setStrategyParamRange(MaPsarStrategy.class, new Integer[]{12, 500, 2}, new String[]{MA.MODE_EMA, MA.MODE_SMMA}, new APPLIED_PRICE[] {APPLIED_PRICE.PRICE_CLOSE, APPLIED_PRICE.PRICE_TYPICAL}, new Float[]{0.01f, 0.02f, 0.01f}, new Float[]{0.1f, 0.2f, 0.02f});
System.out.println(num);
so.StartOptimization();
Set<Entry<Object[],Performances>> entryset = so.result_db.entrySet();
for (Entry<Object[],Performances> entry : entryset) {
for (Object obj : entry.getKey()) {
System.out.print(obj + ",\t");
}
System.out.println("ProfitRatio: " + String.format("%.5f", entry.getValue().ProfitRatio) + "\tMaxDrawDown: " + entry.getValue().MaxDrawDown);
}
}
示例10: main
import org.apache.logging.log4j.core.LoggerContext; //导入依赖的package包/类
public static void main(String[] args) {
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
config.getLoggerConfig(strategy.Portfolio.class.getName()).setLevel(Level.WARN);
ctx.updateLoggers(config);
final CommonParam cp = ParamManager.getCommonParam("cu", TIME_FRAME.DAY, "19980101 000000", "20160916 170000");
StrategyOptimizer so = new StrategyOptimizer(tester.RealStrategyTester.class);
so.setInstrumentParam(cp.instrument, cp.tf);
so.setTestDateRange((int) DateTimeHelper.Ldt2Long(cp.start_date), (int) DateTimeHelper.Ldt2Long(cp.end_date));
int num = so.setStrategyParamRange(ChannelBreakStrategy.class, new Integer[]{4, 800, 2});
System.out.println(num);
so.StartOptimization();
Set<Entry<Object[],Performances>> entryset = so.result_db.entrySet();
for (Entry<Object[],Performances> entry : entryset) {
for (Object obj : entry.getKey()) {
System.out.print(obj + ",\t");
}
System.out.println("ProfitRatio: " + entry.getValue().ProfitRatio + "\tMaxDrawDown: " + entry.getValue().MaxDrawDown);
}
}
示例11: main
import org.apache.logging.log4j.core.LoggerContext; //导入依赖的package包/类
public static void main(String[] args) {
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
config.getLoggerConfig(strategy.Portfolio.class.getName()).setLevel(Level.WARN);
ctx.updateLoggers(config);
final CommonParam cp = ParamManager.getCommonParam("m", TIME_FRAME.MIN60, "19980101 000000", "20160101 170000");
StrategyOptimizer so = new StrategyOptimizer(tester.RealStrategyTester.class);
so.setInstrumentParam(cp.instrument, cp.tf);
so.setTestDateRange((int) DateTimeHelper.Ldt2Long(cp.start_date), (int) DateTimeHelper.Ldt2Long(cp.end_date));
int num = so.setStrategyParamRange(DblMaPsarStrategy.class, new Integer[]{3, 5, 1}, new Integer[]{10, 100, 2}, MA.MA_MODES, new APPLIED_PRICE[] {APPLIED_PRICE.PRICE_CLOSE, APPLIED_PRICE.PRICE_TYPICAL}, new Float[]{0.01f, 0.02f, 0.01f}, new Float[]{0.12f, 0.2f, 0.02f});
System.out.println(num);
so.StartOptimization();
Set<Entry<Object[],Performances>> entryset = so.result_db.entrySet();
for (Entry<Object[],Performances> entry : entryset) {
for (Object obj : entry.getKey()) {
System.out.print(obj + ",\t");
}
System.out.println("ProfitRatio: " + String.format("%.5f", entry.getValue().ProfitRatio) + "\tMaxDrawDown: " + entry.getValue().MaxDrawDown);
}
}
示例12: main
import org.apache.logging.log4j.core.LoggerContext; //导入依赖的package包/类
public static void main(String[] args) {
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
config.getLoggerConfig(strategy.Portfolio.class.getName()).setLevel(Level.WARN);
ctx.updateLoggers(config);
final CommonParam cp = ParamManager.getCommonParam("cu", TIME_FRAME.MIN15, "20080101 000000", "20160101 170000");
StrategyOptimizer so = new StrategyOptimizer(tester.RealStrategyTester.class);
so.setInstrumentParam(cp.instrument, cp.tf);
so.setTestDateRange((int) DateTimeHelper.Ldt2Long(cp.start_date), (int) DateTimeHelper.Ldt2Long(cp.end_date));
int num = so.setStrategyParamRange(MABreakStrategy.class, new Integer[]{8, 800, 2}, MA.MA_MODES);
System.out.println(num);
so.StartOptimization();
Set<Entry<Object[],Performances>> entryset = so.result_db.entrySet();
for (Entry<Object[],Performances> entry : entryset) {
for (Object obj : entry.getKey()) {
System.out.print(obj + ",\t");
}
System.out.println("ProfitRatio: " + String.format("%.5f", entry.getValue().ProfitRatio) + "\tMaxDrawDown: " + entry.getValue().MaxDrawDown);
}
}
示例13: getActiveLogFilePaths
import org.apache.logging.log4j.core.LoggerContext; //导入依赖的package包/类
/**
* We cannot presume that the default file name/location setting won't be changed by the user.
* Therefore, we should be able to retrieve that info from the underlying logging mechanism
* by iterating over appenders.
*/
private List<Path> getActiveLogFilePaths() {
LoggerContextFactory factory = LogManager.getFactory();
ContextSelector selector = ((Log4jContextFactory) factory).getSelector();
List<Path> fileNameList = new ArrayList<>();
for (LoggerContext ctx : selector.getLoggerContexts()) {
for (Appender appender : ctx.getConfiguration().getAppenders().values()) {
String fileName = extractFileName(appender);
if (fileName != null) {
fileNameList.add(Paths.get(fileName));
}
}
}
return fileNameList;
}
示例14: KafkaManager
import org.apache.logging.log4j.core.LoggerContext; //导入依赖的package包/类
public KafkaManager(final LoggerContext loggerContext, final String name, final String topic, final String zkServers, final String mail, final String rpc,
final String app, final String host, final Property[] properties) {
super(loggerContext, name);
this.topic = topic;
this.zkServers = zkServers;
this.mail = mail;
this.rpc = rpc;
this.app = app;
this.orginApp = app;
this.host = host;
this.checkAndSetConfig(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class.getName());
this.checkAndSetConfig(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
// 设置分区类, 使用自定义的KeyModPartitioner,同样的key进入相同的partition
this.checkAndSetConfig(ProducerConfig.PARTITIONER_CLASS_CONFIG, KeyModPartitioner.class.getName());
// xml配置里面的参数
for (final Property property : properties) {
this.config.put(property.getName(), property.getValue());
}
// 由于容器部署需要从外部获取host
this.config.put(ProducerConfig.CLIENT_ID_CONFIG, this.app + Constants.MIDDLE_LINE + this.host + Constants.MIDDLE_LINE + "log4j2");
}
示例15: configureLoggers
import org.apache.logging.log4j.core.LoggerContext; //导入依赖的package包/类
/**
* Setup the file logger so that it outputs messages to the appropriately named file,
* depending on the subtitle of the current program instance.
*/
private static void configureLoggers( String subtitle )
{
// Remove all characters that are not allowed for Windows filenames.
subtitle = subtitle.replaceAll( "[\\s" + Pattern.quote( "\\/:*?\"<>|" ) + "]", "" );
LoggerContext context = (LoggerContext)LogManager.getContext();
Configuration config = context.getConfiguration();
PatternLayout layout = PatternLayout.createLayout(
"%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%throwable%n",
null, null, null, Charset.defaultCharset(), false, false, null, null
);
FileAppender appender = FileAppender.createAppender(
"logs/log-" + subtitle + ".txt", "false", "false", "LogFile-" + subtitle, "true", "true", "true",
"8192", layout, null, "false", "", config
);
org.apache.logging.log4j.core.Logger rootLogger = (org.apache.logging.log4j.core.Logger)LogManager.getRootLogger();
rootLogger.addAppender( appender );
appender.start();
}