本文整理汇总了Java中org.apache.logging.log4j.core.config.Configuration.addLogger方法的典型用法代码示例。如果您正苦于以下问题:Java Configuration.addLogger方法的具体用法?Java Configuration.addLogger怎么用?Java Configuration.addLogger使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.logging.log4j.core.config.Configuration
的用法示例。
在下文中一共展示了Configuration.addLogger方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getOrCreateLoggerConfig
import org.apache.logging.log4j.core.config.Configuration; //导入方法依赖的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;
}
示例2: configureLogging
import org.apache.logging.log4j.core.config.Configuration; //导入方法依赖的package包/类
protected void configureLogging() {
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
Layout layout = PatternLayout.createLayout(PatternLayout.SIMPLE_CONVERSION_PATTERN, null, config, null, null,
true, false, null, null);
Appender appender = FileAppender.createAppender(workDir + "/logs/camel-standalone.log", "false", "false", "File", "true",
"false", "false", "4000", layout, null, "false", null, config);
appender.start();
config.addAppender(appender);
AppenderRef ref = AppenderRef.createAppenderRef("File", null, null);
AppenderRef[] refs = new AppenderRef[] {ref};
LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.INFO, "StandaloneFileLoggerConfig",
"true", refs, null, config, null );
loggerConfig.addAppender(appender, null, null);
config.addLogger("StandaloneFileLoggerConfig", loggerConfig);
ctx.updateLoggers();
}
示例3: configureLog4j
import org.apache.logging.log4j.core.config.Configuration; //导入方法依赖的package包/类
private static Logger configureLog4j() {
LoggerContext context = (LoggerContext) LogManager.getContext();
Configuration config = context.getConfiguration();
PatternLayout layout = PatternLayout.createLayout("%m%n", null, null, Charset.defaultCharset(), false, false, null, null);
Appender appender = ConsoleAppender.createAppender(layout, null, null, "CONSOLE_APPENDER", null, null);
appender.start();
AppenderRef ref = AppenderRef.createAppenderRef("CONSOLE_APPENDER", null, null);
AppenderRef[] refs = new AppenderRef[]{ref};
LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.INFO, "CONSOLE_LOGGER", "com", refs, null, null, null);
loggerConfig.addAppender(appender, null, null);
config.addAppender(appender);
config.addLogger("Main.class", loggerConfig);
context.updateLoggers(config);
return LogManager.getContext().getLogger("Main.class");
}
示例4: bindLogger
import org.apache.logging.log4j.core.config.Configuration; //导入方法依赖的package包/类
public static void bindLogger() {
LoggerContext context = (LoggerContext) LogManager.getContext(false);
Configuration config = context.getConfiguration();
Map<String, ESLogger> loggers = Maps.newConcurrentHashMap();
Appender appender = new AbstractAppender("", null, null) {
@Override
public void append(LogEvent event) {
String name = event.getLoggerName();
ESLogger logger = loggers.computeIfAbsent(name, key -> new ESLogger(key, null, (LoggerImpl) LoggerFactory.getLogger(key)));
logger.log(event.getLevel(), event.getMarker(), event.getMessage(), event.getThrown());
}
};
appender.start();
config.addAppender(appender);
LoggerConfig loggerConfig = new LoggerConfig("", Level.ALL, false);
loggerConfig.addAppender(appender, null, null);
config.addLogger("", loggerConfig);
context.updateLoggers();
}
示例5: loadLogger
import org.apache.logging.log4j.core.config.Configuration; //导入方法依赖的package包/类
/**
* <p>
* This method loads the required properties into log4j for the logger
* </p>
*
* @param logFileDir
* Log file directory
* @param taskAttemptID
* The task attempt id
*
* @throws IOException
* If any error occurs
* @throws URISyntaxException
* @throws SAXException
* @throws ParserConfigurationException
* @throws TransformerException
*/
public static void loadLogger(String logFileDir, String taskAttemptID) throws IOException, URISyntaxException, ParserConfigurationException,
SAXException, TransformerException {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
ctx.reconfigure();
Configuration config = ctx.getConfiguration();
MemoryMappedFileAppender memoryMappedfileAppender = createMemoryMappedFileAppender(config, LOG_APPENDER_NAME + taskAttemptID, logFileDir, taskAttemptID, 0);
memoryMappedfileAppender.start();
AppenderRef[] ar = new AppenderRef [1];
ar[0] = AppenderRef.createAppenderRef(LOG_APPENDER_NAME + taskAttemptID , Level.INFO, null);
LoggerConfig lgf = LoggerConfig.createLogger("false",Level.INFO , LOG_CATEGORY + taskAttemptID , null, ar, null, config, null);
config.addLogger(LOG_CATEGORY + taskAttemptID, lgf);
ctx.getLogger(LOG_CATEGORY + taskAttemptID).addAppender(memoryMappedfileAppender);
ctx.updateLoggers();
ctx.start();
mapReduceLoggers = new ArrayList<Logger>(1);
mapReduceLoggers.add(LogManager.getLogger(LOG_CATEGORY + taskAttemptID));
LOG.debug("Finished loading logger");
}
示例6: setLevel
import org.apache.logging.log4j.core.config.Configuration; //导入方法依赖的package包/类
public static void setLevel(Logger logger, Level level) {
LoggerContext ctx = (LoggerContext) LogManager.getContext(StartServlet.class.getClassLoader(), false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = new LoggerConfig();
loggerConfig.setLevel(level);
config.removeLogger(logger.getName());
config.addLogger(logger.getName(), loggerConfig);
ctx.updateLoggers(config);
}
示例7: getLogger
import org.apache.logging.log4j.core.config.Configuration; //导入方法依赖的package包/类
/**
* Get the logger used for output
*
* @param validatedArgs with output file, silent, and verbose options that
* are used to configure the logger
* @return Logger with dynamically-generated configuration
*/
public static Logger getLogger(ValidatedArgs validatedArgs) {
// This approach is ugly, but it circumvents the need for multiple log4j
// configuration files and simplifies writing results to the console and the output file
// Silence StatusLogger
System.setProperty("org.apache.logging.log4j.simplelog.StatusLogger.level", "FATAL");
// Setup context
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
Configuration configuration = loggerContext.getConfiguration();
// Define layout
PatternLayout patternLayout = PatternLayout.newBuilder()
.withConfiguration(configuration)
// uncomment this pattern for debugging
//.withPattern("%d{ISO8601} [%level] [%F:%L] %msg%n")
.build();
// Add appenders
AppenderRef[] appenderRefs;
//// Create console appender unless silent
ConsoleAppender consoleAppender = null;
AppenderRef consoleAppenderRef = null;
if (!validatedArgs.silent) {
consoleAppender = ConsoleAppender.newBuilder()
.setConfiguration(configuration)
.withLayout(patternLayout)
.withName(CONSOLE_APPENDER)
.build();
consoleAppender.start();
configuration.addAppender(consoleAppender);
consoleAppenderRef = AppenderRef.createAppenderRef(CONSOLE_APPENDER, null, null);
}
//// Create file appender if output file specified
FileAppender fileAppender = null;
AppenderRef fileAppenderRef = null;
if (validatedArgs.outputFile != null) {
fileAppender = FileAppender.newBuilder()
.setConfiguration(configuration)
.withLayout(patternLayout)
.withName(FILE_APPENDER)
.withFileName(validatedArgs.outputFile.getAbsolutePath())
.build();
fileAppender.start();
configuration.addAppender(fileAppender);
fileAppenderRef = AppenderRef.createAppenderRef(FILE_APPENDER, null, null);
}
if ((consoleAppenderRef != null) && (fileAppenderRef != null)) {
appenderRefs = new AppenderRef[]{consoleAppenderRef, fileAppenderRef};
} else if (consoleAppenderRef != null) {
appenderRefs = new AppenderRef[]{consoleAppenderRef};
} else if (fileAppenderRef != null) {
appenderRefs = new AppenderRef[]{fileAppenderRef};
} else {
throw new IllegalStateException("At least one appender must be configured to provide output!");
}
// Build and update the LoggerConfig
Level levelToUse = validatedArgs.verbose ? VERBOSE_LEVEL : DEFAULT_LEVEL;
LoggerConfig loggerConfig = LoggerConfig.createLogger(false, levelToUse, LOGGER_NAME, "true", appenderRefs, null, configuration, null);
if (consoleAppender != null) {
loggerConfig.addAppender(consoleAppender, null, null);
}
if (fileAppender != null) {
loggerConfig.addAppender(fileAppender, null, null);
}
configuration.addLogger(LOGGER_NAME, loggerConfig);
loggerContext.updateLoggers();
return (Logger) LogManager.getLogger(LOGGER_NAME);
}
示例8: configure
import org.apache.logging.log4j.core.config.Configuration; //导入方法依赖的package包/类
/**
* {@inheritDoc}
* @see com.heliosapm.streams.tracing.AbstractMetricWriter#configure(java.util.Properties)
*/
@Override
public void configure(final Properties config) {
super.configure(config);
final String loggerName = ConfigurationHelper.getSystemThenEnvProperty(CONFIG_LOGGER_NAME, null, config);
if(loggerName==null || !LogManager.getContext(true).hasLogger(loggerName)) {
/*
* ===================================================
* FIXME: this is super ugly
* ===================================================
* TODO:
* - log4j2 async appender
* - low gc message objects
*/
final String entryPrefix = ConfigurationHelper.getSystemThenEnvProperty(CONFIG_ENTRY_PREFIX, DEFAULT_ENTRY_PREFIX, config);
this.config.put("entryPrefix", entryPrefix);
final String entrySuffix = ConfigurationHelper.getSystemThenEnvProperty(CONFIG_ENTRY_SUFFIX, DEFAULT_ENTRY_SUFFIX, config);
this.config.put("entrySuffix", entrySuffix);
final boolean randomAccessFile = ConfigurationHelper.getBooleanSystemThenEnvProperty(CONFIG_RA_FILE, DEFAULT_RA_FILE, config);
this.config.put("randomAccessFile", randomAccessFile);
final String fileName = ConfigurationHelper.getSystemThenEnvProperty(CONFIG_FILE_NAME, DEFAULT_FILE_NAME, config);
this.config.put("fileName", fileName);
final File file = new File(fileName);
final File dir = file.getParentFile();
if(dir.exists()) {
if(!dir.isDirectory()) throw new IllegalArgumentException("The logging directory is a file [" + dir + "]");
} else {
if(!dir.mkdirs()) throw new IllegalArgumentException("Cannot create the logging directory [" + dir + "]");
}
LoggerContext context= (LoggerContext) LogManager.getContext();
Configuration loggingConfig = context.getConfiguration();
PatternLayout layout= PatternLayout.createLayout(entryPrefix + "%m" + entrySuffix + "%n" , null, loggingConfig, null, UTF8, false, false, null, null);
final DefaultRolloverStrategy strategy = DefaultRolloverStrategy.createStrategy("10", "1", null, null, null, true, loggingConfig);
// final int lastIndex = fileName.lastIndexOf('.');
final String format = ConfigurationHelper.getSystemThenEnvProperty(CONFIG_ROLL_PATTERN, DEFAULT_ROLL_PATTERN, config);
this.config.put("format", format);
final StringBuilder b = new StringBuilder(fileName).append(format);
// if(lastIndex==-1) {
// b.append(".").append(format);
// } else {
// b.insert(lastIndex, format);
// }
final String rolledFileFormat = b.toString();
final TriggeringPolicy trigger = TimeBasedTriggeringPolicy.createPolicy("1", "true");
if(randomAccessFile) {
appender = createRollingRandomAccessFileAppender(fileName, loggingConfig, layout, strategy, rolledFileFormat, trigger);
} else {
appender = createRollingFileAppender(fileName, loggingConfig, layout, strategy, rolledFileFormat, trigger);
}
loggingConfig.addAppender(appender);
AppenderRef ref = AppenderRef.createAppenderRef(getClass().getSimpleName(), Level.INFO, null);
AppenderRef[] refs = new AppenderRef[] { ref };
LoggerConfig loggerConfig = LoggerConfig.createLogger(
false,
Level.INFO,
getClass().getSimpleName() + "Logger",
"false", refs, null, loggingConfig, null);
loggerConfig.addAppender(appender, Level.INFO, null);
loggingConfig.addLogger(getClass().getSimpleName() + "Logger", loggerConfig);
context.updateLoggers();
org.apache.logging.log4j.core.Logger xlogger = context.getLogger(getClass().getName() + "Logger");
for(Appender app: xlogger.getAppenders().values()) {
xlogger.removeAppender(app);
}
xlogger.addAppender(appender);
metricLog = context.getLogger(getClass().getName() + "Logger");
} else {
metricLog = LogManager.getLogger(loggerName);
}
}
示例9: init
import org.apache.logging.log4j.core.config.Configuration; //导入方法依赖的package包/类
@SuppressWarnings({ "rawtypes", "unchecked" })
public static synchronized void init() {
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
Layout layout = PatternLayout.newBuilder().withPattern("%d{yyyy-MM-dd HH:mm:ss}:%p %t %c - %m%n")
.withConfiguration(config).withRegexReplacement(null).withCharset(null).withAlwaysWriteExceptions(true)
.withNoConsoleNoAnsi(false).withHeader(null).withFooter(null).build();
// file info
Filter fileInfoFilter = ThresholdFilter.createFilter(Level.ERROR, Result.DENY, Result.ACCEPT);
Appender fileInfoAppender = RollingFileAppender.createAppender(LOG_ROOT + "/zebra.log",
LOG_ROOT + "/zebra.log.%d{yyyy-MM-dd}.gz", "true", "FileInfo", "true", "4000", "true",
TimeBasedTriggeringPolicy.createPolicy("1", "true"),
ZebraRolloverStrategy.createStrategy("30", "1", null, Deflater.DEFAULT_COMPRESSION + "", config),
layout, fileInfoFilter, "false", null, null, config);
config.addAppender(fileInfoAppender);
fileInfoAppender.start();
AppenderRef fileInfoRef = AppenderRef.createAppenderRef("FileInfo", null, fileInfoFilter);
// console error
Appender consoleErrorAppender = ConsoleAppender.createAppender(layout, null, "SYSTEM_ERR", "ConsoleError",
"false", "false");
config.addAppender(consoleErrorAppender);
consoleErrorAppender.start();
// console info
Filter consoleWarnFilter = ThresholdFilter.createFilter(Level.ERROR, Result.DENY, Result.NEUTRAL);
Appender consoleWarnAppender = ConsoleAppender.createAppender(layout, consoleWarnFilter, "SYSTEM_OUT",
"ConsoleWarn", "false", "false");
config.addAppender(consoleWarnAppender);
consoleWarnAppender.start();
AppenderRef consoleWarnAppenderRef = AppenderRef.createAppenderRef("ConsoleWarn", Level.WARN,
consoleWarnFilter);
AppenderRef consoleErrorAppenderRef = AppenderRef.createAppenderRef("ConsoleError", Level.WARN, null);
AppenderRef[] refs = new AppenderRef[] { consoleErrorAppenderRef, consoleWarnAppenderRef, fileInfoRef };
LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.INFO, "com.dianping.zebra", "true", refs,
null, config, null);
loggerConfig.addAppender(consoleErrorAppender, Level.ERROR, null);
loggerConfig.addAppender(consoleWarnAppender, Level.INFO, null);
loggerConfig.addAppender(fileInfoAppender, Level.INFO, null);
config.addLogger("com.dianping.zebra", loggerConfig);
ctx.updateLoggers();
context = ctx;
}
示例10: createFileLogger
import org.apache.logging.log4j.core.config.Configuration; //导入方法依赖的package包/类
/**
* Creates a new file-logger for an addOn. The logger will log to a file with the addOnId as name in the logs folder
* of Izou
*
* @param addOnId the Id of the addOn the logger is created for
* @param level level of logger (at what level of log the logger should be activated
* @return the new logger
*/
public synchronized ExtendedLogger createFileLogger(String addOnId, String level) {
try {
LoggerContext ctx = LogManager.getContext(false);
Configuration config = ((org.apache.logging.log4j.core.LoggerContext) ctx).getConfiguration();
//creates a new pattern layout (what determines how the log is formated, i.e. date, thread etc.)
Layout layout = PatternLayout.createLayout("%d %-5p [%t] %C{10} (%F:%L) - %m%n", config, null, null, true,
false, null, null);
//creates a file appender for the logger (so that it knows what file to log to)
Appender fileAppender = FileAppender.createAppender("logs" + File.separator + addOnId + ".log", "true",
"false", "file", "true", "false", "false", "4000", layout, null, "false", null, config);
fileAppender.start();
config.addAppender(fileAppender);
//creates also a console appender for the logger (so that the logger also outputs the log in the console)
Appender consoleAppender = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "console", null, null);
consoleAppender.start();
config.addAppender(consoleAppender);
//adds appenders to an array called refs. It will later serve as references to the logger as to what
// appenders it has
AppenderRef fileRef = AppenderRef.createAppenderRef("file", Level.DEBUG, null);
AppenderRef consoleRef = AppenderRef.createAppenderRef("console", Level.DEBUG, null);
AppenderRef[] refs = new AppenderRef[]{fileRef, consoleRef};
//creates the logger configurations for the logger, where the appender-references are also added
LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.DEBUG, addOnId,
"true", refs, null, config, null);
loggerConfig.addAppender(fileAppender, Level.DEBUG, null);
loggerConfig.addAppender(consoleAppender, Level.DEBUG, null);
//finally creates the logger and returns it
config.addLogger(addOnId, loggerConfig);
((org.apache.logging.log4j.core.LoggerContext) ctx).updateLoggers();
ctx.getLogger(addOnId);
ExtendedLogger logger = ctx.getLogger(addOnId);
return logger;
} catch(Exception e) {
fileLogger.error("Unable to create FileLogger",e);
return null;
}
}