fix(api-service): replace zap with logrus for unified logging#72
fix(api-service): replace zap with logrus for unified logging#72puzhen-ryan merged 3 commits intomainfrom
Conversation
- Replace zap with logrus + lumberjack, matching faas-apiserver pattern - Unify all standard "log" imports to logrus across service/controller - Add request/response body truncation (2KB) to prevent log bloat - Tune rotation: MaxBackups=10, MaxAge=7 days, Compress=true - Remove go.uber.org/zap and go.uber.org/multierr dependencies Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses critical logging issues by standardizing the logging infrastructure. Previously, the system suffered from mixed logging libraries and significant log disk bloat due to full request/response body logging. The changes unify logging under Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request replaces the zap logging library with logrus to unify logging across the service, improving consistency and introducing valuable features like request/response body truncation and tuned log rotation for better log volume management. However, a critical security finding has been identified: the logging middleware continues to log unfiltered request and response bodies, which may contain sensitive information such as environment variables and secrets. Further improvements include enhancing the structure of latency logs for better analysis and ensuring the string truncation logic is safe for multi-byte UTF-8 characters to prevent potential log corruption.
| // Add request body (truncated) | ||
| if len(reqBody) > 0 { | ||
| fields = append(fields, zap.String("request_body", string(reqBody))) | ||
| fields["request_body"] = truncateString(string(reqBody), maxBodyLogSize) | ||
| } | ||
|
|
||
| // Add response body (if exists) | ||
| // Add response body (truncated) | ||
| if blw.body.Len() > 0 { | ||
| fields = append(fields, zap.String("response_body", blw.body.String())) | ||
| fields["response_body"] = truncateString(blw.body.String(), maxBodyLogSize) | ||
| } |
There was a problem hiding this comment.
The logging middleware captures and logs the entire request and response bodies (truncated to 2KB). These bodies can contain sensitive information such as authentication tokens, passwords, or PII. Specifically, the CreateEnvInstanceRequest includes environment_variables, which are often used to store secrets. Logging these secrets to a file increases the risk of exposure if the log files are compromised. It is recommended to implement a sanitization or redaction mechanism to remove sensitive fields from the request and response bodies before logging, or avoid logging the bodies entirely in production environments.
| "method": c.Request.Method, | ||
| "path": c.Request.URL.Path, | ||
| "status": statusCode, | ||
| "latency": latency.String(), |
There was a problem hiding this comment.
Logging latency as a string (latency.String()) is less flexible for analysis and filtering in log management systems compared to a numeric type. Logrus can handle time.Duration fields natively, typically logging them as an integer (nanoseconds). This allows for easier querying, sorting, and calculations on the latency value.
| "latency": latency.String(), | |
| "latency": latency, |
| if len(s) > maxLen { | ||
| return s[:maxLen] + "...(truncated)" | ||
| } |
There was a problem hiding this comment.
The current implementation of truncateString can corrupt multi-byte UTF-8 characters by slicing a string at an arbitrary byte index. This can lead to invalid characters in the logs, making them hard to read or process. A safer approach is to ensure the truncation happens at a rune boundary.
if len(s) > maxLen {
// To avoid cutting a multi-byte character in half, find the last valid start of a UTF-8 rune.
end := maxLen
for end > 0 && (s[end]&0xC0) == 0x80 { // is a continuation byte
end--
}
return s[:end] + "...(truncated)"
}Replace log.Printf/Println with semantic log levels: - Errors (failed operations) → log.Errorf - Warnings (close body, parse issues) → log.Warnf - Info (lifecycle, success) → log.Infof - Debug (routine queries, key details) → log.Debugf Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Align with faas-apiserver log rotation config. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary
"log"imports to logrus across service/controller layers so all logs write to filego.uber.org/zapandgo.uber.org/multierrdependenciesMotivation
Production logs were growing at ~50MB/min due to full request/response body logging (especially MCP traffic). Additionally, service/controller layers used mixed logging libraries (standard
log, logrus, zap) with only zap writing to log files — meaning most application logs were lost to stderr.Changes
middleware/logging.gomain.goservice/redis.go"log"→ logrusservice/cleanup_service.go"log"→ logrusservice/env_instance.go"log"→ logruscontroller/mcp_proxy.go"log"→ logrusgo.modTest plan
go build ./...passesgo vet ./...passes🤖 Generated with Claude Code