use of ch.qos.logback.classic.encoder.PatternLayoutEncoder in project pravega by pravega.
the class SelfTestRunner method setupLogging.
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();
}
use of ch.qos.logback.classic.encoder.PatternLayoutEncoder in project dubbo by alibaba.
the class LogbackContainer method doInitializer.
/**
* Initializer logback
*
* @param file
* @param level
* @param maxHistory
*/
private void doInitializer(String file, String level, int maxHistory) {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
rootLogger.detachAndStopAllAppenders();
// appender
RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>();
fileAppender.setContext(loggerContext);
fileAppender.setName("application");
fileAppender.setFile(file);
fileAppender.setAppend(true);
// policy
TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<ILoggingEvent>();
policy.setContext(loggerContext);
policy.setMaxHistory(maxHistory);
policy.setFileNamePattern(file + ".%d{yyyy-MM-dd}");
policy.setParent(fileAppender);
policy.start();
fileAppender.setRollingPolicy(policy);
// encoder
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern("%date [%thread] %-5level %logger (%file:%line\\) - %msg%n");
encoder.start();
fileAppender.setEncoder(encoder);
fileAppender.start();
rootLogger.addAppender(fileAppender);
rootLogger.setLevel(Level.toLevel(level));
rootLogger.setAdditive(false);
}
use of ch.qos.logback.classic.encoder.PatternLayoutEncoder in project coffeenet-starter by coffeenet.
the class CoffeeNetLoggingAutoConfiguration method rollingFileAppender.
private RollingFileAppender<ILoggingEvent> rollingFileAppender(LoggerContext loggerContext) {
RollingFileAppender<ILoggingEvent> rfAppender = new RollingFileAppender<>();
rfAppender.setName(COFFEENET_FILE_APPENDER);
rfAppender.setContext(loggerContext);
rfAppender.setFile(coffeeNetLoggingFileProperties.getFile());
// TimeBasedRollingPolicy
TimeBasedRollingPolicy rollingPolicy = new TimeBasedRollingPolicy();
rollingPolicy.setContext(loggerContext);
rollingPolicy.setParent(rfAppender);
rollingPolicy.setFileNamePattern(coffeeNetLoggingFileProperties.getFileNamePattern());
rollingPolicy.setMaxHistory(coffeeNetLoggingFileProperties.getMaxHistory());
rollingPolicy.start();
rfAppender.setRollingPolicy(rollingPolicy);
// PatternLayoutEncoder
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern(coffeeNetLoggingFileProperties.getPattern());
encoder.start();
rfAppender.setEncoder(encoder);
rfAppender.start();
return rfAppender;
}
use of ch.qos.logback.classic.encoder.PatternLayoutEncoder in project spring-boot by spring-projects.
the class LogbackConfigurationTests method filePatternCanBeOverridden.
@Test
void filePatternCanBeOverridden() throws JoranException {
JoranConfigurator configurator = new JoranConfigurator();
LoggerContext context = new LoggerContext();
configurator.setContext(context);
configurator.doConfigure(new File("src/test/resources/custom-file-log-pattern.xml"));
Appender<ILoggingEvent> appender = context.getLogger("ROOT").getAppender("FILE");
assertThat(appender).isInstanceOf(FileAppender.class);
Encoder<?> encoder = ((FileAppender<?>) appender).getEncoder();
assertThat(encoder).isInstanceOf(PatternLayoutEncoder.class);
assertThat(((PatternLayoutEncoder) encoder).getPattern()).isEqualTo("bar");
}
use of ch.qos.logback.classic.encoder.PatternLayoutEncoder in project BIMserver by opensourceBIM.
the class AbstractLocalDevBimServerStarter method fixLogging.
/**
* Add a file appender to every logger we can find (the loggers should already have been configured via logback.xml)
*
* @throws IOException
*/
private void fixLogging(BimServerConfig config) throws IOException {
Path logFolder = config.getHomeDir().resolve("logs");
if (!Files.isDirectory(logFolder)) {
Files.createDirectories(logFolder);
}
Path file = logFolder.resolve("bimserver.log");
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<ILoggingEvent>();
String filename = file.toAbsolutePath().toString();
if (lc instanceof LoggerContext) {
if (!lc.isStarted()) {
lc.start();
}
}
System.out.println("Logging to " + filename);
fileAppender.setFile(filename);
fileAppender.setEncoder(ple);
fileAppender.setContext(lc);
fileAppender.start();
for (ch.qos.logback.classic.Logger log : lc.getLoggerList()) {
if (log.getLevel() != null) {
log.addAppender(fileAppender);
}
}
}
Aggregations