The purpose of a custom Spring Boot starter is to provide a default Logback XML configuration, e.g.:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml"/>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
</configuration>
It also provides a default logging.pattern.level defined in the starter's properties loaded by EnvironmentPostProcessor. For instance, let's assume it appends spring.application.name to the logging level:
logging.pattern.level=%5p [${spring.application.name}]
The problems that occur:
Property spring.application.name is not recognized:
2023-02-07 09:44:00.704 INFO [spring.application.name_IS_UNDEFINED] 70495 ---...
The default logging.pattern.level cannot be overridden in the application properties. For example, if the application.properties contains the property:
logging.pattern.level=%5p stackoverflow
#Bean
public String test(#Value("${logging.pattern.level}") String loggingPattern) {
Logger logger = Logger.getLogger("LogDemo");
logger.info(loggingPattern);
return loggingPattern;
}
2023-02-07 09:57:50.124 INFO [spring.application.name_IS_UNDEFINED] 70880 --- [ main] LogDemo : %5p stackoverflow
If logback.xml is not defined in the custom starter the spring.application.name is recognized successfully. If logging.pattern.level is moved from the starter's properties to the application.properties it also works.
It seems like logging is being configured right after loading the starter's properties but before loading the application properties, in case the starter's properties contain the logging.pattern.level.
Is there a way to define a default logging configuration without the problems listed above?
I managed to define a default logging configuration in a custom starter using Spring Boot's extensions to Logback, without additional properties and EnvironmentPostProcessor.
Because the standard logback.xml configuration file is loaded too
early, you cannot use extensions in it. You need to either use
logback-spring.xml or define a logging.config property.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProperty scope="context" name="SPRING_APPLICATION_NAME" source="spring.application.name"/>
<springProperty scope="context" name="SPRING_LOG_LEVEL_PATTERN" source="logging.pattern.level"/>
<property name="DEFAULT_LOG_LEVEL_PATTERN"
value="%5p [${SPRING_APPLICATION_NAME}]"/>
<property name="LOG_LEVEL_PATTERN"
value="${SPRING_LOG_LEVEL_PATTERN:-${DEFAULT_LOG_LEVEL_PATTERN}}"/>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml"/>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
</configuration>
Related
The following Open Telemetry starters have been added to the Spring Boot project (v2.7.2) to instrument the application:
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-spring-boot-starter</artifactId>
<version>1.22.1-alpha</version>
</dependency>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-jaeger-spring-boot-starter</artifactId>
<version>1.22.1-alpha</version>
</dependency>
Traces and spans are successfully exported to a Jaeger collector. The problem is those traces and spans cannot be correlated with log statements because logs don't contain the current trace_id and span_id.
By following the documentation I added the logging.pattern.level property to application.properties but it seems like information about the current span is not injected into the logging event's MDC copy.
logging.pattern.level = trace_id=%mdc{trace_id} span_id=%mdc{span_id} trace_flags=%mdc{trace_flags} %5p
For example:
log.info(
"traceId: {}, spanId: {}",
Span.current().getSpanContext().getTraceId(),
Span.current().getSpanContext().getSpanId());
2023-01-25 12:21:36.774 trace_id= span_id= trace_flags= INFO 34272 --- [nio-8080-exec-2] h.c.DemoController : traceId: 1bccb6a4fea8345026ca87a202f0091f, spanId: c59a5d44ee40cd2c
Have I missed anything?
opentelemetry-spring-boot-starter is missing this feature right now (version 1.22.1-alpha), but it can be added very easily.
Firstly, MDC Instrumentation for Logback dependency should be added to the project.
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-logback-mdc-1.0</artifactId>
<version>OPENTELEMETRY_VERSION</version>
</dependency>
Then add a custom logback.xml configuration to the classpath, which will automatically inject context information from the span context to logging events.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- Default logback configuration -->
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml"/>
<!-- -->
<appender name="OTEL" class="io.opentelemetry.instrumentation.logback.v1_0.OpenTelemetryAppender">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</appender>
<root level="INFO">
<appender-ref ref="OTEL"/>
</root>
</configuration>
Finally, add the property to application.properties (as mentioned in the question):
logging.pattern.level = trace_id=%mdc{trace_id} span_id=%mdc{span_id} trace_flags=%mdc{trace_flags} %5p
I'm using spring-boot-starter-web like framework. These, use Logback (without implement other libraries) to manage logs with SLF4J like a facade. I need to use it with rsyslog but the official doc refers only to Syslog.
I tried to use the Syslog implementation since Syslog inherit for rsyslog but not found ( i attach my logback-spring.xml below ).
Any idea?
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
<property resource="application.properties" />
<!-- Syslog , make sure that syslog are enabled in the OS-->
<appender name="RSYSLOG" class="ch.qos.logback.classic.net.SyslogAppender">
<syslogHost>127.0.0.1</syslogHost>
<facility>LOCAL0</facility>
<port>514</port>
<throwableExcluded>true</throwableExcluded>
<suffixPattern>%package.yes.rest %m thread:%t priority:%p category:%c
exception:%exception:%msg</suffixPattern>
</appender>
<logger name="package.yes.rest" level="info" additivity="false">
<appender-ref ref="RSYSLOG"/>
</logger>
</configuration>
Bonus clip: I see the choose of change Logback to Log4j2 too, but it's more stable use the inherit Logback.
As per this guidelines, one has to specify application name in bootstrap.properties if using custom logback.xml. I was wondering if there's any way where I can hardcode the application name in logback.xml instead of creating the bootstrap.properties file with that property ?
I have bootstrap.properties with property spring.application.name and slueth recognizes that and things are fine. But I was wondering if there's any way where I can specify any logback property and sleuth will pick the application name up!?
Please note I am using my own custom logging format. Following is how my logback.xml file looks like.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="CONSOLE_LOG_PATTERN" value="%date{ISO8601}
${LOG_LEVEL_PATTERN:-%5p} ${PID:- } [%15.15t] %-40.40logger{39} :
%m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
</configuration>
The <springProperty> tag lets you expose properties from the Spring Environment for use within Logback.
i.e.
<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host" defaultValue="localhost"/>
I tried following settings in application.properties:
logging.file=foo/bar.log
logging.file.max-history=2
logging.file.max-size=1KB
Still, its not limiting the number of archive logs to 2.
As per application properties documentation reference, only supported when you setup logback.
logging.file.max-history=0 # Maximum of archive log files to keep. Only supported with the default logback setup.
So to add support of logback please see section 79.1 Configure Logback for Logging & 79.1.1 Configure Logback for File-only Output of Spring Boot Logging Guide
If you want to disable console logging and write output only to a
file, you need a custom logback-spring.xml that imports
file-appender.xml but not console-appender.xml, as shown in the
following example:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>
I am trying to configure Log4j 2 in Spring XML configuration file for the first time (but unsuccessfully). I need to create two appenders - one for logging into console (>=DEBUG) and another for logging into database via JDBCAppender (>= INFO).
There is a problem because I don't know how to set another log level logger that differs from root logger.
Thank you for sharing some XML configuration sample. Thanks in advance!
You can set the level on the appender ref.
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
<appenders>
<appender name="A">
...
</appender>
<appender name="B">
...
</appender>
</appenders>
<loggers>
<root level="trace">
<appender-ref ref="A" level="info"/>
<appender-ref ref="B" level="debug"/>
</root>
</loggers>
</configuration>