Skip to content

feat(SimpleLoggerAdvisor): Add log level configuration function #3747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
import org.springframework.lang.Nullable;

/**
* A simple logger advisor that logs the request and response messages.
* A configurable logger advisor that logs the request and response messages at
* configurable log levels.
*
* @author Christian Tzolov
* @author Engineer Zhong
*/
public class SimpleLoggerAdvisor implements CallAdvisor, StreamAdvisor {

Expand All @@ -52,19 +54,26 @@ public class SimpleLoggerAdvisor implements CallAdvisor, StreamAdvisor {

private final int order;

private final LogLevel requestLogLevel;

private final LogLevel responseLogLevel;

public SimpleLoggerAdvisor() {
this(DEFAULT_REQUEST_TO_STRING, DEFAULT_RESPONSE_TO_STRING, 0);
this(DEFAULT_REQUEST_TO_STRING, DEFAULT_RESPONSE_TO_STRING, 0, LogLevel.DEBUG, LogLevel.DEBUG);
}

public SimpleLoggerAdvisor(int order) {
this(DEFAULT_REQUEST_TO_STRING, DEFAULT_RESPONSE_TO_STRING, order);
this(DEFAULT_REQUEST_TO_STRING, DEFAULT_RESPONSE_TO_STRING, order, LogLevel.DEBUG, LogLevel.DEBUG);
}

public SimpleLoggerAdvisor(@Nullable Function<ChatClientRequest, String> requestToString,
@Nullable Function<ChatResponse, String> responseToString, int order) {
@Nullable Function<ChatResponse, String> responseToString, int order, LogLevel requestLogLevel,
LogLevel responseLogLevel) {
this.requestToString = requestToString != null ? requestToString : DEFAULT_REQUEST_TO_STRING;
this.responseToString = responseToString != null ? responseToString : DEFAULT_RESPONSE_TO_STRING;
this.order = order;
this.requestLogLevel = requestLogLevel;
this.responseLogLevel = responseLogLevel;
}

@Override
Expand All @@ -89,11 +98,41 @@ public Flux<ChatClientResponse> adviseStream(ChatClientRequest chatClientRequest
}

private void logRequest(ChatClientRequest request) {
logger.debug("request: {}", this.requestToString.apply(request));
logMessage(requestLogLevel, "request: {}", this.requestToString.apply(request));
}

private void logResponse(ChatClientResponse chatClientResponse) {
logger.debug("response: {}", this.responseToString.apply(chatClientResponse.chatResponse()));
logMessage(responseLogLevel, "response: {}", this.responseToString.apply(chatClientResponse.chatResponse()));
}

private void logMessage(LogLevel level, String format, Object arg) {
switch (level) {
case TRACE:
if (logger.isTraceEnabled()) {
logger.trace(format, arg);
}
break;
case DEBUG:
if (logger.isDebugEnabled()) {
logger.debug(format, arg);
}
break;
case INFO:
if (logger.isInfoEnabled()) {
logger.info(format, arg);
}
break;
case WARN:
if (logger.isWarnEnabled()) {
logger.warn(format, arg);
}
break;
case ERROR:
if (logger.isErrorEnabled()) {
logger.error(format, arg);
}
break;
}
}

@Override
Expand All @@ -115,6 +154,12 @@ public static Builder builder() {
return new Builder();
}

public enum LogLevel {

TRACE, DEBUG, INFO, WARN, ERROR

}

public static final class Builder {

private Function<ChatClientRequest, String> requestToString;
Expand All @@ -123,6 +168,10 @@ public static final class Builder {

private int order = 0;

private LogLevel requestLogLevel = LogLevel.DEBUG;

private LogLevel responseLogLevel = LogLevel.DEBUG;

private Builder() {
}

Expand All @@ -141,8 +190,25 @@ public Builder order(int order) {
return this;
}

public Builder requestLogLevel(LogLevel logLevel) {
this.requestLogLevel = logLevel;
return this;
}

public Builder responseLogLevel(LogLevel logLevel) {
this.responseLogLevel = logLevel;
return this;
}

public Builder logLevel(LogLevel logLevel) {
this.requestLogLevel = logLevel;
this.responseLogLevel = logLevel;
return this;
}

public SimpleLoggerAdvisor build() {
return new SimpleLoggerAdvisor(this.requestToString, this.responseToString, this.order);
return new SimpleLoggerAdvisor(this.requestToString, this.responseToString, this.order,
this.requestLogLevel, this.responseLogLevel);
}

}
Expand Down