Events
17 Mar, 9 pm - 21 Mar, 10 am
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
The Basic, Standard, and Enterprise plans will be deprecated starting from mid-March, 2025, with a 3 year retirement period. We recommend transitioning to Azure Container Apps. For more information, see the Azure Spring Apps retirement announcement.
The Standard consumption and dedicated plan will be deprecated starting September 30, 2024, with a complete shutdown after six months. We recommend transitioning to Azure Container Apps. For more information, see Migrate Azure Spring Apps Standard consumption and dedicated plan to Azure Container Apps.
This article applies to: ✅ Java ❎ C#
This article applies to: ✅ Basic/Standard ✅ Enterprise
This article shows you how to load Logback and write logs to custom persistent storage in Azure Spring Apps.
Note
When a file in the application's classpath has one of the following names, Spring Boot will automatically load it over the default configuration for Logback:
You can set the path to where logs will be written by using the logback-spring.xml example file.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="Console"
class="ch.qos.logback.core.ConsoleAppender">
<!-- please feel free to customize the log layout -->
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
</Pattern>
</layout>
</appender>
<appender name="RollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 'LOGS' here is a value to be read from the application's environment variable -->
<file>${LOGS}/spring-boot-logger.log</file>
<!-- please feel free to customize the log layout pattern -->
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily and when the file reaches 10 MegaBytes -->
<fileNamePattern>${LOGS}/archived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<!-- LOG everything at the INFO level -->
<root level="info">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</root>
<!-- LOG "com.baeldung*" at the TRACE level -->
<logger name="com.baeldung" level="trace" additivity="false">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</logger>
</configuration>
In the preceding example, there are two placeholders named {LOGS}
in the path for writing the application's logs to. A value needs to be assigned to the environment variable LOGS
to have the log write to both the console and your persistent storage.
Use the following command to create an application in Azure Spring Apps with persistent storage enabled and the environment variable set:
az spring app create \
--resource-group <resource-group-name> \
--name <app-name> \
--service <spring-instance-name> \
--persistent-storage <path-to-json-file> \
--env LOGS=/byos/logs
Note
The value of the LOGS
environment variable can be the same as, or a subdirectory of the mountPath
.
Here's an example of the JSON file that is passed to the --persistent-storage
parameter in the create command. In this example, the same value is passed for the environment variable in the CLI command above and in the mountPath
property below:
{
"customPersistentDisks": [
{
"storageName": "<Storage-Resource-Name>",
"customPersistentDiskProperties": {
"type": "AzureFileVolume",
"shareName": "<Azure-File-Share-Name>",
"mountPath": "/byos/logs",
"readOnly": false
}
}
]
}
Use the following command to deploy your application:
az spring app deploy \
--resource-group <resource-group-name> \
--name <app-name> \
--service <spring-instance-name> \
--artifact-path <path-to-jar-file>
Use the following command to check your application's console log:
az spring app logs \
--resource-group <resource-group-name> \
--name <app-name> \
--service <spring-instance-name>
Go to the Azure Storage Account resource you bound and find the Azure file share that was attached as persistent storage. In this example, the logs will be written to the spring-boot-logger.log file at the root of your Azure file share. All of the rotated log files will be stored in the /archived folder in your Azure file share.
Optionally, use the following command to update the path or persistent storage of an existing app:
The path or persistent storage where the logs are saved can be changed at any time. The application will restart when changes are made to either environment variables or persistent storage.
az spring app update \
--resource-group <resource-group-name> \
--name <app-name> \
--service <spring-instance-name> \
--persistent-storage <path-to-new-json-file> \
--env LOGS=<new-path>
Events
17 Mar, 9 pm - 21 Mar, 10 am
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Deploy a Spring Boot app to Azure Container Apps - Training
In this module, you learn how to deploy a Spring Boot app to Azure Container Apps. You deploy a Spring Boot application to Azure Container Apps and maintain it using the built-in Java stack.
Certification
Microsoft Certified: Azure for SAP Workloads Specialty - Certifications
Demonstrate planning, migration, and operation of an SAP solution on Microsoft Azure while you leverage Azure resources.
Documentation
Log with the Azure SDK for Java and Logback - Java on Azure
Provides an overview of the Azure SDK for Java integration with Logback.
Structured Application Log for Azure Spring Apps
This article explains how to generate and collect structured application log data in Azure Spring Apps.