本文整理汇总了Java中ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy类的典型用法代码示例。如果您正苦于以下问题:Java SizeBasedTriggeringPolicy类的具体用法?Java SizeBasedTriggeringPolicy怎么用?Java SizeBasedTriggeringPolicy使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SizeBasedTriggeringPolicy类属于ch.qos.logback.core.rolling包,在下文中一共展示了SizeBasedTriggeringPolicy类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: fileAppender
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy; //导入依赖的package包/类
private Appender<ILoggingEvent> fileAppender(LogbackConfigurator config,
String logFile) {
RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<ILoggingEvent>();
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
String logPattern = this.patterns.getProperty("file", FILE_LOG_PATTERN);
encoder.setPattern(OptionHelper.substVars(logPattern, config.getContext()));
appender.setEncoder(encoder);
config.start(encoder);
appender.setFile(logFile);
FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
rollingPolicy.setFileNamePattern(logFile + ".%i");
appender.setRollingPolicy(rollingPolicy);
rollingPolicy.setParent(appender);
config.start(rollingPolicy);
SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<ILoggingEvent>();
triggeringPolicy.setMaxFileSize("10MB");
appender.setTriggeringPolicy(triggeringPolicy);
config.start(triggeringPolicy);
config.appender("FILE", appender);
return appender;
}
示例2: configureFileLogging
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy; //导入依赖的package包/类
private static OutputStreamAppender<ILoggingEvent> configureFileLogging(File logFile, LoggerContext loggerContext) {
RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<ILoggingEvent>();
rollingFileAppender.setFile(logFile.getAbsolutePath());
rollingFileAppender.setName("com.documents4j.logger.client.file");
rollingFileAppender.setContext(loggerContext);
FixedWindowRollingPolicy fixedWindowRollingPolicy = new FixedWindowRollingPolicy();
fixedWindowRollingPolicy.setFileNamePattern(logFile.getAbsolutePath() + ".%i.gz");
fixedWindowRollingPolicy.setMaxIndex(LogDescription.MAXIMUM_LOG_HISTORY_INDEX);
fixedWindowRollingPolicy.setContext(loggerContext);
fixedWindowRollingPolicy.setParent(rollingFileAppender);
SizeBasedTriggeringPolicy<ILoggingEvent> sizeBasedTriggeringPolicy = new SizeBasedTriggeringPolicy<ILoggingEvent>();
sizeBasedTriggeringPolicy.setMaxFileSize(LogDescription.MAXIMUM_LOG_FILE_SIZE);
sizeBasedTriggeringPolicy.setContext(loggerContext);
rollingFileAppender.setRollingPolicy(fixedWindowRollingPolicy);
rollingFileAppender.setTriggeringPolicy(sizeBasedTriggeringPolicy);
sizeBasedTriggeringPolicy.start();
fixedWindowRollingPolicy.start();
System.out.println("Logging: The log is written to " + logFile);
return rollingFileAppender;
}
示例3: configureFileLogging
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy; //导入依赖的package包/类
private static OutputStreamAppender<ILoggingEvent> configureFileLogging(File logFile, LoggerContext loggerContext) {
RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<ILoggingEvent>();
rollingFileAppender.setFile(logFile.getAbsolutePath());
rollingFileAppender.setName("com.documents4j.logger.server.file");
rollingFileAppender.setContext(loggerContext);
FixedWindowRollingPolicy fixedWindowRollingPolicy = new FixedWindowRollingPolicy();
fixedWindowRollingPolicy.setFileNamePattern(logFile.getAbsolutePath() + ".%i.gz");
fixedWindowRollingPolicy.setMaxIndex(LogDescription.MAXIMUM_LOG_HISTORY_INDEX);
fixedWindowRollingPolicy.setContext(loggerContext);
fixedWindowRollingPolicy.setParent(rollingFileAppender);
SizeBasedTriggeringPolicy<ILoggingEvent> sizeBasedTriggeringPolicy = new SizeBasedTriggeringPolicy<ILoggingEvent>();
sizeBasedTriggeringPolicy.setMaxFileSize(LogDescription.MAXIMUM_LOG_FILE_SIZE);
sizeBasedTriggeringPolicy.setContext(loggerContext);
rollingFileAppender.setRollingPolicy(fixedWindowRollingPolicy);
rollingFileAppender.setTriggeringPolicy(sizeBasedTriggeringPolicy);
sizeBasedTriggeringPolicy.start();
fixedWindowRollingPolicy.start();
System.out.println("Logging: The log is written to " + logFile);
return rollingFileAppender;
}
示例4: createLogAppender
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy; //导入依赖的package包/类
/**
* Create an appender that will roll over based on size
*
* @param context
* @param encoder
* @return An appender that matches the set up of the logger builder
*/
private RollingFileAppender<ILoggingEvent> createLogAppender(LoggerContext context, Encoder<ILoggingEvent> encoder){
RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
appender.setEncoder(encoder);
appender.setFile(file);
FixedWindowRollingPolicy rolling = new FixedWindowRollingPolicy();
rolling.setParent(appender);
rolling.setContext(context);
//Set the maximum number of logs, either to the user specified setting or default to 1
rolling.setMinIndex(1);
if (maxNumberLogs.isPresent() && maxNumberLogs.get() >= 0) {
rolling.setMaxIndex(maxNumberLogs.get());
} else {
rolling.setMaxIndex(1);
}
rolling.setFileNamePattern(getFileWithPattern("%i"));
//Configure size based rolling
SizeBasedTriggeringPolicy<ILoggingEvent> trigger = new SizeBasedTriggeringPolicy<>();
trigger.setMaxFileSize(getMaxFileSize());
trigger.setContext(context);
rolling.start();
trigger.start();
appender.setRollingPolicy(rolling);
appender.setTriggeringPolicy(trigger);
return appender;
}
示例5: createTriggeringPolicy
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy; //导入依赖的package包/类
@Override
public TriggeringPolicy<ILoggingEvent> createTriggeringPolicy(LoggerContext context) {
SizeBasedTriggeringPolicy<ILoggingEvent> policy = new SizeBasedTriggeringPolicy<ILoggingEvent>();
if (fileSize != null && fileSize.length() > 0) {
policy.setMaxFileSize(FileSize.valueOf(fileSize));
}
policy.setContext(context);
return policy;
}
示例6: getFileAppender
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy; //导入依赖的package包/类
/**
* Creates the file appender for the logger with a custom logging policy
* (RollingFileAppender with max size of 1 MB and up to 3 backups)
* @param logFile the file to log into
* @return the FileAppender for the specified File and current logging
* context
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected static FileAppender getFileAppender(File logFile) {
// init rolling file appender
RollingFileAppender rfAppender = new RollingFileAppender();
rfAppender.setContext(lc);
rfAppender.setFile(logFile.getAbsolutePath());
rfAppender.setAppend(true);
// rolling policy: keep up to 3 rollover-files with postfix .%i.log
FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
rollingPolicy.setContext(lc);
rollingPolicy.setParent(rfAppender);
rollingPolicy.setFileNamePattern(logFileName + ".%i.log");
rollingPolicy.setMinIndex(1);
rollingPolicy.setMaxIndex(3);
rollingPolicy.start();
// rollover after logfixe exceeds 1MB
SizeBasedTriggeringPolicy triggeringPolicy = new SizeBasedTriggeringPolicy();
triggeringPolicy.setMaxFileSize(FileSize.valueOf("1mb"));
triggeringPolicy.start();
// layout of the log entries
Encoder encoder = getDefaultEncoder();
// apply settings and start appender
rfAppender.setEncoder(encoder);
rfAppender.setRollingPolicy(rollingPolicy);
rfAppender.setTriggeringPolicy(triggeringPolicy);
rfAppender.start();
return rfAppender;
}
示例7: initLogback
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy; //导入依赖的package包/类
/**
* Initialize Logback
*/
private static void initLogback() {
// reset logger context
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
context.reset();
// shared console appender
ConsoleAppender.setContext(context);
ConsoleAppender.setTarget("System.out");
PatternLayoutEncoder consoleEncoder = new PatternLayoutEncoder();
consoleEncoder.setContext(context);
consoleEncoder.setPattern(LogPattern);
consoleEncoder.start();
ConsoleAppender.setEncoder(consoleEncoder);
ConsoleAppender.start();
// rolling file
String logFile = getFilePath() + File.separatorChar + getFileName();
FileAppender.setContext(context);
FileAppender.setFile(logFile);
// log pattern
PatternLayoutEncoder fileEncoder = new PatternLayoutEncoder();
fileEncoder.setContext(context);
fileEncoder.setPattern(LogPattern);
fileEncoder.start();
FileAppender.setEncoder(fileEncoder);
// rolling policy
FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
rollingPolicy.setContext(context);
rollingPolicy.setParent(FileAppender);
rollingPolicy.setFileNamePattern(logFile + "%i.zip");
rollingPolicy.setMinIndex(1);
rollingPolicy.setMaxIndex(1);
rollingPolicy.start();
// file max size - if fileLimit is 0, set max file size to maximum allowed
long fileLimit = getFileLimitInKBytes() != 0 ? getFileLimitInKBytes() * 1024 : Long.MAX_VALUE;
SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<ILoggingEvent>(String.valueOf(fileLimit));
triggeringPolicy.start();
FileAppender.setRollingPolicy(rollingPolicy);
FileAppender.setTriggeringPolicy(triggeringPolicy);
FileAppender.start();
}
示例8: init
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy; //导入依赖的package包/类
private void init(FileLoggingSetup setup)
{
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
lc.reset();
// 1) FileLoggingSetup - Encoder for File
PatternLayoutEncoder encoder1 = new PatternLayoutEncoder();
encoder1.setContext(lc);
encoder1.setPattern(setup.mLogPattern);
encoder1.start();
// 2) FileLoggingSetup - rolling file appender
RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<ILoggingEvent>();
rollingFileAppender.setAppend(true);
rollingFileAppender.setContext(lc);
//rollingFileAppender.setFile(setup.mFolder + "/" + setup.mFileName + "." + setup.mFileExtension);
// 3) FileLoggingSetup - Rolling policy (one log per day)
TriggeringPolicy<ILoggingEvent> triggeringPolicy = null;
switch (setup.mMode)
{
case DateFiles:
{
TimeBasedRollingPolicy timeBasedRollingPolicy = new TimeBasedRollingPolicy<ILoggingEvent>();
timeBasedRollingPolicy.setFileNamePattern(setup.mFolder + "/" + setup.mFileName + "_%d{yyyyMMdd}." + setup.mFileExtension);
timeBasedRollingPolicy.setMaxHistory(setup.mLogsToKeep);
timeBasedRollingPolicy.setCleanHistoryOnStart(true);
timeBasedRollingPolicy.setParent(rollingFileAppender);
timeBasedRollingPolicy.setContext(lc);
triggeringPolicy = timeBasedRollingPolicy;
break;
}
case NumberedFiles:
{
FixedWindowRollingPolicy fixedWindowRollingPolicy = new FixedWindowRollingPolicy();
fixedWindowRollingPolicy.setFileNamePattern(setup.mFolder + "/" + setup.mFileName + "%i." + setup.mFileExtension);
fixedWindowRollingPolicy.setMinIndex(1);
fixedWindowRollingPolicy.setMaxIndex(setup.mLogsToKeep);
fixedWindowRollingPolicy.setParent(rollingFileAppender);
fixedWindowRollingPolicy.setContext(lc);
SizeBasedTriggeringPolicy<ILoggingEvent> sizeBasedTriggeringPolicy = new SizeBasedTriggeringPolicy<>();
sizeBasedTriggeringPolicy.setMaxFileSize(setup.mNumberedFileSizeLimit);
triggeringPolicy = sizeBasedTriggeringPolicy;
rollingFileAppender.setFile(FileLoggingUtil.getDefaultLogFile(setup));
rollingFileAppender.setRollingPolicy(fixedWindowRollingPolicy);
fixedWindowRollingPolicy.start();
break;
}
}
triggeringPolicy.start();
rollingFileAppender.setTriggeringPolicy(triggeringPolicy);
rollingFileAppender.setEncoder(encoder1);
rollingFileAppender.start();
// add the newly created appenders to the root logger;
// qualify Logger to disambiguate from org.slf4j.Logger
ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) mLogger;
root.detachAndStopAllAppenders();
root.addAppender(rollingFileAppender);
}
示例9: configureLogger
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy; //导入依赖的package包/类
private void configureLogger() {
File targetFile = new File(LOGFILE_PATH);
// reset the default context (which may already have been initialized)
// since we want to reconfigure it
LoggerContext lc = (LoggerContext)LoggerFactory.getILoggerFactory();
lc.reset();
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(lc);
encoder.setPattern("%d{yyyy-MM-dd HH:mm:ss.SSS} : %-5level : %msg%n");
encoder.start();
String filePattern = targetFile.getParent() + "/" + targetFile.getName() + ".%i.zip";
FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
rollingPolicy.setFileNamePattern(filePattern);
rollingPolicy.setMinIndex(1);
rollingPolicy.setMaxIndex(3);
rollingPolicy.setContext(lc);
SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<ILoggingEvent>();
triggeringPolicy.setMaxFileSize("5MB");
triggeringPolicy.setContext(lc);
RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<ILoggingEvent>();
rollingFileAppender.setAppend(true);
rollingFileAppender.setName("FILE");
rollingFileAppender.setContext(lc);
rollingFileAppender.setFile(LOGFILE_PATH);
rollingFileAppender.setRollingPolicy(rollingPolicy);
rollingFileAppender.setTriggeringPolicy(triggeringPolicy);
rollingFileAppender.setEncoder(encoder);
triggeringPolicy.start();
rollingPolicy.setParent(rollingFileAppender);
rollingPolicy.start();
rollingFileAppender.start();
// Logcat appender
LogcatAppender logcatAppender = new LogcatAppender();
logcatAppender.setContext(lc);
logcatAppender.setName("LOGCAT");
logcatAppender.setEncoder(encoder);
logcatAppender.start();
// add the newly created appenders to the root logger;
// qualify Logger to disambiguate from org.slf4j.Logger
ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
root.setLevel(Level.ALL);
root.addAppender(rollingFileAppender);
root.addAppender(logcatAppender);
}
示例10: LogConfigurator
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy; //导入依赖的package包/类
public LogConfigurator(File f) {
Appender appender = null;
if (f != null) {
try {
String line = Files.readFirstLine(f, Charset.defaultCharset()).trim();
switch (line) {
case "rolling":
String logFile = workingDirectory();
message = ("rolling to file://{}" + logFile);
RollingFileAppender r = new RollingFileAppender();
r.setFile(logFile);
r.setAppend(true);
SizeBasedTriggeringPolicy triggeringPolicy = new ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy();
triggeringPolicy.setMaxFileSize(FileSize.valueOf("5MB"));
triggeringPolicy.start();
FixedWindowRollingPolicy policy = new FixedWindowRollingPolicy();
policy.setFileNamePattern("log.%i.gz");
policy.setParent(r);
policy.setContext(LOG.getLoggerContext());
policy.start();
r.setEncoder(logEncoder());
r.setRollingPolicy(policy);
r.setTriggeringPolicy(triggeringPolicy);
appender = r;
break;
//TODO TCP/UDP etc
}
} catch (IOException e) {
logger.error("{}", e);
appender = null;
}
}
if (appender == null) {
//default
ConsoleAppender a = new ConsoleAppender();
a.setEncoder(logEncoder());
appender = a;
message = "ConsoleAppender";
}
LOG.detachAndStopAllAppenders();
appender.setContext(LOG.getLoggerContext());
appender.start();
LOG.addAppender(appender);
this.appender = appender;
}
示例11: createAppender
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy; //导入依赖的package包/类
/**
* Creates an {@link OutputStreamAppender} for the required filter, pattern and logger output.
*
* @param context Logger context to associate the appender with.
* @param filter Event log filter.
* @param logOutput Logger output information for the destination to write logger events to.
* @param patternProperty Logger context property that defines the pattern for formatting logger event output.
* @param name The name of the appender.
* @return An {@link OutputStreamAppender} for the required parameters.
*/
private static OutputStreamAppender<ILoggingEvent> createAppender(LoggerContext context, Filter<ILoggingEvent> filter, LoggerOutput logOutput, String patternProperty, String name) {
final PatternLayoutEncoder patternLayoutEncoder = createPatternLayoutEncoder(context, patternProperty);
final OutputStreamAppender<ILoggingEvent> appender;
if (logOutput.isConsole()) {
appender = new OutputStreamAppender<>();
appender.setContext(context);
appender.setEncoder(patternLayoutEncoder);
appender.setOutputStream(logOutput.getPrintStream());
appender.setName(name);
appender.addFilter(filter);
appender.start();
} else {
RollingFileAppender<ILoggingEvent> rAppender = new RollingFileAppender<>();
rAppender.setContext(context);
rAppender.setEncoder(patternLayoutEncoder);
rAppender.setFile(logOutput.getOutputName()+"."+logOutput.getOutputType());
rAppender.setName(name);
rAppender.addFilter(filter);
final FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
rollingPolicy.setContext(context);
rollingPolicy.setParent(rAppender);
rollingPolicy.setFileNamePattern(logOutput.getOutputName()+"%i"+"."+logOutput.getOutputType());
rollingPolicy.setMinIndex(1);
rollingPolicy.setMaxIndex(logOutput.getFileCount());
rollingPolicy.start();
final SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<>();
triggeringPolicy.setContext(context);
triggeringPolicy.setMaxFileSize(logOutput.getFileLimit());
triggeringPolicy.start();
rAppender.setRollingPolicy(rollingPolicy);
rAppender.setTriggeringPolicy(triggeringPolicy);
rAppender.start();
appender = rAppender;
}
return appender;
}
示例12: setup
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy; //导入依赖的package包/类
public static void setup(String fileName) {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
RollingFileAppender appender = new RollingFileAppender();
appender.setContext(loggerContext);
appender.setFile(fileName + ".log");
FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
rollingPolicy.setContext(loggerContext);
rollingPolicy.setParent(appender);
rollingPolicy.setFileNamePattern(fileName + "_%i.log");
rollingPolicy.setMinIndex(1);
rollingPolicy.setMaxIndex(10);
rollingPolicy.start();
SizeBasedTriggeringPolicy triggeringPolicy = new SizeBasedTriggeringPolicy();
triggeringPolicy.setMaxFileSize(FileSize.valueOf("10MB"));
triggeringPolicy.start();
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern("%d{MMM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{15}: %msg %xEx%n");
encoder.start();
//noinspection unchecked
appender.setEncoder(encoder);
appender.setRollingPolicy(rollingPolicy);
//noinspection unchecked
appender.setTriggeringPolicy(triggeringPolicy);
appender.start();
logbackLogger = loggerContext.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
//noinspection unchecked
logbackLogger.addAppender(appender);
logbackLogger.setLevel(Level.INFO);
// log errors in separate file
// not working as expected still.... damn logback...
/* FileAppender errorAppender = new FileAppender();
errorAppender.setEncoder(encoder);
errorAppender.setName("Error");
errorAppender.setContext(loggerContext);
errorAppender.setFile(fileName + "_error.log");
LevelFilter levelFilter = new LevelFilter();
levelFilter.setLevel(Level.ERROR);
levelFilter.setOnMatch(FilterReply.ACCEPT);
levelFilter.setOnMismatch(FilterReply.DENY);
levelFilter.start();
errorAppender.addFilter(levelFilter);
errorAppender.start();
logbackLogger.addAppender(errorAppender);*/
}
示例13: configureRotatingLogging
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy; //导入依赖的package包/类
public static void configureRotatingLogging( String baseLoggingDir, String loglevel, String appName ) {
if ( !baseLoggingDir.endsWith( "/" ) ) {
baseLoggingDir = baseLoggingDir + "/";
}
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
lc.reset();
Logger rootLogger = getRootLogger( loglevel );
PatternLayoutEncoder ple = getPatternLayoutEncoder( lc );
String baseAppLoggingDir = baseLoggingDir;
// set a system property so other loggers write the correct place
System.setProperty( "krux-base-app-log-dir", baseAppLoggingDir );
try {
// Define file appender with layout and output log file name
RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>();
fileAppender.setContext( lc );
fileAppender.setName( "krux-file-appender" );
fileAppender.setFile( baseAppLoggingDir + appName + ".log" );
fileAppender.setAppend( true );
fileAppender.setEncoder( ple );
FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
rollingPolicy.setContext( lc );
rollingPolicy.setMinIndex( 1 );
rollingPolicy.setMaxIndex( 9 );
rollingPolicy.setFileNamePattern( baseAppLoggingDir + appName + ".%i.log.gz" );
rollingPolicy.setParent( fileAppender );
rollingPolicy.start();
SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<ILoggingEvent>( "100MB" );
triggeringPolicy.setContext( lc );
triggeringPolicy.start();
fileAppender.setRollingPolicy( rollingPolicy );
fileAppender.setTriggeringPolicy( triggeringPolicy );
fileAppender.start();
// Wrap the console appenders in an async appenders
AsyncAppender asyncOut = new AsyncAppender();
asyncOut.setContext( lc );
asyncOut.setDiscardingThreshold( 0 );
asyncOut.setQueueSize( 500 );
asyncOut.addAppender( fileAppender );
asyncOut.setName( "stdlib-async-out" );
asyncOut.start();
// Add the appender to root logger
rootLogger.addAppender( asyncOut );
// wrap stdout & stderr in log4j appenders
StdOutErrLog.tieSystemOutAndErrToLog();
} catch ( Exception e ) {
System.out.println( "Failed to add appender !!" );
e.printStackTrace();
}
}
示例14: init
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy; //导入依赖的package包/类
@Override
public void init(LogAppenderDto appenderDto, FileConfig config, Path filePath) {
LOG.info(
"[{}][{}] Initializing with rollingFileNamePatern: {}, rollingMaxHistory: {},"
+ " triggerMaxFileSize: {}, encoderPattern: {}",
appenderDto.getTenantId(), appenderDto.getApplicationId(),
config.getRollingFileNamePatern(), config.getRollingMaxHistory(),
config.getTriggerMaxFileSize(), config.getEncoderPattern());
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
rfAppender = new RollingFileAppender();
rfAppender.setContext(loggerContext);
rfAppender.setFile(filePath.toAbsolutePath().toString());
TimeBasedRollingPolicy rollingPolicy = new TimeBasedRollingPolicy<>();
rollingPolicy.setFileNamePattern(config.getRollingFileNamePatern());
rollingPolicy.setMaxHistory(config.getRollingMaxHistory());
rollingPolicy.setContext(loggerContext);
rollingPolicy.setParent(rfAppender);
rollingPolicy.start();
SizeBasedTriggeringPolicy triggeringPolicy =
new ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy();
triggeringPolicy.setMaxFileSize(config.getTriggerMaxFileSize());
triggeringPolicy.start();
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern(config.getEncoderPattern());
encoder.start();
rfAppender.setEncoder(encoder);
rfAppender.setRollingPolicy(rollingPolicy);
rfAppender.setTriggeringPolicy(triggeringPolicy);
rfAppender.start();
logger = loggerContext.getLogger(appenderDto.getTenantId() + "."
+ appenderDto.getApplicationToken());
logger.setLevel(Level.ALL);
logger.addAppender(rfAppender);
LOG.debug("[{}][{}] Initialized with context {}", appenderDto.getTenantId(),
appenderDto.getApplicationId(), loggerContext);
}
示例15: startFileLogging
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy; //导入依赖的package包/类
/**
*
* @param logFilePath
*/
public static void startFileLogging(String logFilePath) {
Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
LoggerContext loggerContext = rootLogger.getLoggerContext();
RollingFileAppender<ILoggingEvent> rfAppender = new RollingFileAppender<>();
rfAppender.setContext(loggerContext);
rfAppender.setFile(logFilePath);
FixedWindowRollingPolicy fwRollingPolicy = new FixedWindowRollingPolicy();
fwRollingPolicy.setContext(loggerContext);
fwRollingPolicy.setFileNamePattern(logFilePath + "-%i.log.zip");
fwRollingPolicy.setParent(rfAppender);
fwRollingPolicy.start();
SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<>();
triggeringPolicy.setMaxFileSize(FileSize.valueOf("5 mb"));
triggeringPolicy.start();
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern("%d{HH:mm:ss.SSS} [%thread] %highlight(%-5level) %logger{36} - %msg%n");
encoder.start();
rfAppender.setEncoder(encoder);
rfAppender.setName("ROLLINGFILE");
rfAppender.setRollingPolicy(fwRollingPolicy);
rfAppender.setTriggeringPolicy(triggeringPolicy);
rfAppender.start();
AsyncAppender asyncAppender = new AsyncAppender();
asyncAppender.setContext(loggerContext);
asyncAppender.setName("ASYNC");
asyncAppender.setQueueSize(ASYNC_QUEUE_SIZE);
asyncAppender.setMaxFlushTime(ASYNC_MAX_FLUSH_TIME);
asyncAppender.addAppender(rfAppender);
asyncAppender.start();
rootLogger.addAppender(asyncAppender);
}