ch.qos.logback.core.encoder.LayoutWrappingEncoder Java Examples

The following examples show how to use ch.qos.logback.core.encoder.LayoutWrappingEncoder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LogbackConfiguration.java    From milkman with MIT License 5 votes vote down vote up
private void setupConsoleAppender(LoggerContext loggerContext) {
	ConsoleAppender<ILoggingEvent> ca = new ConsoleAppender<ILoggingEvent>();
	ca.setContext(loggerContext);
	ca.setName("STDOUT");
	
	var filter = new ThresholdFilter();
	filter.setLevel("INFO");
	filter.start();
	ca.addFilter(filter);
	
	LayoutWrappingEncoder<ILoggingEvent> encoder = new LayoutWrappingEncoder<ILoggingEvent>();
	encoder.setContext(loggerContext);

	
	// same as
	// PatternLayout layout = new PatternLayout();
	// layout.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
	TTLLLayout layout = new TTLLLayout();

	layout.setContext(loggerContext);
	layout.start();
	encoder.setLayout(layout);

	
	
	ca.setEncoder(encoder);
	ca.start();

	Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
	rootLogger.addAppender(ca);
}
 
Example #2
Source File: LogBackConfiguration.java    From summerframework with Apache License 2.0 5 votes vote down vote up
private void setLayout(LoggerContext loggerContext,OutputStreamAppender outputStreamAppender){
    Encoder<?> encoder = outputStreamAppender.getEncoder();
    if (encoder instanceof LayoutWrappingEncoder) {
        TraceIdPatternLogbackLayout traceIdLayOut = new TraceIdPatternLogbackLayout();
        traceIdLayOut.setContext(loggerContext);
        traceIdLayOut.setPattern(getLogBackPattern());
        traceIdLayOut.start();
        Field field = ReflectionUtils.findField(encoder.getClass(), "layout");
        field.setAccessible(true);
        ReflectionUtils.setField(field, encoder, traceIdLayOut);
    }
}
 
Example #3
Source File: LogBackHookProxy.java    From uavstack with Apache License 2.0 5 votes vote down vote up
private void getAppenderInfo(FileAppender<ILoggingEvent> fileAppender, LinkedList<LogProfileInfo> list, String appid) {
    
    LogProfileInfo logProfileInfo = new LogProfileInfo();
    logProfileInfo.setLogType(LogProfileInfo.LogType.Log4j);
    logProfileInfo.setFilePath(fileAppender.getFile());
    if (appid != null) {
        logProfileInfo.setAppId(appid);
    }
    
    Map<String, String> attributes = new HashMap<String, String>();
    attributes.put(LogProfileInfo.ENGINE, "logback");

    @SuppressWarnings("rawtypes")
    LayoutWrappingEncoder encoder = (LayoutWrappingEncoder) fileAppender.getEncoder();
    Layout<?> layout = encoder.getLayout();
    if (null != layout) {
        if (layout instanceof PatternLayout) {
            PatternLayout patternLayout = (PatternLayout) encoder.getLayout();
            attributes.put(LogProfileInfo.PATTERN, patternLayout.getPattern());
        }
        else if (layout instanceof HTMLLayout) {
            attributes.put(LogProfileInfo.PATTERN, "HTMLLayout");
        }
        else if (layout instanceof XMLLayout) {
            attributes.put(LogProfileInfo.PATTERN, "XMLLayout");
        }
        else if (layout instanceof TTLLLayout) {
            attributes.put(LogProfileInfo.PATTERN, "TTCCLayout");
        }
    }

    logProfileInfo.setAttributes(attributes);
    list.add(logProfileInfo);      
}
 
Example #4
Source File: LogbackConfiguration.java    From milkman with MIT License 4 votes vote down vote up
private void setupFileAppender(LoggerContext loggerContext) {
	RollingFileAppender<ILoggingEvent> fa = new RollingFileAppender<ILoggingEvent>();
	fa.setFile(PlatformUtil.getWritableLocationForFile("errors.log"));
	
	SizeBasedTriggeringPolicy<ILoggingEvent> sizePolicy = new SizeBasedTriggeringPolicy<ILoggingEvent>();
	sizePolicy.setContext(loggerContext);
	sizePolicy.setMaxFileSize(FileSize.valueOf("10mb"));
	sizePolicy.start();
	fa.setTriggeringPolicy(sizePolicy);
	var policy = new FixedWindowRollingPolicy();
	policy.setMaxIndex(3);
	policy.setMinIndex(1);
	policy.setFileNamePattern("errors.%i.log.zip");
	policy.setParent(fa);
	policy.setContext(loggerContext);
	policy.start();
	fa.setRollingPolicy(policy);
	
	
	fa.setContext(loggerContext);
	fa.setName("FILE");
	
	var filter = new ThresholdFilter();
	filter.setLevel("ERROR");
	filter.start();
	fa.addFilter(filter);

	LayoutWrappingEncoder<ILoggingEvent> encoder = new LayoutWrappingEncoder<ILoggingEvent>();
	encoder.setContext(loggerContext);

	// same as
	// PatternLayout layout = new PatternLayout();
	// layout.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
	PatternLayout layout = new PatternLayout();
	layout.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n");

	layout.setContext(loggerContext);
	layout.start();
	encoder.setLayout(layout);
	fa.setEncoder(encoder);

	
	fa.start();

	Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
	rootLogger.addAppender(fa);
}