本文整理汇总了Java中org.apache.logging.log4j.core.appender.FileAppender类的典型用法代码示例。如果您正苦于以下问题:Java FileAppender类的具体用法?Java FileAppender怎么用?Java FileAppender使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FileAppender类属于org.apache.logging.log4j.core.appender包,在下文中一共展示了FileAppender类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: registerLogFileTailThreads
import org.apache.logging.log4j.core.appender.FileAppender; //导入依赖的package包/类
private void registerLogFileTailThreads() throws IOException {
final Collection<String> outputFileNames = new HashSet<>();
final Collection<Appender> loggerAppenders = this.loggerContext.getConfiguration().getAppenders().values();
loggerAppenders.forEach(appender -> {
if (appender instanceof FileAppender) {
outputFileNames.add(((FileAppender) appender).getFileName());
} else if (appender instanceof RandomAccessFileAppender) {
outputFileNames.add(((RandomAccessFileAppender) appender).getFileName());
} else if (appender instanceof RollingFileAppender) {
outputFileNames.add(((RollingFileAppender) appender).getFileName());
} else if (appender instanceof MemoryMappedFileAppender) {
outputFileNames.add(((MemoryMappedFileAppender) appender).getFileName());
} else if (appender instanceof RollingRandomAccessFileAppender) {
outputFileNames.add(((RollingRandomAccessFileAppender) appender).getFileName());
}
});
outputFileNames.forEach(s -> {
final Tailer t = new Tailer(new File(s), new LogTailerListener(), 100, false, true);
final Thread thread = new Thread(t);
thread.setPriority(Thread.MIN_PRIORITY);
thread.setName(s);
thread.start();
});
}
示例2: Log4j2LoggerAdapter
import org.apache.logging.log4j.core.appender.FileAppender; //导入依赖的package包/类
public Log4j2LoggerAdapter() {
try {
org.apache.logging.log4j.Logger logger = LogManager.getLogger();
if (logger != null) {
Map<String, Appender> appenderMap = ((org.apache.logging.log4j.core.Logger) logger).getAppenders();
for (Map.Entry<String, Appender> entry : appenderMap.entrySet()) {
if (entry.getValue() instanceof FileAppender) {
String filename = ((FileAppender) entry.getValue()).getFileName();
file = new File(filename);
break;
}
}
}
} catch (Throwable t) {
}
}
示例3: 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);
}
示例4: extractFileName
import org.apache.logging.log4j.core.appender.FileAppender; //导入依赖的package包/类
/**
* File appender types do not share a "write-to-file" superclass.
*/
private String extractFileName(Appender a) {
if (a instanceof FileAppender) {
return ((FileAppender) a).getFileName();
} else if (a instanceof RollingFileAppender) {
return ((RollingFileAppender) a).getFileName();
} else if (a instanceof RollingRandomAccessFileAppender) {
return ((RollingRandomAccessFileAppender) a).getFileName();
} else if (a instanceof RandomAccessFileAppender) {
return ((RandomAccessFileAppender) a).getFileName();
} else if (a instanceof MemoryMappedFileAppender) {
return ((MemoryMappedFileAppender) a).getFileName();
} else {
return null;
}
}
示例5: 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();
}
示例6: addFileAppender
import org.apache.logging.log4j.core.appender.FileAppender; //导入依赖的package包/类
private static void addFileAppender() throws IOException {
final String tempDir = System.getProperty("java.io.tmpdir");
final File logFile = new File(tempDir, "meghanada_server.log");
final LoggerContext context = (LoggerContext) LogManager.getContext(false);
final Configuration configuration = context.getConfiguration();
final LoggerConfig loggerConfig = configuration.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
final FileAppender fileAppender =
FileAppender.newBuilder()
.withName("file")
.withLayout(
PatternLayout.newBuilder()
.withPattern("[%d][%-5.-5p][%-14.-14c{1}:%4L] %-22.-22M - %m%n")
.build())
.withFileName(logFile.getCanonicalPath())
.build();
configuration.addAppender(fileAppender);
loggerConfig.addAppender(fileAppender, Level.ERROR, null);
context.updateLoggers();
}
示例7: 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();
}
}
示例8: configureLogging
import org.apache.logging.log4j.core.appender.FileAppender; //导入依赖的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();
}
示例9: 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();
}
示例10: initTargetAppender
import org.apache.logging.log4j.core.appender.FileAppender; //导入依赖的package包/类
@Override
public void initTargetAppender() {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
// get log file path from existing file appender
RollingRandomAccessFileAppender mainFile =
(RollingRandomAccessFileAppender) ctx.getConfiguration().getAppender("mainFile");
String pathPrefix = mainFile == null
? System.getProperty("user.dir") + StringUtils.replace("/target/log", "/", File.separator)
+ File.separator
: StringUtils.replace(mainFile.getFileName(), "core.log", StringUtils.EMPTY);
targetAppender = FileAppender.newBuilder()
.withName(getTargetAppenderName())
.withAppend(true)
.withFileName(pathPrefix + getTargetAppenderName() + ".log")
.withLayout(
PatternLayout.newBuilder()
.withPattern("%d{HH:mm:ss.SSS} %-5level %logger - %msg%n")
.build()).
build();
}
示例11: initTargetAppender
import org.apache.logging.log4j.core.appender.FileAppender; //导入依赖的package包/类
@Override
public void initTargetAppender() {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
// get log file path from existing file appender
RollingRandomAccessFileAppender mainFile =
(RollingRandomAccessFileAppender) ctx.getConfiguration().getAppender("mainFile");
String pathPrefix = StringUtils.replace(mainFile.getFileName(), "core.log", StringUtils.EMPTY);
targetAppender = FileAppender.newBuilder().
withName(getTargetAppenderName()).
withAppend(true).
withFileName(pathPrefix + getTargetAppenderName() + ".log").
withLayout(
PatternLayout.newBuilder()
.withPattern("%d{HH:mm:ss.SSS} %-5level %logger - %msg%n")
.build())
.build();
}
示例12: 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();
}
示例13: createFileAppender
import org.apache.logging.log4j.core.appender.FileAppender; //导入依赖的package包/类
private void createFileAppender(DiagnosticContext ctx) {
String logDir = ctx.getTempDir() + SystemProperties.fileSeparator + "diagnostics.log";
final LoggerContext context = (LoggerContext) LogManager.getContext(false);
final Configuration config = context.getConfiguration();
Layout layout = PatternLayout.createLayout("%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n", null, config, null,
null,true, true, null, null );
Appender appender = FileAppender.createAppender(logDir, "false", "false", "File", "true",
"false", "false", "0", layout, null, "false", null, config);
appender.start();
config.addAppender(appender);
AppenderRef ref = AppenderRef.createAppenderRef("File", null, null);
AppenderRef[] refs = new AppenderRef[] {ref};
config.getRootLogger().addAppender(appender, null, null);
context.updateLoggers();
Logger testLog = LogManager.getLogger();
testLog.error("Testing");
}
示例14: testConfiguration
import org.apache.logging.log4j.core.appender.FileAppender; //导入依赖的package包/类
@Test
public void testConfiguration() {
final Configuration configuration = context.getConfiguration();
assertNotNull(configuration);
final FileAppender appender = configuration.getAppender("info");
assertNotNull(appender);
final CompositeFilter compFilter = (CompositeFilter) appender.getFilter();
assertNotNull(compFilter);
final Filter[] filters = compFilter.getFiltersArray();
assertNotNull(filters);
boolean foundLevel = false;
for (final Filter filter : filters) {
final ThresholdFilter tFilter = (ThresholdFilter) filter;
if (infom1Level.equals(tFilter.getLevel())) {
foundLevel = true;
break;
}
}
Assert.assertTrue("Level not found: " + infom1Level, foundLevel);
}
示例15: createFileAppender
import org.apache.logging.log4j.core.appender.FileAppender; //导入依赖的package包/类
public static FileAppender createFileAppender(String name, String fileName, String pattern){
PatternLayout layout = createLayout(pattern);
return FileAppender.newBuilder()
.withFileName(fileName)
.withName(name)
.withLayout(layout)
.build();
}