Skip to content

fix(agent): Make sure the log-file directory exists#18818

Open
srebhan wants to merge 1 commit intoinfluxdata:masterfrom
srebhan:agent_issue_16186
Open

fix(agent): Make sure the log-file directory exists#18818
srebhan wants to merge 1 commit intoinfluxdata:masterfrom
srebhan:agent_issue_16186

Conversation

@srebhan
Copy link
Copy Markdown
Member

@srebhan srebhan commented Apr 29, 2026

Summary

This PR creates the log-file directory if necessary to ensure it exists.

Checklist

Related issues

resolves #16186

@telegraf-tiger telegraf-tiger Bot added the fix pr to fix corresponding bug label Apr 29, 2026
@telegraf-tiger
Copy link
Copy Markdown
Contributor

@srebhan srebhan added the ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. label Apr 29, 2026
Copy link
Copy Markdown
Contributor

@skartikey skartikey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srebhan Thanks! A couple of minor comments.

add("structured", func(cfg *Config) (sink, error) {
var writer io.Writer = os.Stderr
if cfg.Logfile != "" {
// Make sure the directory for the log-file exists
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a small test in logger/text_logger_test.go and logger/structured_logger_test.go that points Logfile at filepath.Join(t.TempDir(), "subdir", "test.log") to lock in the new behavior? The existing tests already use t.TempDir() so it's a one-liner per file.

Comment thread logger/text_logger.go
func createTextLogger(cfg *Config) (sink, error) {
var writer io.Writer = os.Stderr
if cfg.Logfile != "" {
// Make sure the directory for the log-file exists
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: the four-line os.MkdirAll(...) block is identical in both sinks, small helper (e.g. ensureLogDir) in logger/registry.go would avoid duplication if a third sink is ever added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agent area/logging fix pr to fix corresponding bug ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create log directory if not exist

2 participants