@@ -20,6 +20,7 @@ import (
20
20
"io"
21
21
"math"
22
22
"os"
23
+ "path/filepath"
23
24
"strconv"
24
25
25
26
"github.com/dustin/go-humanize"
@@ -146,51 +147,68 @@ func (fw FileWriter) WriterKey() string {
146
147
147
148
// OpenWriter opens a new file writer.
148
149
func (fw FileWriter ) OpenWriter () (io.WriteCloser , error ) {
149
- if fw .Mode == 0 {
150
- fw .Mode = 0o600
150
+ modeIfCreating := os .FileMode (fw .Mode )
151
+ if modeIfCreating == 0 {
152
+ modeIfCreating = 0o600
151
153
}
152
154
153
- // roll log files by default
154
- if fw .Roll == nil || * fw .Roll {
155
- if fw .RollSizeMB == 0 {
156
- fw .RollSizeMB = 100
157
- }
158
- if fw .RollCompress == nil {
159
- compress := true
160
- fw .RollCompress = & compress
161
- }
162
- if fw .RollKeep == 0 {
163
- fw .RollKeep = 10
164
- }
165
- if fw .RollKeepDays == 0 {
166
- fw .RollKeepDays = 90
167
- }
155
+ // roll log files as a sensible default to avoid disk space exhaustion
156
+ roll := fw .Roll == nil || * fw .Roll
168
157
169
- // create the file if it does not exist with the right mode.
170
- // lumberjack will reuse the file mode across log rotation.
171
- f_tmp , err := os .OpenFile (fw .Filename , os .O_WRONLY | os .O_APPEND | os .O_CREATE , os .FileMode (fw .Mode ))
158
+ // create the file if it does not exist; create with the configured mode, or default
159
+ // to restrictive if not set. (lumberjack will reuse the file mode across log rotation)
160
+ if err := os .MkdirAll (filepath .Dir (fw .Filename ), 0o700 ); err != nil {
161
+ return nil , err
162
+ }
163
+ file , err := os .OpenFile (fw .Filename , os .O_WRONLY | os .O_APPEND | os .O_CREATE , modeIfCreating )
164
+ if err != nil {
165
+ return nil , err
166
+ }
167
+ info , err := file .Stat ()
168
+ if roll {
169
+ file .Close () // lumberjack will reopen it on its own
170
+ }
171
+
172
+ // Ensure already existing files have the right mode, since OpenFile will not set the mode in such case.
173
+ if configuredMode := os .FileMode (fw .Mode ); configuredMode != 0 {
172
174
if err != nil {
173
- return nil , err
175
+ return nil , fmt . Errorf ( "unable to stat log file to see if we need to set permissions: %v" , err )
174
176
}
175
- f_tmp . Close ()
176
- // ensure already existing files have the right mode,
177
- // since OpenFile will not set the mode in such case.
178
- if err = os . Chmod ( fw . Filename , os . FileMode ( fw . Mode )); err != nil {
179
- return nil , err
177
+ // only chmod if the configured mode is different
178
+ if info . Mode () & os . ModePerm != configuredMode & os . ModePerm {
179
+ if err = os . Chmod ( fw . Filename , configuredMode ); err != nil {
180
+ return nil , err
181
+ }
180
182
}
183
+ }
181
184
182
- return & lumberjack.Logger {
183
- Filename : fw .Filename ,
184
- MaxSize : fw .RollSizeMB ,
185
- MaxAge : fw .RollKeepDays ,
186
- MaxBackups : fw .RollKeep ,
187
- LocalTime : fw .RollLocalTime ,
188
- Compress : * fw .RollCompress ,
189
- }, nil
185
+ // if not rolling, then the plain file handle is all we need
186
+ if ! roll {
187
+ return file , nil
190
188
}
191
189
192
- // otherwise just open a regular file
193
- return os .OpenFile (fw .Filename , os .O_WRONLY | os .O_APPEND | os .O_CREATE , os .FileMode (fw .Mode ))
190
+ // otherwise, return a rolling log
191
+ if fw .RollSizeMB == 0 {
192
+ fw .RollSizeMB = 100
193
+ }
194
+ if fw .RollCompress == nil {
195
+ compress := true
196
+ fw .RollCompress = & compress
197
+ }
198
+ if fw .RollKeep == 0 {
199
+ fw .RollKeep = 10
200
+ }
201
+ if fw .RollKeepDays == 0 {
202
+ fw .RollKeepDays = 90
203
+ }
204
+ return & lumberjack.Logger {
205
+ Filename : fw .Filename ,
206
+ MaxSize : fw .RollSizeMB ,
207
+ MaxAge : fw .RollKeepDays ,
208
+ MaxBackups : fw .RollKeep ,
209
+ LocalTime : fw .RollLocalTime ,
210
+ Compress : * fw .RollCompress ,
211
+ }, nil
194
212
}
195
213
196
214
// UnmarshalCaddyfile sets up the module from Caddyfile tokens. Syntax:
0 commit comments