自定义记录器

云眼About 5 min

自定义记录器

本主题描述如何定制有关来自云眼灰度实验 Java SDK 的实验的日志信息,以帮助进行调试。

记录器记录有关实验的信息,以帮助你进行调试。可以自定义日志信息的发送位置以及跟踪的信息类型。

在 Java SDK 中,缺省情况下不启用日志记录功能。为了改善设置 SDK 和配置生产环境的体验,我们建议在依赖项中包含 SLF4Jopen in new window 实现。对于Java SDK,我们需要使用SLF4Jopen in new window实现。

为了改善您设置 SDK 和配置生产环境的体验,我们建议为 Eyeofcloud 客户端传入记录器。请参阅下面的代码示例。

❗️

警告

由于最近宣布的 Log4J 库中存在安全漏洞open in new window,如果与 SDK 一起使用,我们建议尽快升级到变体 2.15.0 或更高版本。查看 Apache 关于 Log4j 漏洞的文档open in new window以获取更多信息。

build.gradle

// Provide a SLF4j binding like logback or log4j compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.25' compile group: 'log4j', name: 'log4j', version: '2.15.0'

slf4j.properties

//For formatting: under slf4j.properties # Root logger option log4j.rootLogger=DEBUG, stdout # Redirect log messages to console log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d {yyyy-MM-dd HH:mm:ss} %-5p %c {1} :%L - %m%n

为了更好地控制日志记录,例如控制每个包的日志记录级别或控制日志记录目标,我们建议使用日志open in new window

build.gradle

implementation 'ch.qos.logback:logback-classic:1.1.7'

将 logback.xml 文件保存在资源目录中。下面是捕获云眼灰度实验日志级别并将其管道传输到其他位置的简单示例。

登录.xml

<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern> %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n </pattern> </encoder> </appender> <appender name="MEMORY" class="com.eyeofcloud.intellij.plugin.utils.LogAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern> %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n </pattern> </encoder> </appender> <logger name="com.eyeofcloud" level="debug" additivity="false"> <appender-ref ref="MEMORY"/> </logger> <root level="error"> <appender-ref ref="CONSOLE"/> </root>

The preceding example pipes all debug logs to error messages. These are then sent to a different log appender.

You can also set logging levels for any Eyeofcloud package, giving you control over logs granularity. In the following example, we simply set the log level for the highest-level package, com.eyeofcloud:

logback.xml

<logger name="com.eyeofcloud" level="debug" />

Finally, the following general example show you how to write a custom appender rather than using one of the many default appenders:

LogAppender example

public class LogAppender extends AppenderBase<ILoggingEvent> { int counter = 0; public static AtomicBoolean captureLogging = new AtomicBoolean(false); public static List<String> logs = Collections.synchronizedList(new ArrayList<String>()); PatternLayoutEncoder encoder; @Override public void start() { if (this.encoder == null) { addError("No encoder set for the appender named ["+ name +"]."); return; } try { encoder.init(System.out); } catch (IOException e) { } super.start(); } public static void clearLogs() { synchronized (logs) { logs.clear(); } } public void append(ILoggingEvent event) { // output the events as formatted by our layout try { this.encoder.doEncode(event); if (captureLogging.get()) { synchronized (logs) { logs.add(event.getFormattedMessage()); } } } catch (IOException e) { } // prepare for next event counter++; } public PatternLayoutEncoder getEncoder() { return encoder; } public void setEncoder(PatternLayoutEncoder encoder) { this.encoder = encoder; } }

Log levels

The table below lists the log levels for the Java SDK.

Log Level

Explanation

ERROR

Events that prevent feature flags from functioning correctly (for example, invalid datafile in initialization and invalid feature keys) are logged. The user can take action to correct.

WARNING

Events that do not prevent feature flags from functioning correctly, but can have unexpected outcomes (for example, future API deprecation, logger or error handler are not set properly, and nil values from getters) are logged.

INFO

Events of significance (for example, decision started, decision succeeded, tracking started, and tracking succeeded) are logged. This is helpful in showing the lifecycle of an API call.

DEBUG

Any information related to errors that can help us debug the issue (for example, the feature flag is not running user is not included in the rollout) are logged.

Updated about 2 months ago


[

Event batching

](/experimentation/v4.0.0-full-stack/docs/event-batching-java)[

Customize error handler

](/experimentation/v4.0.0-full-stack/docs/customize-error-handler-java)

Did this page help you?

Yes

No

创建用户上下文

介绍创建用户上下文方法,该方法为云眼灰度实验中的标帜决策和事件创建用户上下文。

此方法的目的是创建用户并设置用户上下文一次,因此不必在每次做出标帜决策或跟踪事件时都指定用户。可以定义多个用户上下文。系统将用户上下文作为运行时对象返回,否则不会持久化。

版本

1.0.0-beta 或更高版本

描述

此调用为标帜决策和事件创建用户上下文。可以在 Eyeofcloud 客户端实例上成功调用此方法,甚至在完全配置实例之前也是如此。

参数

下表列出了必需参数和可选参数:

参数

类型

描述

(必选)用户 ID

字符串

用户的 ID。

属性
可选

Map

自定义键值字符串对的映射,指定系统用户用于受众群体定位的用户的属性。有关更多详细信息,请参阅以下部分。

受众群体属性

为用户设置自定义受众群体属性,可以使用这些属性来定位受众群体。可以将字符串、数字、布尔值和 nil 作为自定义用户属性值传递。如果要根据他们使用的应用程序变体定位访问群体,还可以传入格式为语义变体的open in new window字符串,然后在 Eyeofcloud 应用中定义受众条件。version

🚧 重要

在访问群体评估期间,如果没有为给定的访问群体条件传递有效的属性值(例如,如果在受众群体条件需要布尔值时传递字符串,或者忘记传递值),则系统会跳过该条件。发生这种情况时,SDK 日志会包含警告。

返回

返回一个 EyeofcloudUserContext 对象。有关详细信息,请参阅云眼用户上下文

Dart

// option 1: create a user, then set attributes var user = await flutterSDK.createUserContext("user123"); var attributes = <String, dynamic>{}; attributes["is_logged_in"] = false; attributes["app_version"] = "1.3.2"; user!.setAttributes(attributes); // option 2: pass attributes when creating the user var attributes = <String, dynamic>{}; attributes["is_logged_in"] = false; attributes["app_version"] = "1.3.2"; var eyeofcloudUserContext = await eyeofcloudClient.createUserContext("user123", attributes);

参见

云眼用户上下文

源文件

包含 Flutter SDK for Android Eyeofcloudopen in new window 和 Swift 实现的语言/平台源文件.java为 EyeofcloudClient.swiftopen in new window

Last update:
Contributors: “zhangweixue”