-
Notifications
You must be signed in to change notification settings - Fork 0
log dir setup #59
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
Merged
Merged
log dir setup #59
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,10 @@ import ( | |
"log/slog" | ||
"os" | ||
"os/signal" | ||
"path/filepath" | ||
"strings" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/coder/boundary" | ||
"github.com/coder/boundary/audit" | ||
|
@@ -22,6 +24,7 @@ import ( | |
type Config struct { | ||
AllowStrings []string | ||
LogLevel string | ||
LogDir string | ||
Unprivileged bool | ||
} | ||
|
||
|
@@ -56,20 +59,26 @@ func BaseCommand() *serpent.Command { | |
Short: "Network isolation tool for monitoring and restricting HTTP/HTTPS requests", | ||
Long: `boundary creates an isolated network environment for target processes, intercepting HTTP/HTTPS traffic through a transparent proxy that enforces user-defined allow rules.`, | ||
Options: []serpent.Option{ | ||
serpent.Option{ | ||
{ | ||
Flag: "allow", | ||
Env: "BOUNDARY_ALLOW", | ||
Description: "Allow rule (repeatable). Format: \"pattern\" or \"METHOD[,METHOD] pattern\".", | ||
Value: serpent.StringArrayOf(&config.AllowStrings), | ||
}, | ||
serpent.Option{ | ||
{ | ||
Flag: "log-level", | ||
Env: "BOUNDARY_LOG_LEVEL", | ||
Description: "Set log level (error, warn, info, debug).", | ||
Default: "warn", | ||
Value: serpent.StringOf(&config.LogLevel), | ||
}, | ||
serpent.Option{ | ||
{ | ||
Flag: "log-dir", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need the option to send logs to a directory when running applications like claude inside boundary so that the logs don't break up/cause bugs in the tui rendering |
||
Env: "BOUNDARY_LOG_DIR", | ||
Description: "Set a directory to write logs to rather than stderr.", | ||
Value: serpent.StringOf(&config.LogDir), | ||
}, | ||
{ | ||
Flag: "unprivileged", | ||
Env: "BOUNDARY_UNPRIVILEGED", | ||
Description: "Run in unprivileged mode (no network isolation, uses proxy environment variables).", | ||
|
@@ -87,7 +96,11 @@ func BaseCommand() *serpent.Command { | |
func Run(ctx context.Context, config Config, args []string) error { | ||
ctx, cancel := context.WithCancel(ctx) | ||
defer cancel() | ||
logger := setupLogging(config.LogLevel) | ||
|
||
logger, err := setupLogging(config) | ||
if err != nil { | ||
return fmt.Errorf("could not set up logging: %v", err) | ||
} | ||
username, uid, gid, homeDir, configDir := util.GetUserInfo() | ||
|
||
// Get command arguments | ||
|
@@ -204,9 +217,9 @@ func Run(ctx context.Context, config Config, args []string) error { | |
} | ||
|
||
// setupLogging creates a slog logger with the specified level | ||
func setupLogging(logLevel string) *slog.Logger { | ||
func setupLogging(config Config) (*slog.Logger, error) { | ||
var level slog.Level | ||
switch strings.ToLower(logLevel) { | ||
switch strings.ToLower(config.LogLevel) { | ||
case "error": | ||
level = slog.LevelError | ||
case "warn": | ||
|
@@ -219,12 +232,34 @@ func setupLogging(logLevel string) *slog.Logger { | |
level = slog.LevelWarn // Default to warn if invalid level | ||
} | ||
|
||
logTarget := os.Stderr | ||
|
||
if config.LogDir != "" { | ||
// Set up the logging directory if it doesn't exist yet | ||
if err := os.MkdirAll(config.LogDir, 0755); err != nil { | ||
return nil, fmt.Errorf("could not set up log dir %s: %v", config.LogDir, err) | ||
} | ||
|
||
// Create a logfile (timestamp and pid to avoid race conditions with multiple boundary calls running) | ||
logFilePath := fmt.Sprintf("boundary-%s-%d.log", | ||
time.Now().Format("2006-01-02_15-04-05"), | ||
os.Getpid()) | ||
|
||
logFile, err := os.Create(filepath.Join(config.LogDir, logFilePath)) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not create log file %s: %v", logFilePath, err) | ||
} | ||
|
||
// Set the log target to the file rather than stderr. | ||
logTarget = logFile | ||
} | ||
|
||
// Create a standard slog logger with the appropriate level | ||
handler := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{ | ||
handler := slog.NewTextHandler(logTarget, &slog.HandlerOptions{ | ||
Level: level, | ||
}) | ||
|
||
return slog.New(handler) | ||
return slog.New(handler), nil | ||
} | ||
|
||
// createJailer creates a new jail instance for the current platform | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type annotations are redundant.