@Override
  public byte[] encode(ILoggingEvent event) {
    var baos = new ByteArrayOutputStream();
    try (var generator = jsonFactory.createGenerator(baos)) {
      generator.writeStartObject();

      // https://cloud.google.com/logging/docs/structured-logging#structured_logging_special_fields
      // https://github.com/googleapis/java-logging-logback/blob/main/src/main/java/com/google/cloud/logging/logback/LoggingAppender.java

      writeTimestamp(generator, event);
      writeSeverity(generator, event);
      writeLogger(generator, event);
      writeMessage(generator, event);
      writeThread(generator, event);

      writeServiceContext(generator);
      writeTraceContext(generator);

      var mdc = event.getMDCPropertyMap();
      writeMdc(generator, mdc);

      writeKeyValue(generator, event);

      if ("ERROR".equals(event.getLevel().toString())) {
        writeError(generator, event, mdc);
      }
      writeStackTrace(generator, event);

      generator.writeEndObject();
      generator.writeRaw('\n');
      generator.flush();
    } catch (NullPointerException | IOException e) {
      return logFallbackError(event, e);
    }
    return baos.toByteArray();
  }
@Test
  void encode_simple() {

    var e = mockEvent();
    when(e.getFormattedMessage()).thenReturn("Hello World!");

    var msg = encoder.encode(e);

    assertMatchesJson(
        """
          {"logger":"com.example.MyLogger","message":"Hello World!","severity":"INFO","thread_name":"main","time":"2024-08-09T14:13:33Z"}
          """,
        msg);
  }