【Azure 事件中心】关闭或开启Azure Event Hub SDK中的日志输出

简介: 【Azure 事件中心】关闭或开启Azure Event Hub SDK中的日志输出

问题描述

使用Azure Event Hub的Java SDK 作为消费端消费消息,集成在项目中后,发现大量日志产生,并且都是Debug 级别日志,如何来关闭这部分日志输出呢?

import com.azure.messaging.eventhubs.EventHubClientBuilder;
import com.azure.messaging.eventhubs.EventProcessorClient;
import com.azure.messaging.eventhubs.EventProcessorClientBuilder;
import com.azure.messaging.eventhubs.checkpointstore.blob.BlobCheckpointStore;
import com.azure.messaging.eventhubs.models.EventContext;
import com.azure.storage.blob.BlobContainerAsyncClient;
import com.azure.storage.blob.BlobContainerClientBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
 
@component
@slf4j
public class AzureDeviceLifecycleListener {
 
private static String connectionString = "Endpoint=*****";
private static String storageConnectionString = "******";
private static String storageContainerName = "*******";
 
private static BlobContainerAsyncClient blobContainerAsyncClient = new BlobContainerClientBuilder()
        .connectionString(storageConnectionString)
        .containerName(storageContainerName)
        .buildAsyncClient();
 
private static EventProcessorClient eventProcessorClient;
 
private static void doProcessEvent(EventContext eventContext) {
    byte[] body = eventContext.getEventData().getBody();
    System.out.println("body string: " + new String(body));
}
 
public static void main(String[] args) {
    eventProcessorClient = new EventProcessorClientBuilder()
            .consumerGroup(EventHubClientBuilder.DEFAULT_CONSUMER_GROUP_NAME)
            .connectionString(connectionString)
            .checkpointStore(new BlobCheckpointStore(blobContainerAsyncClient))
            .processEvent(AzureDeviceLifecycleListener::doProcessEvent)
            .processError(errorContext -> log.error("Error occurred while processing azure events " + errorContext.getThrowable().getMessage()))
            .buildEventProcessorClient();

在应用启动后,输出了大量Event Hub SDK中的日志,导致产生大量日志内容,干扰正常的业务日志,需要关闭。

 

问题解答

这是因为Event Hub SDK使用的日志也是使用 log4j 输出日志,所以当整个项目配置 log4j.properties 的日志级别定义为Debug后, Event Hub SDK中所记录的日志同样输出到Console页面。 当不需要DEBUG级别日志的时候,只需要在配置文件中去掉即可!

在log4j.properties中去掉 debug 配置即可。改为 error 或者是 Warn级别,既能减少日志输出!

og4j.rootCategory=INFO, stdout , R
此句为将等级为INFO的日志信息输出到stdout和R这两个目的地,
stdout和R的定义在下面的代码,可以任意起名。
等级可分为OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALL,
如果配置OFF则不打出任何信息,
如果配置为INFO这样只显示INFO, WARN, ERROR的log信息,
而DEBUG信息不会被显示
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
此句为定义名为stdout的输出端是哪种类型,有:
org.apache.log4j.ConsoleAppender(控制台)
org.apache.log4j.FileAppender(文件)
org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件)
org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生一个新的文件)
org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方)

 

参考文件

java日志文件log4j.properties配置详解: https://www.cnblogs.com/cuiqq/p/11175975.html

 

相关实践学习
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
相关文章
|
JSON API 数据格式
【Azure APIM】如何把APIM中处理的请求的所有请求头保存在日志中?
Azure API Management 默认诊断日志不记录请求的 Header 和 Body 信息。为实现记录,可通过配置 Trace 策略解决。例如,使用 `context.Request.Headers` 和 `context.Request.Body` 获取相关信息,并以 JSON 或字符串格式保存。示例代码展示了如何将 Headers 转换为 JSON 或逗号分隔字符串形式记录。相关参考资料包括 Set Body Policy 和 Trace Policy 官方文档,帮助进一步了解与扩展功能。
400 36
|
存储 Windows
【Azure Cloud Service】微软云服务上的日志收集方法
本文介绍了在使用微软云服务(Cloud Service Extended Support)时,如何收集日志以分析未记录在应用日志中的服务异常。由于云服务基于传统虚拟机模式,需通过远程桌面登录实例,查看IIS、Windows Event及云服务组件日志(如WindowsAzureGuestAgent)。此外,可使用CollectGuestLogs.exe工具打包日志,或通过“File Server Resource Manager”检查日志存储配额是否不足。附参考文档链接供深入学习。
531 29
|
存储 监控 API
【Azure App Service】分享使用Python Code获取App Service的服务器日志记录管理配置信息
本文介绍了如何通过Python代码获取App Service中“Web服务器日志记录”的配置状态。借助`azure-mgmt-web` SDK,可通过初始化`WebSiteManagementClient`对象、调用`get_configuration`方法来查看`http_logging_enabled`的值,从而判断日志记录是否启用及存储方式(关闭、存储或文件系统)。示例代码详细展示了实现步骤,并附有执行结果与官方文档参考链接,帮助开发者快速定位和解决问题。
464 22
|
API 开发工具 Python
FFmpeg【SDK01】日志和字典的使用
FFmpeg中日志功能的使用方法,包括日志级别的设置和AVDictionary的基本操作,同时展示了字符串解析函数如av_parse_video_size、av_parse_video_rate和av_parse_time的应用。
392 2
【Azure Policy】分享Policy实现对Azure Activity Log导出到Log A workspace中
在Policy Rule部分中,选择资源的类型为 "Microsoft.Resources/subscriptions", 效果使用 DeployIfNotExists (如果不存在,则通过修复任务进行修正。 在 existenceCondition 条件中,如果当前订阅已经启用了 diagnostic setting并且输出日志到同一个Log A workspace,表示满足Policy要求,不需要进行修正。 在 deployment 中,使用了 ARM 模板, 为订阅添加Diagnostic Setting并且所有的日志Category均启用。
237 3
|
Kubernetes 数据安全/隐私保护 容器
【Azure APIM】APIM Self-Hosted网关中,添加网关日志以记录请求头信息(Request Header / Response Header)
【Azure APIM】APIM Self-Hosted网关中,添加网关日志以记录请求头信息(Request Header / Response Header)
365 3
[Azure Developer]把Azure Function中ILogger对象静态化为静态方法提供日志记录
[Azure Developer]把Azure Function中ILogger对象静态化为静态方法提供日志记录
231 3
【Azure Function & Application Insights】在Azure Function的日志中,发现DrainMode mode enabled Traces。它是什么意思呢?
【Azure Function & Application Insights】在Azure Function的日志中,发现DrainMode mode enabled Traces。它是什么意思呢?
309 1
|
存储 大数据 索引
【Azure Contianer Apps】在云上使用容器应用时收集日志遇见延迟问题
【Azure Contianer Apps】在云上使用容器应用时收集日志遇见延迟问题
238 0