本文整理汇总了Java中ch.qos.logback.core.FileAppender类的典型用法代码示例。如果您正苦于以下问题:Java FileAppender类的具体用法?Java FileAppender怎么用?Java FileAppender使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FileAppender类属于ch.qos.logback.core包,在下文中一共展示了FileAppender类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addAppender
import ch.qos.logback.core.FileAppender; //导入依赖的package包/类
/**
* Start a specific file logging, typically for the processing of a given book.
*
* @param name appender name (typically the book radix)
* @param logFolder target folder where the log file is to be written
*/
public static void addAppender (String name,
Path logFolder)
{
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(
Logger.ROOT_LOGGER_NAME);
FileAppender fileAppender = new FileAppender();
PatternLayoutEncoder fileEncoder = new PatternLayoutEncoder();
fileAppender.setName(name);
fileAppender.setContext(loggerContext);
fileAppender.setAppend(false);
String now = new SimpleDateFormat("yyyyMMdd'T'HHmm").format(new Date());
Path logFile = logFolder.resolve(name + "-" + now + ".log");
fileAppender.setFile(logFile.toAbsolutePath().toString());
fileEncoder.setContext(loggerContext);
fileEncoder.setPattern("%date %level [%X{BOOK}%X{SHEET}] %25file:%-4line | %msg%n%ex");
fileEncoder.start();
fileAppender.setEncoder(fileEncoder);
fileAppender.start();
root.addAppender(fileAppender);
}
示例2: addFileAppender
import ch.qos.logback.core.FileAppender; //导入依赖的package包/类
/**
* Add a specific appender meant for FILE.
*/
public static void addFileAppender ()
{
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(
Logger.ROOT_LOGGER_NAME);
FileAppender fileAppender = new FileAppender();
PatternLayoutEncoder fileEncoder = new PatternLayoutEncoder();
fileAppender.setName("FILE");
fileAppender.setContext(loggerContext);
fileAppender.setAppend(false);
String now = new SimpleDateFormat("yyyyMMdd'T'HHmmss").format(new Date());
Path logPath = WellKnowns.LOG_FOLDER.resolve(now + ".log").toAbsolutePath();
fileAppender.setFile(logPath.toString());
fileEncoder.setContext(loggerContext);
fileEncoder.setPattern("%date %-5level [%X{BOOK}%X{SHEET}] %25replace(%file){'\\.java$',''} %-4line | %msg%n%ex");
fileEncoder.start();
fileAppender.setEncoder(fileEncoder);
fileAppender.start();
root.addAppender(fileAppender);
System.out.println("LogUtil. Logging to " + logPath);
}
示例3: buildFileAppender
import ch.qos.logback.core.FileAppender; //导入依赖的package包/类
public FileAppender<ILoggingEvent> buildFileAppender(LoggerContext context, String file) {
FileAppender<ILoggingEvent> fileAppender = new FileAppender<>();
fileAppender.setFile(file);
fileAppender.setContext(context);
fileAppender.setPrudent(true);
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(context);
encoder.setPattern(primaryConfiguration.getLoggingPattern().or(baseConfiguration.getLoggingPattern()).or(JavaUtils.LOGBACK_LOGGING_PATTERN));
encoder.start();
fileAppender.setEncoder(encoder);
fileAppender.start();
return fileAppender;
}
示例4: logToFile
import ch.qos.logback.core.FileAppender; //导入依赖的package包/类
static void logToFile(Parameters parameters) {
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
PatternLayoutEncoder ple = new PatternLayoutEncoder();
ple.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n");
ple.setContext(lc);
ple.start();
FileAppender<ILoggingEvent> fileAppender = new FileAppender<>();
fileAppender.setFile(parameters.getWorkingDirectory() + "/jlineup.log");
fileAppender.setEncoder(ple);
fileAppender.setContext(lc);
fileAppender.start();
ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger)LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
logger.addAppender(fileAppender);
logger.setLevel(Level.DEBUG);
}
示例5: setupLogging
import ch.qos.logback.core.FileAppender; //导入依赖的package包/类
private static void setupLogging(TestConfig testConfig) {
val logFile = new java.io.File(testConfig.getTestLogPath());
if (logFile.delete()) {
TestLogger.log("Main", "Deleted log file %s.", logFile.getAbsolutePath());
}
// Configure slf4j to not log anything (console or whatever). This interferes with the console interaction.
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
context.getLoggerList().get(0).detachAndStopAllAppenders();
val fa = new FileAppender<ILoggingEvent>();
fa.setContext(context);
fa.setName("selftest");
fa.setFile(logFile.getAbsolutePath());
val encoder = new PatternLayoutEncoder();
encoder.setContext(context);
encoder.setPattern("%date{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %level - %msg%n");
encoder.start();
fa.setEncoder(encoder);
fa.start();
context.getLoggerList().get(0).addAppender(fa);
context.getLoggerList().get(0).setLevel(Level.INFO);
//context.reset();
}
示例6: configureAppender
import ch.qos.logback.core.FileAppender; //导入依赖的package包/类
private Appender<ILoggingEvent> configureAppender(LoggerContext loggerContext, String name, Config config)
throws ReflectiveOperationException {
List<Object> children = new ArrayList<>();
@SuppressWarnings("unchecked")
Class<Appender<ILoggingEvent>> clazz = (Class<Appender<ILoggingEvent>>) Class
.forName(config.getString("class"));
Appender<ILoggingEvent> appender = this.configureObject(loggerContext, clazz, config, children);
appender.setName(name);
for (Object child : children) {
if (child instanceof RollingPolicy) {
((RollingPolicy) child).setParent((FileAppender<?>) appender);
}
if (child instanceof LifeCycle) {
((LifeCycle) child).start();
}
}
appender.start();
return appender;
}
示例7: createLogger
import ch.qos.logback.core.FileAppender; //导入依赖的package包/类
private Logger createLogger(File logFile) {
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
ch.qos.logback.classic.Logger log = loggers.get();
log.setAdditive(false);
FileAppender fileAppender = new FileAppender();
fileAppender.setContext(context);
fileAppender.setName(Thread.currentThread().getName());
fileAppender.setFile(logFile.getAbsolutePath());
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(context);
encoder.setPattern(LOG_PATTERN);
encoder.start();
fileAppender.setEncoder(encoder);
fileAppender.start();
log.detachAndStopAllAppenders();
log.addAppender(fileAppender);
return log;
}
示例8: createLogger
import ch.qos.logback.core.FileAppender; //导入依赖的package包/类
/**
* Creates a new logger.
*
* @param name a name for the logger
* @param level the logging level for the logger
* @param file the file to output all logging to
* @return the created logger
*/
public static Logger createLogger(String name, Level level, String file) {
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
PatternLayoutEncoder ple = new PatternLayoutEncoder();
ple.setPattern("%date %level [%thread] %logger{10} [%file:%line]%n%msg%n%n");
ple.setContext(lc);
ple.start();
FileAppender<ILoggingEvent> fileAppender = new FileAppender<ILoggingEvent>();
fileAppender.setFile(file);
fileAppender.setEncoder(ple);
fileAppender.setAppend(false);
fileAppender.setContext(lc);
fileAppender.start();
logger = (Logger) LoggerFactory.getLogger(name);
logger.detachAndStopAllAppenders(); // detach console (doesn't seem to work)
logger.addAppender(fileAppender); // attach file appender
logger.setLevel(level);
logger.setAdditive(true); // set to true if root should log too
return logger;
}
示例9: smoke
import ch.qos.logback.core.FileAppender; //导入依赖的package包/类
@Test
public void smoke() {
String filename = CoreTestConstants.OUTPUT_DIR_PREFIX + "/fat-smoke.log";
FileAppender<Object> appender = new FileAppender<Object>();
appender.setEncoder(new DummyEncoder<Object>());
appender.setAppend(false);
appender.setFile(filename);
appender.setName("smoke");
appender.setContext(context);
appender.start();
appender.doAppend(new Object());
appender.stop();
File file = new File(filename);
assertTrue(file.exists());
assertTrue("failed to delete " + file.getAbsolutePath(), file.delete());
}
示例10: testCreateParentFolders
import ch.qos.logback.core.FileAppender; //导入依赖的package包/类
@Test
public void testCreateParentFolders() {
String filename = CoreTestConstants.OUTPUT_DIR_PREFIX + "/fat-testCreateParentFolders-" + diff
+ "/testCreateParentFolders.txt";
File file = new File(filename);
assertFalse(file.getParentFile().exists());
assertFalse(file.exists());
FileAppender<Object> appender = new FileAppender<Object>();
appender.setEncoder(new DummyEncoder<Object>());
appender.setAppend(false);
appender.setFile(filename);
appender.setName("testCreateParentFolders");
appender.setContext(context);
appender.start();
appender.doAppend(new Object());
appender.stop();
assertTrue(file.getParentFile().exists());
assertTrue(file.exists());
// cleanup
assertTrue("failed to delete " + file.getAbsolutePath(), file.delete());
File parent = file.getParentFile();
assertTrue("failed to delete " + parent.getAbsolutePath(), parent.delete());
}
示例11: buildLoggerContext
import ch.qos.logback.core.FileAppender; //导入依赖的package包/类
static LoggerContext buildLoggerContext(String stamp, String filename,
boolean safetyMode) {
LoggerContext loggerContext = new LoggerContext();
FileAppender<ILoggingEvent> fa = new FileAppender<ILoggingEvent>();
PatternLayoutEncoder patternLayout = new PatternLayoutEncoder();
patternLayout.setPattern(stamp + " %5p - %m%n");
patternLayout.setContext(loggerContext);
patternLayout.start();
fa.setEncoder(patternLayout);
fa.setFile(filename);
fa.setAppend(true);
fa.setPrudent(safetyMode);
fa.setContext(loggerContext);
fa.start();
ch.qos.logback.classic.Logger root = loggerContext
.getLogger(Logger.ROOT_LOGGER_NAME);
root.addAppender(fa);
return loggerContext;
}
示例12: buildLoggerContext
import ch.qos.logback.core.FileAppender; //导入依赖的package包/类
static LoggerContext buildLoggerContext(String filename, boolean safetyMode) {
LoggerContext loggerContext = new LoggerContext();
FileAppender<ILoggingEvent> fa = new FileAppender<ILoggingEvent>();
PatternLayoutEncoder patternLayout = new PatternLayoutEncoder();
patternLayout.setPattern("%5p %c - %m%n");
patternLayout.setContext(loggerContext);
patternLayout.start();
fa.setEncoder(patternLayout);
fa.setFile(filename);
fa.setAppend(false);
fa.setPrudent(safetyMode);
fa.setContext(loggerContext);
fa.start();
ch.qos.logback.classic.Logger root = loggerContext
.getLogger(Logger.ROOT_LOGGER_NAME);
root.addAppender(fa);
return loggerContext;
}
示例13: conditionalConsoleApp_IF_THEN_True
import ch.qos.logback.core.FileAppender; //导入依赖的package包/类
@SuppressWarnings("rawtypes")
@Test
public void conditionalConsoleApp_IF_THEN_True() throws JoranException,
IOException, InterruptedException {
InetAddress localhost = InetAddress.getLocalHost();
System.out.println("In conditionalConsoleApp_IF_THEN_True, canonicalHostName=\"" + localhost.getCanonicalHostName() + "] and hostNmae=\"" + localhost.getHostName() + "\"");
context.putProperty("aHost", localhost.getHostName());
String configFileAsStr = ClassicTestConstants.JORAN_INPUT_PREFIX
+ "conditional/conditionalConsoleApp.xml";
configure(configFileAsStr);
FileAppender fileAppender = (FileAppender) root.getAppender("FILE");
assertNotNull(fileAppender);
ConsoleAppender consoleAppender = (ConsoleAppender) root.getAppender("CON");
assertNotNull(consoleAppender);
StatusChecker checker = new StatusChecker(context);
checker.assertIsErrorFree();
}
示例14: conditionalConsoleApp_IF_THEN_False
import ch.qos.logback.core.FileAppender; //导入依赖的package包/类
@SuppressWarnings("rawtypes")
@Test
public void conditionalConsoleApp_IF_THEN_False() throws JoranException,
IOException, InterruptedException {
String configFileAsStr = ClassicTestConstants.JORAN_INPUT_PREFIX
+ "conditional/conditionalConsoleApp.xml";
configure(configFileAsStr);
FileAppender fileAppender = (FileAppender) root.getAppender("FILE");
assertNotNull(fileAppender);
ConsoleAppender consoleAppender = (ConsoleAppender) root.getAppender("CON");
assertNull(consoleAppender);
StatusChecker checker = new StatusChecker(context);
checker.assertIsErrorFree();
}
示例15: conditionalConsoleApp_IF_THEN_ELSE
import ch.qos.logback.core.FileAppender; //导入依赖的package包/类
@SuppressWarnings("rawtypes")
@Test
public void conditionalConsoleApp_IF_THEN_ELSE() throws JoranException,
IOException, InterruptedException {
String configFileAsStr = ClassicTestConstants.JORAN_INPUT_PREFIX
+ "conditional/conditionalConsoleApp_ELSE.xml";
configure(configFileAsStr);
FileAppender fileAppender = (FileAppender) root.getAppender("FILE");
assertNotNull(fileAppender);
ConsoleAppender consoleAppender = (ConsoleAppender) root.getAppender("CON");
assertNull(consoleAppender);
ListAppender listAppender = (ListAppender) root.getAppender("LIST");
assertNotNull(listAppender);
// StatusPrinter.printIfErrorsOccured(context);
StatusChecker checker = new StatusChecker(context);
checker.assertIsErrorFree();
}