【Azure Spring Cloud】Azure Spring Cloud服务,如何获取应用程序日志文件呢?
问题描述
在使用Azure Spring Cloud服务时,如果要收集应用程序的日志。有控制台输出(实时流日志),也可以配置Log Analytics服务。
日志流式处理
可以通过以下命令在 Azure CLI 中使用日志流式处理。
az spring-cloud app logs -n hellospring -s yourspringcloudname -g <resource group name> --lines 100 -f

Log Analytics
在Azure Spring Cloud门户页面,转到“服务 | 概述”页,然后在“监视”部分中选择“日志” 。 选择 Azure Spring Cloud 的一个示例查询上的“运行”。

但是,如果应用中需要自行把日志写入到日志文件中,那么如果应用部署到Azure Spring Cloud 上后,如何来查看并获取到应用程序自身专门用于记录日志的文件呢?

问题解答
Azure Spring Cloud 与 App Service有较大的区别。App Service可以通过Kudu工具访问应用的文件系统,就可以在Home目录下直接看见应用程序生成的日志文件并下载。
而Spring Cloud服务,需要通过文件挂载(Mount)的方式,把一个存储账号(Storage Account)挂载到Spring Cloud的App上。
挂载Storage Account以及在代码中配置挂载后的日志路径步骤:
第一步:为Azure Spring Cloud服务添加一个Storage

- Storage name:为自定义名称,根据自己情况设定,如 springstorage01
- Account name:为存储账号(Storage Account)的名称,需要从Azure Storage Account的页面中获取
- Account Key:从Azure Storage Account的Access Key页面中获取。注意:此处只需要填写Access Key就可以,不需要完整的Conneciton String
第二步:为Spring Cloud App添加挂载
在Spring Cloud页面,点击“Apps”列举出所有的App。选中需要配置日志文件路径的应用。然后选择“Configuration” --> "Persistent Storage"

- Storage Name:为第一步中自定义的Storage Name。
- Share Name:为在Azure Storage Account的文件共享中所创建的一个共享文件夹。可以自定义文件夹名称。
- Mount Path: 所挂载Spring Cloud App所运行实例上的文件路径。这一步的内容也是将在应用为日志文件所配置的存放路径。非常关键!如使用 /app/logs
第三步:在Java Spring应用中重新配置日志生成路径
修改应用中的日志保存路径。如本实例中使用的 logback-spring.xml 。 修改文件输出路径为: <file>/app/logs/test.log</file>

重新生成Jar文件并再次发布。
第四步: 在存储账号中检查应用日志
在Azure门户中,进入Storage Account中,查看Spring Cloud App的运行日志。如下:

附录:Spring Cloud应用示例代码
POM.XML 依赖包文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.11</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>hellospring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hellospring</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>6.5</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
application.properties 配置文件
spring.cloud.config.enabled=false
logback-spring.xml配置文件
<configuration>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp>
<fieldName>timestamp</fieldName>
<timeZone>UTC</timeZone>
</timestamp>
<loggerName>
<fieldName>logger</fieldName>
</loggerName>
<logLevel>
<fieldName>level</fieldName>
</logLevel>
<threadName>
<fieldName>thread</fieldName>
</threadName>
<nestedField>
<fieldName>mdc</fieldName>
<providers>
<mdc />
</providers>
</nestedField>
<stackTrace>
<fieldName>stackTrace</fieldName>
<!-- maxLength - limit the length of the stack trace -->
<throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
<maxDepthPerThrowable>200</maxDepthPerThrowable>
<maxLength>14000</maxLength>
<rootCauseFirst>true</rootCauseFirst>
</throwableConverter>
</stackTrace>
<message />
<throwableClassName>
<fieldName>exceptionClass</fieldName>
</throwableClassName>
</providers>
</encoder>
</appender>
<appender name="files" class="ch.qos.logback.core.FileAppender">
<file>/app/logs/test.log</file>
<append>true</append>
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp>
<fieldName>timestamp</fieldName>
<timeZone>UTC</timeZone>
</timestamp>
<loggerName>
<fieldName>logger</fieldName>
</loggerName>
<logLevel>
<fieldName>level</fieldName>
</logLevel>
<threadName>
<fieldName>thread</fieldName>
</threadName>
<nestedField>
<fieldName>mdc</fieldName>
<providers>
<mdc />
</providers>
</nestedField>
<stackTrace>
<fieldName>stackTrace</fieldName>
<!-- maxLength - limit the length of the stack trace -->
<throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
<maxDepthPerThrowable>200</maxDepthPerThrowable>
<maxLength>14000</maxLength>
<rootCauseFirst>true</rootCauseFirst>
</throwableConverter>
</stackTrace>
<message />
<throwableClassName>
<fieldName>exceptionClass</fieldName>
</throwableClassName>
</providers>
</encoder>
</appender>
<root level="info">
<appender-ref ref="stdout" />
<appender-ref ref="files" />
</root>
</configuration>
HelloController.java 代码
package com.example.hellospring; import org.springframework.web.bind.annotation.RestController;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping; @RestController
public class HelloController { org.slf4j.Logger logger = LoggerFactory.getLogger(getClass()); @RequestMapping("/")
public String index() { logger.info("Loging into Storage Folder.... request from index page.... test by lb @09-02"); return "Greetings from Azure Spring Cloud!";
} }
HellospringApplication.java 代码
package com.example.hellospring; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class HellospringApplication { public static void main(String[] args) {
SpringApplication.run(HellospringApplication.class, args);
} }
参考资料
快速入门:在 Azure Spring Cloud 中部署你的第一个应用程序:https://docs.azure.cn/zh-cn/spring-cloud/quickstart?tabs=Azure-CLI#build-and-deploy-the-app
日志: https://docs.azure.cn/zh-cn/spring-cloud/quickstart-logs-metrics-tracing?tabs=Azure-CLI
【Azure Spring Cloud】Azure Spring Cloud服务,如何获取应用程序日志文件呢?的更多相关文章
- C# windows服务:如何获取服务程序所在的文件夹
AppDomain.CurrentDomain.BaseDirectory 就这么一句话
- react native android 上传文件,Nodejs服务端获取上传的文件
React Native端 使用react-native-image-picker 做出选择图片的操作,选择完成后,直接将图片Post至服务器,保存在服务器的某个地方(保存图片的路径需要公开显示),并 ...
- 从Spring迁移到Spring Boot
文章目录 添加Spring Boot starters 添加应用程序入口 Import Configuration和Components 迁移应用程序资源 迁移应用程序属性文件 迁移Spring We ...
- spring boot / cloud (十四) 微服务间远程服务调用的认证和鉴权的思考和设计,以及restFul风格的url匹配拦截方法
spring boot / cloud (十四) 微服务间远程服务调用的认证和鉴权的思考和设计,以及restFul风格的url匹配拦截方法 前言 本篇接着<spring boot / cloud ...
- 笔记:Spring Cloud Feign 声明式服务调用
在实际开发中,对于服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以我们通常会针对各个微服务自行封装一些客户端类来包装这些依赖服务的调用,Spring Cloud Feign 在此基础上做了进 ...
- spring boot / cloud (二十) 相同服务,发布不同版本,支撑并行的业务需求
spring boot / cloud (二十) 相同服务,发布不同版本,支撑并行的业务需求 有半年多没有更新了,按照常规剧本,应该会说项目很忙,工作很忙,没空更新,吧啦吧啦,相关的话吧, 但是细想想 ...
- Spring Cloud 入门教程 - Eureka服务注册与发现
简介 在微服务中,服务注册与发现对管理各个微服务子系统起着关键作用.随着系统水平扩展的越来越多,系统拆分为微服务的数量也会相应增加,那么管理和获取这些微服务的URL就会变得十分棘手,如果我们每新加一个 ...
- 《Spring Cloud与Docker微服务架构实战》配套代码
不才写了本使用Spring Cloud玩转微服务架构的书,书名是<Spring Cloud与Docker微服务架构实战> - 周立,已于2017-01-12交稿.不少朋友想先看看源码,现将 ...
- 一起来学Spring Cloud | 第二章:服务注册和发现组件 (Eureka)
本篇文章,很浅显的一步步讲解如何搭建一个能运行的springcloud项目(带所有操作截图).相信!看完本篇之后,你会觉得springcloud搭建如此简单~~~~ 一. Eureka简介: 1.1 ...
随机推荐
- React中render Props模式
React组件复用 React组件复用的方式有两种: 1.render Props模式 2.高阶组件HOC 上面说的这两种方式并不是新的APi. 而是利用Raect自身的编码特点,演化而来的固定编码写 ...
- python 基础知识-day6(内置函数)
1.sorted():用于字典的排序 dict1={"name":"cch","age":"3","sex&q ...
- 『现学现忘』Docker基础 — 38、COPY指令和ADD指令
目录 1.COPY指令 (1)COPY指令说明 (2)COPY指令格式 (3)COPY指令使用 (4)其他 2.ADD指令 (1)ADD指令说明 (2)ADD指令格式 (3)ADD指令使用 (4)不推 ...
- mysql备份数据库linux
备份数据库 问题描述: 我们用的是mysql,以今天遇到的情况为例,我们是在两台服务器上要搭相同的平台,部署完成后页面报错,发现是数据库的问题,我们打开数据库查看,确实数据库中少建一个wind数据 ...
- HHL论文及代码理解(Generalizing A Person Retrieval Model Hetero- and Homogeneously ECCV 2018)
行人再识别Re-ID面临两个特殊的问题: 1)源数据集和目标数据集类别完全不同 2)相机造成的图片差异 因为一般来说传统的域适应问题源域和目标域的类别是相同的,相机之间的不匹配也是造成行人再识别数据集 ...
- java通过注解顺序通过映射导出excel
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.ann ...
- Note -「0/1 Fractional Programming」
What is that? Let us pay attention to a common problem that we often meet in daily life: There are \ ...
- 「Python实用秘技09」更好用的函数运算缓存
本文完整示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/PythonPracticalSkills 这是我的系列文章「Python实用秘技」的第9期 ...
- 二分法求最长子序列长度(STL)(nlogn)
声明: 正如标题所说,只是求长度,应对题目要求,请自行判断,用错代码概不负责! 本蒟蒻的代码可能有错,有错误还请各位dalao请指出 运用了upper_bound()和lower_bound()函数 ...
- 2022-7-11第五组 pan小堂 js基础
##为何学习 JavaScript? ###JavaScript 是 web 开发者必学的三种语言之一: HTML 定义网页的内容 CSS 规定网页的布局 JavaScript 对网页行为进行编程 在 ...