11package rocks .inspectit .ocelot .core .selfmonitoring ;
22
33import ch .qos .logback .classic .Level ;
4+ import ch .qos .logback .classic .Logger ;
45import ch .qos .logback .classic .spi .ILoggingEvent ;
6+ import ch .qos .logback .classic .spi .LoggingEvent ;
57import lombok .extern .slf4j .Slf4j ;
8+ import org .slf4j .LoggerFactory ;
69import org .springframework .stereotype .Component ;
710import rocks .inspectit .ocelot .config .model .InspectitConfig ;
811import rocks .inspectit .ocelot .config .model .selfmonitoring .LogPreloadingSettings ;
2326@ Slf4j
2427public class LogPreloader extends DynamicallyActivatableService implements InternalProcessingAppender .LogEventConsumer {
2528
29+ private static final String LOG_INVALIDATION_EVENT = "{}! Some previous log messages may now be outdated." ;
30+
31+ private final Logger invalidationLogger = (Logger ) LoggerFactory .getLogger ("### LOG-INVALIDATING EVENT ###" );
32+
2633 private ILoggingEvent [] buffer ;
2734
2835 private final AtomicInteger currentIndex = new AtomicInteger (0 );
@@ -41,7 +48,36 @@ public LogPreloader() {
4148 */
4249 @ Override
4350 public void onLoggingEvent (ILoggingEvent event , Class <?> invalidator ) {
44- if (buffer != null && event .getLevel ().isGreaterOrEqual (minimumPreloadingLevel )) {
51+ if (event .getLevel ().isGreaterOrEqual (minimumPreloadingLevel )) {
52+ recordLoggingEvent (event );
53+ }
54+ }
55+
56+ /**
57+ * Appends the invalidation event as an artificial log message into the buffer.
58+ *
59+ * @param invalidator The invalidator
60+ */
61+ @ Override
62+ public void onInvalidationEvent (Object invalidator ) {
63+ if (invalidator != null ) {
64+ String invalidationString = invalidator .getClass ().getSimpleName ();
65+ if (invalidationString .endsWith ("Event" )) {
66+ // pretty-format event, e.g., transform 'SomethingHappenedEvent' into 'Something happened'
67+ String invalidationLowercase = invalidationString .substring (0 , invalidationString .length () - "Event" .length ())
68+ .replaceAll ("([a-z])([A-Z]+)" , "$1 $2" )
69+ .toLowerCase ();
70+ invalidationString = invalidationLowercase .substring (0 , 1 )
71+ .toUpperCase () + invalidationLowercase .substring (1 );
72+ }
73+
74+ ILoggingEvent logEvent = new LoggingEvent (getClass ().getName (), invalidationLogger , Level .INFO , LOG_INVALIDATION_EVENT , null , new String []{invalidationString });
75+ recordLoggingEvent (logEvent );
76+ }
77+ }
78+
79+ private void recordLoggingEvent (ILoggingEvent event ) {
80+ if (buffer != null ) {
4581 int index = currentIndex .getAndIncrement () % buffer .length ;
4682 try {
4783 buffer [index ] = event ;
0 commit comments