use of ch.qos.logback.classic.encoder.PatternLayoutEncoder in project jlineup by otto-de.
the class Util method logToFile.
static void logToFile(String workingDir) {
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(workingDir + "/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);
}
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 engineblock by engineblock.
the class ScenarioLogger method start.
public ScenarioLogger start() {
if (!loggerDir.exists()) {
if (!loggerDir.mkdirs()) {
throw new RuntimeException("Unable to create logger directory:" + loggerDir.getPath());
}
}
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
// List<LoggerContextListener> copyOfListenerList = loggerContext.getCopyOfListenerList();
ConsoleAppender<ILoggingEvent> ca = new ConsoleAppender<>();
ca.setContext(loggerContext);
PatternLayoutEncoder ple = new PatternLayoutEncoder();
// ple.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n");
ple.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n");
ple.setContext(loggerContext);
ple.start();
String scenarioLog = loggerDir.getPath() + File.separator + scenario.getName() + ".log";
scenarioLog = scenarioLog.replaceAll("\\s", "_");
FileAppender<ILoggingEvent> fileAppender = new FileAppender<ILoggingEvent>();
fileAppender.setFile(scenarioLog);
fileAppender.setEncoder(ple);
fileAppender.setContext(loggerContext);
System.err.println("Logging to " + scenarioLog);
fileAppender.start();
Logger logger = (Logger) LoggerFactory.getLogger("ROOT");
logger.addAppender(fileAppender);
logger.setLevel(Level.INFO);
logger.setAdditive(true);
/* set to true if root should log too */
purgeOldFiles(logger);
return this;
}
use of ch.qos.logback.classic.encoder.PatternLayoutEncoder in project engineblock by engineblock.
the class ConsoleLogging method enableConsoleLogging.
public static void enableConsoleLogging(Level level, String loggingPattern) {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
// List<LoggerContextListener> copyOfListenerList = loggerContext.getCopyOfListenerList();
ConsoleAppender<ILoggingEvent> ca = new ConsoleAppender<>();
ca.setContext(loggerContext);
PatternLayoutEncoder ple = new PatternLayoutEncoder();
ple.setPattern(loggingPattern);
ple.setContext(loggerContext);
ple.start();
ca.setEncoder(ple);
LevelFilter levelFilter = new LevelFilter(level);
levelFilter.start();
ca.addFilter(levelFilter);
ca.start();
Logger root = loggerContext.getLogger("ROOT");
root.addAppender(ca);
root.setLevel(Level.TRACE);
}
Aggregations