Skip to content

Commit

Permalink
rotate support time
Browse files Browse the repository at this point in the history
  • Loading branch information
absolute8511 committed Jun 29, 2016
1 parent b6b0b11 commit 273be26
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
30 changes: 23 additions & 7 deletions glog.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ func (t *traceLocation) Set(value string) error {
type flushSyncWriter interface {
Flush() error
Sync() error
CheckRotote()
io.Writer
}

Expand All @@ -400,6 +401,8 @@ func init() {
// Default stderrThreshold is ERROR.
innerlogging.stderrThreshold = errorLog
innerlogging.alsoToStderr = true
innerlogging.MaxSize = int64(MaxSize)
innerlogging.RotateDays = 10
*glogDir = "./glog"

innerlogging.setVState(0, nil, false)
Expand All @@ -419,6 +422,8 @@ func InitWithFlag(f *flag.FlagSet) {
}
f.BoolVar(&innerlogging.toStderr, "logtostderr", false, "log to standard error instead of files")
f.BoolVar(&innerlogging.alsoToStderr, "alsologtostderr", false, "log to standard error as well as files")
f.Int64Var(&innerlogging.MaxSize, "glog_maxsize", int64(MaxSize), "log max size before rotate")
f.Int64Var(&innerlogging.RotateDays, "glog_rotate_days", 10, "days before rotate")
f.Var(&innerlogging.verbosity, "gverb", "log level for V logs")
f.Var(&innerlogging.stderrThreshold, "stderrthreshold", "logs at or above this threshold go to stderr")
f.Var(&innerlogging.vmodule, "vmodule", "comma-separated list of pattern=N settings for file-filtered logging")
Expand Down Expand Up @@ -468,9 +473,11 @@ type loggingT struct {
traceLocation traceLocation
// These flags are modified only under lock, although verbosity may be fetched
// safely using atomic.LoadInt32.
vmodule moduleSpec // The state of the -vmodule flag.
verbosity Level // V logging level, the value of the -v flag/
flag *flag.FlagSet
vmodule moduleSpec // The state of the -vmodule flag.
verbosity Level // V logging level, the value of the -v flag/
flag *flag.FlagSet
MaxSize int64
RotateDays int64
}

// buffer holds a byte Buffer for reuse. The zero value is ready for use.
Expand Down Expand Up @@ -831,17 +838,24 @@ func (l *loggingT) exit(err error) {
type syncBuffer struct {
logger *loggingT
*bufio.Writer
file *os.File
sev severity
nbytes uint64 // The number of bytes written to this file
file *os.File
sev severity
nbytes uint64 // The number of bytes written to this file
createTime time.Time
}

func (sb *syncBuffer) Sync() error {
return sb.file.Sync()
}

func (sb *syncBuffer) CheckRotote() {
if int64(time.Since(sb.createTime).Hours()) >= innerlogging.RotateDays*24 {
sb.rotateFile(time.Now())
}
}

func (sb *syncBuffer) Write(p []byte) (n int, err error) {
if sb.nbytes+uint64(len(p)) >= MaxSize {
if sb.nbytes+uint64(len(p)) >= uint64(innerlogging.MaxSize) {
if err := sb.rotateFile(time.Now()); err != nil {
sb.logger.exit(err)
}
Expand Down Expand Up @@ -877,6 +891,7 @@ func (sb *syncBuffer) rotateFile(now time.Time) error {
fmt.Fprintf(&buf, "Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msg\n")
n, err := sb.file.Write(buf.Bytes())
sb.nbytes += uint64(n)
sb.createTime = now
return err
}

Expand Down Expand Up @@ -929,6 +944,7 @@ func (l *loggingT) flushAll() {
if file != nil {
file.Flush() // ignore error
file.Sync() // ignore error
file.CheckRotote()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion glog_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
)

// MaxSize is the maximum size of a log file in bytes.
var MaxSize uint64 = 1024 * 1024 * 1800
var MaxSize uint64 = 1024 * 1024 * 100

// logDirs lists the candidate directories for new log files.
var glogDirs []string
Expand Down
4 changes: 4 additions & 0 deletions glog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func (f *flushBuffer) Sync() error {
return nil
}

func (f *flushBuffer) CheckRotote() {
}

// swap sets the log writers and returns the old array.
func (l *loggingT) swap(writers [numSeverity]flushSyncWriter) (old [numSeverity]flushSyncWriter) {
l.mu.Lock()
Expand Down Expand Up @@ -335,6 +338,7 @@ func TestRollover(t *testing.T) {
}
defer func(previous uint64) { MaxSize = previous }(MaxSize)
MaxSize = 512
innerlogging.MaxSize = int64(MaxSize)

Info("x") // Be sure we have a file.
info, ok := innerlogging.file[infoLog].(*syncBuffer)
Expand Down

0 comments on commit 273be26

Please sign in to comment.