2121import static java .util .Collections .emptyList ;
2222
2323import io .opentelemetry .exporter .logging .LoggingSpanExporter ;
24+ import io .opentelemetry .exporter .logging .otlp .OtlpJsonLoggingSpanExporter ;
2425import io .opentelemetry .sdk .autoconfigure .spi .ConfigProperties ;
2526import io .opentelemetry .sdk .common .CompletableResultCode ;
2627import io .opentelemetry .sdk .trace .SdkTracerProviderBuilder ;
@@ -45,16 +46,32 @@ public class AgentLog {
4546 /** root logger is an empty string */
4647 private static final String ROOT_LOGGER_NAME = "" ;
4748
49+ private static boolean logPlainText = false ;
50+
4851 /**
4952 * debug span logging exporter that can be controlled at runtime, only used when logging span
5053 * exporter has not been explicitly configured.
5154 */
52- private static final DebugLogSpanExporter debugLogSpanExporter =
53- new DebugLogSpanExporter (LoggingSpanExporter .create ());
55+ private static DebugLogSpanExporter debugLogSpanExporter = null ;
5456
5557 private AgentLog () {}
5658
57- public static void init () {
59+ /**
60+ * Initializes agent logging
61+ *
62+ * @param usePlainTextLog {@literal true} to use plain text logging, `{@literal false} to use JSON
63+ * @param initialLevel initial log level to configure
64+ */
65+ public static void init (boolean usePlainTextLog , Level initialLevel ) {
66+ internalInit ();
67+ setLevel (initialLevel );
68+ logPlainText = usePlainTextLog ;
69+ debugLogSpanExporter =
70+ new DebugLogSpanExporter (
71+ logPlainText ? LoggingSpanExporter .create () : OtlpJsonLoggingSpanExporter .create ());
72+ }
73+
74+ private static void internalInit () {
5875
5976 ConfigurationBuilder <BuiltConfiguration > conf =
6077 ConfigurationBuilderFactory .newConfigurationBuilder ();
@@ -74,8 +91,11 @@ public static void addSpanLoggingIfRequired(
7491 // Replicate behavior of the upstream agent: span logging exporter is automatically added when
7592 // not already present when debugging. When logging exporter has been explicitly configured,
7693 // spans logging will be done by the explicitly configured logging exporter instance.
94+
95+ String exporterName = logPlainText ? "logging" : "otlp-logging" ;
96+
7797 boolean loggingExporterNotAlreadyConfigured =
78- !config .getList ("otel.traces.exporter" , emptyList ()).contains ("logging" );
98+ !config .getList ("otel.traces.exporter" , emptyList ()).contains (exporterName );
7999 if (loggingExporterNotAlreadyConfigured ) {
80100 providerBuilder .addSpanProcessor (SimpleSpanProcessor .create (debugLogSpanExporter ));
81101 }
@@ -114,7 +134,7 @@ public static void setLevel(String level) {
114134 *
115135 * @param level log level
116136 */
117- public static void setLevel (Level level ) {
137+ public static synchronized void setLevel (Level level ) {
118138 // Using log4j2 implementation allows to change the log level programmatically at runtime
119139 // which is not directly possible through the slf4j API and simple implementation used in
120140 // upstream distribution.
0 commit comments