本文整理汇总了Java中org.apache.logging.log4j.core.appender.FileAppender.start方法的典型用法代码示例。如果您正苦于以下问题:Java FileAppender.start方法的具体用法?Java FileAppender.start怎么用?Java FileAppender.start使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.logging.log4j.core.appender.FileAppender
的用法示例。
在下文中一共展示了FileAppender.start方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addSuspectFileAppender
import org.apache.logging.log4j.core.appender.FileAppender; //导入方法依赖的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);
}
示例2: configureLoggers
import org.apache.logging.log4j.core.appender.FileAppender; //导入方法依赖的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();
}
示例3: updateLogConfig
import org.apache.logging.log4j.core.appender.FileAppender; //导入方法依赖的package包/类
/**
* Update the log configuration based on command line options
*
* @param cl
*/
protected final void updateLogConfig(CommandLine cl) {
String logFile = cl.getOptionValue(OPT_LOG_FILE.getLongOpt(), null);
String logLevel = cl.getOptionValue(OPT_LOG_LEVEL.getLongOpt(), null);
if (logFile != null || logLevel != null) {
logFile = logFile != null ? logFile : getDefaultLogFileName();
logLevel = logLevel != null ? logLevel : DEFAULT_LOG_LEVEL;
LoggerContext context = (LoggerContext) LogManager.getContext(false);
Configuration configuration = context.getConfiguration();
FileAppender appender = FileAppender.newBuilder()
.withName("File")
.withFileName(logFile)
.withLayout(PatternLayout.newBuilder().withPattern(PatternLayout.SIMPLE_CONVERSION_PATTERN).build())
.withAppend(false)
.build();
appender.start();
configuration.getRootLogger().addAppender(appender, Level.getLevel(logLevel), null);
configuration.getRootLogger().setLevel(Level.getLevel(logLevel));
context.updateLoggers();
}
}
示例4: setUpAppender
import org.apache.logging.log4j.core.appender.FileAppender; //导入方法依赖的package包/类
@Before
public void setUpAppender() throws IOException {
file = folder.newFile();
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
final LoggerConfig root = ((AbstractConfiguration) config).getRootLogger();
PatternLayout layout = PatternLayout.createLayout("[%p] %m%n", config, null, null, true, false, null, null);
FileAppender appender = FileAppender.createAppender(file.getAbsolutePath(), "true", "false", "TestLogFile", "true", "false", "false", "8192", layout, null, "false", null, config);
appender.start();
config.addAppender(appender);
root.addAppender(appender, null, null);
ctx.updateLoggers();
}
示例5: addFileAppender
import org.apache.logging.log4j.core.appender.FileAppender; //导入方法依赖的package包/类
public static void addFileAppender(final String loggerPath, final File logFile, final AppConfig appConfig)
throws IOException
{
// retrieve the logger context
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
Configuration configuration = loggerContext.getConfiguration();
// retrieve the root logger config
LoggerConfig loggerConfig = configuration.getLoggerConfig(loggerPath);
loggerConfig.setLevel(Level.toLevel(appConfig.getTcLogLevel()));
// Define log pattern layout
PatternLayout layout = PatternLayout.createLayout(DEFAULT_LOGGER_PATTERN, null, null, null,
Charset.defaultCharset(), false, false, null, null);
// create the appenders
FileAppender fileAppender =
FileAppender.createAppender(logFile.getAbsolutePath(), "true", "false", "fileAppender",
"true", "true", "true", "8192", layout, null, "false", null, null);
fileAppender.start();
// add the appenders
loggerConfig.addAppender(fileAppender, Level.toLevel(appConfig.getTcLogLevel()), null);
loggerContext.updateLoggers();
}
示例6: addFileLogger
import org.apache.logging.log4j.core.appender.FileAppender; //导入方法依赖的package包/类
public static String addFileLogger(String logFile) {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
// Default log layout
Layout<? extends Serializable> layout =
PatternLayout.createLayout("%d %p [%t] %m%n", null, null, null, true, true, null, null);
// Create a new file appender for the given filename
FileAppender appender = FileAppender.createAppender(
logFile,
"false",
"false",
"FileAppender",
"false",
"true",
"true",
null,
layout,
null,
null,
null,
config);
appender.start();
((Logger) LogManager.getRootLogger()).addAppender(appender);
return appender.getFileName();
}
示例7: addSuspectFileAppender
import org.apache.logging.log4j.core.appender.FileAppender; //导入方法依赖的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);
}
示例8: getLogger
import org.apache.logging.log4j.core.appender.FileAppender; //导入方法依赖的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);
}
示例9: mdcPattern
import org.apache.logging.log4j.core.appender.FileAppender; //导入方法依赖的package包/类
/**
* Test case for MDC conversion pattern.
*/
@Test
public void mdcPattern() throws Exception {
final String mdcMsgPattern1 = "%m : %X%n";
final String mdcMsgPattern2 = "%m : %X{key1}%n";
final String mdcMsgPattern3 = "%m : %X{key2}%n";
final String mdcMsgPattern4 = "%m : %X{key3}%n";
final String mdcMsgPattern5 = "%m : %X{key1},%X{key2},%X{key3}%n";
// set up appender
final PatternLayout layout = PatternLayout.createLayout(msgPattern, ctx.getConfiguration(), null, null, null);
//FileOutputStream fos = new FileOutputStream(OUTPUT_FILE + "_mdc");
final FileAppender appender = FileAppender.createAppender(OUTPUT_FILE + "_mdc", "false", "false", "File", "false",
"true", "false", layout, null, "false", null, null);
appender.start();
// set appender on root and set level to debug
root.addAppender(appender);
root.setLevel(Level.DEBUG);
// output starting message
root.debug("starting mdc pattern test");
layout.setConversionPattern(mdcMsgPattern1);
root.debug("empty mdc, no key specified in pattern");
layout.setConversionPattern(mdcMsgPattern2);
root.debug("empty mdc, key1 in pattern");
layout.setConversionPattern(mdcMsgPattern3);
root.debug("empty mdc, key2 in pattern");
layout.setConversionPattern(mdcMsgPattern4);
root.debug("empty mdc, key3 in pattern");
layout.setConversionPattern(mdcMsgPattern5);
root.debug("empty mdc, key1, key2, and key3 in pattern");
ThreadContext.put("key1", "value1");
ThreadContext.put("key2", "value2");
layout.setConversionPattern(mdcMsgPattern1);
root.debug("filled mdc, no key specified in pattern");
layout.setConversionPattern(mdcMsgPattern2);
root.debug("filled mdc, key1 in pattern");
layout.setConversionPattern(mdcMsgPattern3);
root.debug("filled mdc, key2 in pattern");
layout.setConversionPattern(mdcMsgPattern4);
root.debug("filled mdc, key3 in pattern");
layout.setConversionPattern(mdcMsgPattern5);
root.debug("filled mdc, key1, key2, and key3 in pattern");
ThreadContext.remove("key1");
ThreadContext.remove("key2");
layout.setConversionPattern(msgPattern);
root.debug("finished mdc pattern test");
assertTrue(Compare.compare(this.getClass(), OUTPUT_FILE + "_mdc", WITNESS_FILE + "_mdc"));
root.removeAppender(appender);
appender.stop();
}
示例10: testConfig
import org.apache.logging.log4j.core.appender.FileAppender; //导入方法依赖的package包/类
@Test
public void testConfig() {
// don't bother using "error" since that's the default; try another level
final LoggerContext ctx = this.init.getLoggerContext();
ctx.reconfigure();
final Configuration config = ctx.getConfiguration();
assertThat(config, instanceOf(XmlConfiguration.class));
for (final StatusListener listener : StatusLogger.getLogger().getListeners()) {
if (listener instanceof StatusConsoleListener) {
assertSame(listener.getStatusLevel(), Level.INFO);
break;
}
}
final Layout<? extends Serializable> layout = PatternLayout.newBuilder()
.withPattern(PatternLayout.SIMPLE_CONVERSION_PATTERN)
.withConfiguration(config)
.build();
// @formatter:off
final FileAppender appender = FileAppender.newBuilder()
.withFileName(LOG_FILE)
.withAppend(false)
.withName("File")
.withIgnoreExceptions(false)
.withBufferSize(4000)
.withBufferedIo(false)
.withLayout(layout)
.build();
// @formatter:on
appender.start();
config.addAppender(appender);
final AppenderRef ref = AppenderRef.createAppenderRef("File", null, null);
final AppenderRef[] refs = new AppenderRef[] {ref};
final LoggerConfig loggerConfig = LoggerConfig.createLogger(false, Level.INFO, "org.apache.logging.log4j",
"true", refs, null, config, null );
loggerConfig.addAppender(appender, null, null);
config.addLogger("org.apache.logging.log4j", loggerConfig);
ctx.updateLoggers();
final Logger logger = ctx.getLogger(CustomConfigurationTest.class.getName());
logger.info("This is a test");
final File file = new File(LOG_FILE);
assertThat(file, exists());
assertThat(file, hasLength(greaterThan(0L)));
}