Skip to content

Commit

Permalink
httpserver/roller: introduce rotate_compress directive
Browse files Browse the repository at this point in the history
This directive will enable gzip compression provided by [Lumberjack](natefinch/lumberjack#43).

The directive `rotate_compress` can be `true` or `false`, being `false` by default.
  • Loading branch information
fern4lvarez committed Jun 28, 2017
1 parent ab51133 commit 7d66158
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
4 changes: 3 additions & 1 deletion caddyhttp/errors/setup_test.go
Expand Up @@ -85,13 +85,14 @@ func TestErrorsParse(t *testing.T) {
Roller: httpserver.DefaultLogRoller(),
},
}},
{`errors errors.txt { rotate_size 2 rotate_age 10 rotate_keep 3 }`, false, ErrorHandler{
{`errors errors.txt { rotate_size 2 rotate_age 10 rotate_keep 3 rotate_compress true }`, false, ErrorHandler{
ErrorPages: map[int]string{},
Log: &httpserver.Logger{
Output: "errors.txt", Roller: &httpserver.LogRoller{
MaxSize: 2,
MaxAge: 10,
MaxBackups: 3,
Compress: true,
LocalTime: true,
},
},
Expand All @@ -113,6 +114,7 @@ func TestErrorsParse(t *testing.T) {
MaxSize: 3,
MaxAge: 11,
MaxBackups: 5,
Compress: false,
LocalTime: true,
},
},
Expand Down
26 changes: 20 additions & 6 deletions caddyhttp/httpserver/roller.go
Expand Up @@ -14,6 +14,7 @@ type LogRoller struct {
MaxSize int
MaxAge int
MaxBackups int
Compress bool
LocalTime bool
}

Expand All @@ -37,6 +38,7 @@ func (l LogRoller) GetLogWriter() io.Writer {
MaxSize: l.MaxSize,
MaxAge: l.MaxAge,
MaxBackups: l.MaxBackups,
Compress: l.Compress,
LocalTime: l.LocalTime,
}
lumberjacks[absPath] = lj
Expand All @@ -48,7 +50,8 @@ func (l LogRoller) GetLogWriter() io.Writer {
func IsLogRollerSubdirective(subdir string) bool {
return subdir == directiveRotateSize ||
subdir == directiveRotateAge ||
subdir == directiveRotateKeep
subdir == directiveRotateKeep ||
subdir == directiveRotateCompress
}

// ParseRoller parses roller contents out of c.
Expand All @@ -57,10 +60,14 @@ func ParseRoller(l *LogRoller, what string, where string) error {
l = DefaultLogRoller()
}
var value int
var valueBool bool
var err error
value, err = strconv.Atoi(where)
if err != nil {
return err
valueBool, err = strconv.ParseBool(where)
if err != nil {
return err
}
}
switch what {
case directiveRotateSize:
Expand All @@ -69,6 +76,8 @@ func ParseRoller(l *LogRoller, what string, where string) error {
l.MaxAge = value
case directiveRotateKeep:
l.MaxBackups = value
case directiveRotateCompress:
l.Compress = valueBool
}
return nil
}
Expand All @@ -79,6 +88,7 @@ func DefaultLogRoller() *LogRoller {
MaxSize: defaultRotateSize,
MaxAge: defaultRotateAge,
MaxBackups: defaultRotateKeep,
Compress: defaultRotateCompress,
LocalTime: true,
}
}
Expand All @@ -89,10 +99,14 @@ const (
// defaultRotateAge is 14 days.
defaultRotateAge = 14
// defaultRotateKeep is 10 files.
defaultRotateKeep = 10
directiveRotateSize = "rotate_size"
directiveRotateAge = "rotate_age"
directiveRotateKeep = "rotate_keep"
defaultRotateKeep = 10
// defaultRotateCompress is false.
defaultRotateCompress = false

directiveRotateSize = "rotate_size"
directiveRotateAge = "rotate_age"
directiveRotateKeep = "rotate_keep"
directiveRotateCompress = "rotate_compress"
)

// lumberjacks maps log filenames to the logger
Expand Down
3 changes: 2 additions & 1 deletion caddyhttp/log/setup_test.go
Expand Up @@ -194,7 +194,7 @@ func TestLogParse(t *testing.T) {
Format: "{when}",
}},
}}},
{`log access.log { rotate_size 2 rotate_age 10 rotate_keep 3 }`, false, []Rule{{
{`log access.log { rotate_size 2 rotate_age 10 rotate_keep 3 rotate_compress false }`, false, []Rule{{
PathScope: "/",
Entries: []*Entry{{
Log: &httpserver.Logger{
Expand All @@ -203,6 +203,7 @@ func TestLogParse(t *testing.T) {
MaxSize: 2,
MaxAge: 10,
MaxBackups: 3,
Compress: false,
LocalTime: true,
}},
Format: DefaultLogFormat,
Expand Down

0 comments on commit 7d66158

Please sign in to comment.