本文整理汇总了Java中org.apache.logging.log4j.core.config.LoggerConfig.createLogger方法的典型用法代码示例。如果您正苦于以下问题:Java LoggerConfig.createLogger方法的具体用法?Java LoggerConfig.createLogger怎么用?Java LoggerConfig.createLogger使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.logging.log4j.core.config.LoggerConfig
的用法示例。
在下文中一共展示了LoggerConfig.createLogger方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getOrCreateLoggerConfig
import org.apache.logging.log4j.core.config.LoggerConfig; //导入方法依赖的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.LoggerConfig; //导入方法依赖的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.LoggerConfig; //导入方法依赖的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: main
import org.apache.logging.log4j.core.config.LoggerConfig; //导入方法依赖的package包/类
public static void main(String[] args) throws UnRetriableException{
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration();
ConsoleAppender appender = ConsoleAppender.createDefaultAppenderForLayout(PatternLayout.createDefaultLayout());
appender.start();
config.addAppender(appender);
AppenderRef[] refs = new AppenderRef[] { AppenderRef.createAppenderRef(appender.getName(), null, null) };
LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.INFO, LogManager.ROOT_LOGGER_NAME, "true", refs, null, config, null);
loggerConfig.addAppender(appender, null, null);
config.addLogger(LogManager.ROOT_LOGGER_NAME, loggerConfig);
ctx.updateLoggers();
Runner runner = defaultRunner();
runner.init();
runner.start();
}
示例5: main
import org.apache.logging.log4j.core.config.LoggerConfig; //导入方法依赖的package包/类
public static void main(String[] args) throws UnRetriableException {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration();
ConsoleAppender appender = ConsoleAppender.createDefaultAppenderForLayout(PatternLayout.createDefaultLayout());
appender.start();
config.addAppender(appender);
AppenderRef[] refs = new AppenderRef[] { AppenderRef.createAppenderRef(appender.getName(), null, null) };
LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.WARN, LogManager.ROOT_LOGGER_NAME, "true", refs, null, config, null);
loggerConfig.addAppender(appender, null, null);
config.addLogger(LogManager.ROOT_LOGGER_NAME, loggerConfig);
ctx.updateLoggers();
Runner runner = defaultRunner();
runner.init();
runner.start();
}
示例6: loadLogger
import org.apache.logging.log4j.core.config.LoggerConfig; //导入方法依赖的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");
}
示例7: createLoggerConfig
import org.apache.logging.log4j.core.config.LoggerConfig; //导入方法依赖的package包/类
/**
* Creates an instance of {@link LoggerConfig} which may be
* {@link AsyncLoggerConfig} if asynchronous loggers are used.
*
* @param configuration the owner configuration
* @param asyncLoggers
* @param loggerName the name of the logger
* @param level the {@link Level} of the logger
* @param additivity if additivity is enabled for the logger
* @return an instance of {@link LoggerConfig} or
* {@link AsyncLoggerConfig}
*/
protected static LoggerConfig createLoggerConfig(final Configuration configuration,
boolean asyncLoggers, final String loggerName, final Level level, final boolean additivity) {
final Filter filter = null;
if (asyncLoggers) {
// XXX Obscure static factory methods.
return new AsyncLoggerConfig(loggerName, Collections.<AppenderRef>emptyList(), filter, level,
additivity, new Property[0], configuration, false) {
private static final long serialVersionUID = 1L;
};
} else {
return LoggerConfig.createLogger(String.valueOf(additivity), level, loggerName, null,
new AppenderRef[0], new Property[0], configuration, filter);
}
}
示例8: configureConsoleAppender
import org.apache.logging.log4j.core.config.LoggerConfig; //导入方法依赖的package包/类
public static void configureConsoleAppender() {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration();
ConsoleAppender appender = ConsoleAppender.createDefaultAppenderForLayout(PatternLayout.createDefaultLayout());
appender.start();
config.addAppender(appender);
AppenderRef[] refs = new AppenderRef[] { AppenderRef.createAppenderRef(appender.getName(), null, null) };
LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.ALL, LogManager.ROOT_LOGGER_NAME, "true",
refs, null, config, null);
loggerConfig.addAppender(appender, null, null);
config.addLogger(LogManager.ROOT_LOGGER_NAME, loggerConfig);
ctx.updateLoggers();
}
示例9: getLogger
import org.apache.logging.log4j.core.config.LoggerConfig; //导入方法依赖的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);
}
示例10: configure
import org.apache.logging.log4j.core.config.LoggerConfig; //导入方法依赖的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);
}
}
示例11: init
import org.apache.logging.log4j.core.config.LoggerConfig; //导入方法依赖的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;
}
示例12: createFileLogger
import org.apache.logging.log4j.core.config.LoggerConfig; //导入方法依赖的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;
}
}