Java Code Examples for ch.qos.logback.classic.spi.LoggingEvent#setLoggerName()

The following examples show how to use ch.qos.logback.classic.spi.LoggingEvent#setLoggerName() . 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: LoggerThresholdFilterTest.java    From logging-java with Apache License 2.0 6 votes vote down vote up
@Test
public void verifyFilteringAllAtWarn() {
    final LoggerThresholdFilter filter = new LoggerThresholdFilter();
    filter.setLevel(WARN);
    filter.start();

    for(Level level : allLevels) {
        final LoggingEvent evt = new LoggingEvent();
        evt.setLevel(level);
        for(String logger : variousLoggers) {
            evt.setLoggerName(logger);
            if(level.isGreaterOrEqual(WARN))
                assertEquals(FilterReply.NEUTRAL, filter.decide(evt));
            else
                assertEquals(FilterReply.DENY, filter.decide(evt));
        }
    }
}
 
Example 2
Source File: CloudWatchAppender.java    From cloudwatch-logback-appender with ISC License 5 votes vote down vote up
private LoggingEvent makeEvent(Level level, String message, Throwable th) {
	LoggingEvent event = new LoggingEvent();
	event.setLoggerName(CloudWatchAppender.class.getName());
	event.setLevel(level);
	event.setMessage(message);
	event.setTimeStamp(System.currentTimeMillis());
	if (th != null) {
		event.setThrowableProxy(new ThrowableProxy(th));
	}
	return event;
}
 
Example 3
Source File: YarnTwillLogHandler.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void onLog(LogEntry logEntry) {

  final LoggingEvent loggingEvent = new LoggingEvent();
  loggingEvent.setTimeStamp(logEntry.getTimestamp());
  loggingEvent.setLoggerName(logEntry.getLoggerName());
  loggingEvent.setLevel(Level.valueOf(logEntry.getLogLevel().name()));
  loggingEvent.setThreadName(logEntry.getThreadName());
  Object [] formatObjects = new Object[] {logEntry.getHost(),
    getSimpleClassName(logEntry.getSourceClassName()),
    logEntry.getSourceMethodName(),
    logEntry.getFileName(),
    logEntry.getLineNumber(),logEntry.getMessage()};
  loggingEvent.setMessage(MESSAGE_FORMAT.get().format(formatObjects));

  // Prints the throwable and stack trace.
  LogThrowable logThrowable = logEntry.getThrowable();
  if (logThrowable != null) {
    loggingEvent.setThrowableProxy(new ThrowableProxy(setThrowable(logThrowable)));
  }

  if (logger instanceof Logger) {
    ((Logger) logger).callAppenders(loggingEvent);
  } else {
    logger.info("Logger is not instance of ch.qos.logback.classic.Logger. Logger event is: {}", loggingEvent);
  }
}
 
Example 4
Source File: LoggerFilterTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
private void assertNeutral(String logger, String loggerName, Boolean exclude) {
  LoggerFilter filter = create(loggerName, exclude);
  LoggingEvent event = new LoggingEvent();

  event.setLevel(Level.INFO);
  event.setLoggerName(logger);

  assertEquals(FilterReply.NEUTRAL, filter.decide(event));
}
 
Example 5
Source File: LoggerFilterTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
private void assertDeny(String logger, String loggerName, Boolean exclude) {
  LoggerFilter filter = create(loggerName, exclude);
  LoggingEvent event = new LoggingEvent();

  event.setLevel(Level.INFO);
  event.setLoggerName(logger);

  assertEquals(FilterReply.DENY, filter.decide(event));
}
 
Example 6
Source File: LoggerFilterTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
private void assertNeutral(String logger, List<String> loggerName, Boolean exclude) {
  LoggerFilter filter = create(loggerName, exclude);
  LoggingEvent event = new LoggingEvent();

  event.setLevel(Level.INFO);
  event.setLoggerName(logger);

  assertEquals(FilterReply.NEUTRAL, filter.decide(event));
}
 
Example 7
Source File: LoggerFilterTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
private void assertDeny(String logger, List<String> loggerName, Boolean exclude) {
  LoggerFilter filter = create(loggerName, exclude);
  LoggingEvent event = new LoggingEvent();

  event.setLevel(Level.INFO);
  event.setLoggerName(logger);

  assertEquals(FilterReply.DENY, filter.decide(event));
}
 
Example 8
Source File: LoggerThresholdFilterTest.java    From logging-java with Apache License 2.0 5 votes vote down vote up
@Test
public void verifyNoFilteringPassesAllEvents() {
    final LoggerThresholdFilter filter = new LoggerThresholdFilter();
    filter.start();

    for(Level level : allLevels) {
        final LoggingEvent evt = new LoggingEvent();
        evt.setLevel(level);
        for(String logger : variousLoggers) {
            evt.setLoggerName(logger);
            assertEquals(FilterReply.NEUTRAL, filter.decide(evt));
        }
    }
}
 
Example 9
Source File: CloudWatchAppenderTest.java    From cloudwatch-logback-appender with ISC License 4 votes vote down vote up
@Test(timeout = 5000)
public void testBasic() throws InterruptedException {
	CloudWatchAppender appender = new CloudWatchAppender();
	AWSLogsClient awsLogClient = createMock(AWSLogsClient.class);
	appender.setAwsLogsClient(awsLogClient);

	appender.setMaxBatchSize(1);
	appender.setRegion("region");
	final String logGroup = "pfqoejpfqe";
	appender.setLogGroup(logGroup);
	final String logStream = "pffqjfqjpoqoejpfqe";
	appender.setLogStream(logStream);
	PatternLayout layout = new PatternLayout();
	layout.setContext(new LoggerContext());
	layout.setPattern("[%thread] %level %logger{20} - %msg%n%xThrowable");
	layout.start();
	appender.setLayout(layout);

	LoggingEvent event = new LoggingEvent();
	event.setTimeStamp(System.currentTimeMillis());
	String loggerName = "name";
	event.setLoggerName(loggerName);
	Level level = Level.DEBUG;
	event.setLevel(level);
	String message = "fjpewjfpewjfpewjfepowf";
	event.setMessage(message);

	String threadName = Thread.currentThread().getName();
	final String fullMessage = "[" + threadName + "] " + level + " " + loggerName + " - " + message + "\n";

	final PutLogEventsResult result = new PutLogEventsResult();
	String sequence = "ewopjfewfj";
	result.setNextSequenceToken(sequence);
	expect(awsLogClient.putLogEvents(isA(PutLogEventsRequest.class))).andAnswer(new IAnswer<PutLogEventsResult>() {
		@Override
		public PutLogEventsResult answer() {
			PutLogEventsRequest request = (PutLogEventsRequest) getCurrentArguments()[0];
			assertEquals(logGroup, request.getLogGroupName());
			assertEquals(logStream, request.getLogStreamName());
			List<InputLogEvent> events = request.getLogEvents();
			assertEquals(1, events.size());
			assertEquals(fullMessage, events.get(0).getMessage());
			return result;
		}
	}).times(2);
	awsLogClient.shutdown();

	// =====================================

	replay(awsLogClient);
	appender.start();
	// for coverage
	appender.start();
	appender.append(event);
	Thread.sleep(10);
	appender.append(event);
	while (appender.getEventsWrittenCount() < 2) {
		Thread.sleep(10);
	}
	appender.stop();
	verify(awsLogClient);
}
 
Example 10
Source File: CloudWatchAppenderTest.java    From cloudwatch-logback-appender with ISC License 4 votes vote down vote up
@Test(timeout = 5000)
public void testInstanceName() throws InterruptedException {
	CloudWatchAppender appender = new CloudWatchAppender();
	AWSLogsClient awsLogClient = createMock(AWSLogsClient.class);
	appender.setAwsLogsClient(awsLogClient);

	appender.setMaxBatchSize(1);
	appender.setRegion("region");
	final String logGroup = "pfqoejpfqe";
	appender.setLogGroup(logGroup);
	String prefix = "logstream-";
	appender.setLogStream(prefix + "%instanceName");
	final String expectedLogStream = prefix + "unknown";
	PatternLayout layout = new PatternLayout();
	layout.setPattern("%msg");
	layout.start();
	appender.setLayout(layout);

	LoggingEvent event = new LoggingEvent();
	event.setTimeStamp(System.currentTimeMillis());
	event.setLoggerName("name");
	event.setLevel(Level.DEBUG);
	event.setMessage("message");

	final PutLogEventsResult result = new PutLogEventsResult();
	result.setNextSequenceToken("ewopjfewfj");
	expect(awsLogClient.putLogEvents(isA(PutLogEventsRequest.class))).andAnswer(new IAnswer<PutLogEventsResult>() {
		@Override
		public PutLogEventsResult answer() {
			PutLogEventsRequest request = (PutLogEventsRequest) getCurrentArguments()[0];
			assertEquals(logGroup, request.getLogGroupName());
			assertEquals(expectedLogStream, request.getLogStreamName());
			return result;
		}
	});
	awsLogClient.shutdown();

	// =====================================

	replay(awsLogClient);
	appender.start();
	appender.append(event);
	while (appender.getEventsWrittenCount() < 1) {
		Thread.sleep(10);
	}
	appender.stop();
	verify(awsLogClient);
}
 
Example 11
Source File: GelfAppender.java    From logback-gelf-appender with Apache License 2.0 4 votes vote down vote up
@Override
protected void append(ILoggingEvent event) {

    if (event == null) {
        return;
    }

    // create a copy of the logging event to avoid passing exception stacktraces to GELF's short_message field
    LoggingEvent copy = new LoggingEvent();
    copy.setMessage(event.getMessage());
    copy.setLevel(event.getLevel());
    copy.setArgumentArray(event.getArgumentArray());
    copy.setLoggerName(event.getLoggerName());
    copy.setThreadName(event.getThreadName());
    copy.setTimeStamp(event.getTimeStamp());
    copy.setMDCPropertyMap(event.getMDCPropertyMap());

    final GelfMessageBuilder builder = new GelfMessageBuilder(this.layout.doLayout(copy), hostName).timestamp(
                event.getTimeStamp() / 1000d)
            .level(GelfMessageLevel.fromNumericLevel(toGelfNumericValue(event.getLevel())))
            .additionalField("loggerName", event.getLoggerName())
            .additionalField("threadName", event.getThreadName());

    final Marker marker = event.getMarker();

    if (marker != null) {
        builder.additionalField("marker", marker.getName());
    }

    if (includeMDC) {
        for (Map.Entry<String, String> entry : event.getMDCPropertyMap().entrySet()) {
            builder.additionalField(entry.getKey(), entry.getValue());
        }
    }

    final StackTraceElement[] callerData = event.getCallerData();

    if (includeSource && event.hasCallerData() && callerData.length > 0) {
        StackTraceElement source = callerData[0];

        builder.additionalField("sourceFileName", source.getFileName());
        builder.additionalField("sourceMethodName", source.getMethodName());
        builder.additionalField("sourceClassName", source.getClassName());
        builder.additionalField("sourceLineNumber", source.getLineNumber());
    }

    final IThrowableProxy thrown = event.getThrowableProxy();

    if (includeStackTrace && thrown != null) {
        String convertedThrowable = throwableConverter.convert(event);

        builder.additionalField("exceptionClass", thrown.getClassName());
        builder.additionalField("exceptionMessage", thrown.getMessage());
        builder.additionalField("exceptionStackTrace", convertedThrowable);

        builder.fullMessage(event.getFormattedMessage() + "\n\n" + convertedThrowable);
    } else {
        builder.fullMessage(event.getFormattedMessage());
    }

    if (includeLevelName) {
        builder.additionalField("levelName", event.getLevel().levelStr);
    }

    if (!additionalFields.isEmpty()) {
        builder.additionalFields(additionalFields);
    }

    if(!client.trySend(builder.build())) {
        addError("Failed to write log event to the GELF server using trySend");
    }
}
 
Example 12
Source File: LoggerThresholdFilterTest.java    From logging-java with Apache License 2.0 4 votes vote down vote up
@Test
public void filterSpotifyAtInfoOthersAtWarn() {
    List<Filter<ILoggingEvent>> filters = new ArrayList<Filter<ILoggingEvent>>();

    LoggerThresholdFilter filter = new LoggerThresholdFilter();
    filter.setLogger(spotifyLog);
    filter.setLevel(INFO);
    filter.start();
    filters.add(filter);

    filter = new LoggerThresholdFilter();
    filter.setExceptLogger(spotifyLog);
    filter.setLevel(WARN);
    filter.start();
    filters.add(filter);

    for(Level level : allLevels) {
        final LoggingEvent evt = new LoggingEvent();
        evt.setLevel(level);

        for(String logger : variousLoggers) {
            evt.setLoggerName(logger);

            FilterReply expected;
            FilterReply actual = FilterReply.NEUTRAL;

            if(spotifyLoggers.contains(logger)) {
                if(level.isGreaterOrEqual(INFO))
                    expected = FilterReply.NEUTRAL;
                else
                    expected = FilterReply.DENY;
            } else {
                if(level.isGreaterOrEqual(WARN))
                    expected = FilterReply.NEUTRAL;
                else
                    expected = FilterReply.DENY;
            }

            for(Filter<ILoggingEvent> logFilter : filters) {
                FilterReply nextReply = logFilter.decide(evt);
                actual = andFilterReplies(actual, nextReply);
            }

            assertEquals(String.format("Logger: %s, Level: %s", logger, level.toString()), expected, actual);
        }
    }
}