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);
logLevelOverrides.forEach((loggerName, loggingLevel) -> {
logger.debug("Overriding log level for " + loggerName + " to " + loggingLevel);
Logger toOverride = (Logger) LoggerFactory.getLogger(loggerName);
toOverride.setLevel(loggingLevel);
toOverride.debug("Log level was set to " + loggingLevel + " by engineblock CLI option.");
});
logger.setLevel(logLevel);
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<ILoggingEvent> levelFilter = new LevelFilter<>(level);
levelFilter.start();
ca.addFilter(levelFilter);
ca.start();
Logger root = loggerContext.getLogger("ROOT");
root.addAppender(ca);
root.setLevel(Level.TRACE);
}
use of ch.qos.logback.classic.encoder.PatternLayoutEncoder in project qpid-broker-j by apache.
the class AppenderUtils method configureRollingFileAppender.
public static void configureRollingFileAppender(FileLoggerSettings fileLoggerSettings, Context loggerContext, RollingFileAppender<ILoggingEvent> appender) {
String fileName = fileLoggerSettings.getFileName();
File file = new File(fileName);
if (file.getParentFile() != null) {
file.getParentFile().mkdirs();
}
validateLogFilePermissions(file);
validateMaxFileSize(fileLoggerSettings.getMaxFileSize());
appender.setFile(fileName);
appender.setAppend(true);
appender.setContext(loggerContext);
TriggeringPolicy triggeringPolicy;
RollingPolicyBase rollingPolicy;
final String maxFileSizeAsString = String.valueOf(fileLoggerSettings.getMaxFileSize()) + "MB";
if (fileLoggerSettings.isRollDaily()) {
DailyTriggeringPolicy dailyTriggeringPolicy = new DailyTriggeringPolicy(fileLoggerSettings.isRollOnRestart(), maxFileSizeAsString);
dailyTriggeringPolicy.setContext(loggerContext);
TimeBasedRollingPolicy<ILoggingEvent> timeBasedRollingPolicy = new TimeBasedRollingPolicy<>();
timeBasedRollingPolicy.setMaxHistory(fileLoggerSettings.getMaxHistory());
timeBasedRollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(dailyTriggeringPolicy);
timeBasedRollingPolicy.setFileNamePattern(fileName + ".%d{yyyy-MM-dd}.%i" + (fileLoggerSettings.isCompressOldFiles() ? ".gz" : ""));
rollingPolicy = timeBasedRollingPolicy;
triggeringPolicy = dailyTriggeringPolicy;
} else {
SizeTriggeringPolicy sizeTriggeringPolicy = new SizeTriggeringPolicy(fileLoggerSettings.isRollOnRestart(), maxFileSizeAsString);
sizeTriggeringPolicy.setContext(loggerContext);
SimpleRollingPolicy simpleRollingPolicy = new SimpleRollingPolicy(fileLoggerSettings.getMaxHistory());
simpleRollingPolicy.setFileNamePattern(fileName + ".%i" + (fileLoggerSettings.isCompressOldFiles() ? ".gz" : ""));
rollingPolicy = simpleRollingPolicy;
triggeringPolicy = sizeTriggeringPolicy;
}
rollingPolicy.setContext(loggerContext);
RollingPolicyDecorator decorator = new RollingPolicyDecorator(rollingPolicy, fileLoggerSettings.getRolloverListener(), fileLoggerSettings.getExecutorService());
decorator.setParent(appender);
appender.setRollingPolicy(decorator);
appender.setTriggeringPolicy(triggeringPolicy);
decorator.start();
triggeringPolicy.start();
final PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setPattern(fileLoggerSettings.getLayout());
encoder.setContext(loggerContext);
encoder.start();
appender.setEncoder(encoder);
}
use of ch.qos.logback.classic.encoder.PatternLayoutEncoder in project cdap-ingest by caskdata.
the class TailerLogUtils method getTimeLogger.
public static Logger getTimeLogger(String file) {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
RollingFileAppender fileAppender = new RollingFileAppender();
fileAppender.setContext(loggerContext);
fileAppender.setFile(file);
fileAppender.setAppend(true);
TimeBasedRollingPolicy rollingPolicy = new TimeBasedRollingPolicy();
rollingPolicy.setContext(loggerContext);
rollingPolicy.setParent(fileAppender);
rollingPolicy.setFileNamePattern(file + "%d{yyyy-MM-dd_HH-mm}");
rollingPolicy.start();
fileAppender.setRollingPolicy(rollingPolicy);
PatternLayoutEncoder layoutEncoder = new PatternLayoutEncoder();
layoutEncoder.setContext(loggerContext);
layoutEncoder.setPattern("[%d %-5p %c{1}] %msg%n");
layoutEncoder.start();
fileAppender.setEncoder(layoutEncoder);
fileAppender.start();
// configures the root logger
Logger rootLogger = loggerContext.getLogger(BaseTailerTest.class.getName() + "time");
rootLogger.setLevel(Level.DEBUG);
rootLogger.addAppender(fileAppender);
return rootLogger;
}
use of ch.qos.logback.classic.encoder.PatternLayoutEncoder in project cdap-ingest by caskdata.
the class FileTailerMetricsProcessor method initAppender.
/**
* Initializes the file appender.
*
* @param path the path to the directory for the metrics file
* @param fileName the name of the metrics file
* @return the initialized file appender
*/
private RollingFileAppender initAppender(String path, String fileName) {
LOG.debug("Starting initialize rolling file appender");
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
RollingFileAppender fileAppender = new RollingFileAppender();
fileAppender.setContext(loggerContext);
fileAppender.setFile(path + "/" + fileName);
fileAppender.setAppend(true);
TimeBasedRollingPolicy rollingPolicy = new TimeBasedRollingPolicy();
rollingPolicy.setContext(loggerContext);
rollingPolicy.setParent(fileAppender);
rollingPolicy.setFileNamePattern(path + "/" + fileName + ".%d");
rollingPolicy.start();
fileAppender.setRollingPolicy(rollingPolicy);
PatternLayoutEncoder layoutEncoder = new PatternLayoutEncoder();
layoutEncoder.setContext(loggerContext);
layoutEncoder.setPattern("%msg%n");
layoutEncoder.start();
fileAppender.setEncoder(layoutEncoder);
fileAppender.start();
return fileAppender;
}
Aggregations