-
Notifications
You must be signed in to change notification settings - Fork 45
feat: add OutputFile option for log destination in logger #152
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ package logger | |
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| ) | ||
|
|
||
| const ( | ||
|
|
@@ -33,6 +34,9 @@ type Options struct { | |
|
|
||
| // OutputLevel is the level of logging | ||
| OutputLevel string | ||
|
|
||
| // OutputFile is the destination file path for logs. | ||
| OutputFile string | ||
| } | ||
|
|
||
| // SetOutputLevel sets the log output level. | ||
|
|
@@ -62,6 +66,11 @@ func (o *Options) AttachCmdFlags( | |
| "log-level", | ||
| defaultOutputLevel, | ||
| "Options are debug, info, warn, error, or fatal (default info)") | ||
| stringVar( | ||
| &o.OutputFile, | ||
| "log-file", | ||
| "", | ||
| "Path to a file where logs will be written") | ||
| } | ||
|
|
||
| if boolVar != nil { | ||
|
|
@@ -79,6 +88,7 @@ func DefaultOptions() Options { | |
| JSONFormatEnabled: defaultJSONOutput, | ||
| appID: undefinedAppID, | ||
| OutputLevel: defaultOutputLevel, | ||
| OutputFile: "", | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -104,5 +114,16 @@ func ApplyOptionsToLoggers(options *Options) error { | |
| v.SetOutputLevel(daprLogLevel) | ||
| } | ||
|
|
||
| if options.OutputFile != "" { | ||
| file, err := os.OpenFile(options.OutputFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to open log file %q: %w", options.OutputFile, err) | ||
| } | ||
|
|
||
| for _, v := range internalLoggers { | ||
| v.SetOutput(file) | ||
| } | ||
| } | ||
|
Comment on lines
+117
to
+126
|
||
|
|
||
| return nil | ||
| } | ||
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.
Should we relax permissions a bit and allow reads?