mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-07-20 17:49:10 +00:00
pkg/logwrite: Append .log to the log files
Also simplify the code by directly storing the path to the log file in the LogFile structure. Signed-off-by: Rolf Neugebauer <rolf.neugebauer@docker.com>
This commit is contained in:
parent
90765efac5
commit
e8786d73bb
@ -62,15 +62,15 @@ func ParseLogMessage(line string) (*LogMessage, error) {
|
|||||||
// LogFile is where we write LogMessages to
|
// LogFile is where we write LogMessages to
|
||||||
type LogFile struct {
|
type LogFile struct {
|
||||||
File *os.File // active file handle
|
File *os.File // active file handle
|
||||||
Name string // filename of log file
|
Path string // Path to the logfile
|
||||||
Dir string // log file directory
|
|
||||||
BytesWritten int // total number of bytes written so far
|
BytesWritten int // total number of bytes written so far
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLogFile creates a new LogFile.
|
// NewLogFile creates a new LogFile.
|
||||||
func NewLogFile(dir, name string) (*LogFile, error) {
|
func NewLogFile(dir, name string) (*LogFile, error) {
|
||||||
// If the log exists already we want to append to it.
|
// If the log exists already we want to append to it.
|
||||||
f, err := os.OpenFile(filepath.Join(dir, name), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
|
p := filepath.Join(dir, name+".log")
|
||||||
|
f, err := os.OpenFile(p, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -80,8 +80,7 @@ func NewLogFile(dir, name string) (*LogFile, error) {
|
|||||||
}
|
}
|
||||||
return &LogFile{
|
return &LogFile{
|
||||||
File: f,
|
File: f,
|
||||||
Name: name,
|
Path: p,
|
||||||
Dir: dir,
|
|
||||||
BytesWritten: int(fi.Size()),
|
BytesWritten: int(fi.Size()),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
@ -106,15 +105,14 @@ func (l *LogFile) Rotate(maxLogFiles int) error {
|
|||||||
if err := l.File.Close(); err != nil {
|
if err := l.File.Close(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
path := filepath.Join(l.Dir, l.Name)
|
|
||||||
for i := maxLogFiles - 1; i >= 0; i-- {
|
for i := maxLogFiles - 1; i >= 0; i-- {
|
||||||
newerFile := fmt.Sprintf("%s.%d", path, i-1)
|
newerFile := fmt.Sprintf("%s.%d", l.Path, i-1)
|
||||||
// special case: if index is 0 we omit the suffix i.e. we expect
|
// special case: if index is 0 we omit the suffix i.e. we expect
|
||||||
// foo foo.1 foo.2 up to foo.<maxLogFiles-1>
|
// foo foo.1 foo.2 up to foo.<maxLogFiles-1>
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
newerFile = path
|
newerFile = l.Path
|
||||||
}
|
}
|
||||||
olderFile := fmt.Sprintf("%s.%d", path, i)
|
olderFile := fmt.Sprintf("%s.%d", l.Path, i)
|
||||||
// overwrite the olderFile with the newerFile
|
// overwrite the olderFile with the newerFile
|
||||||
err := os.Rename(newerFile, olderFile)
|
err := os.Rename(newerFile, olderFile)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
@ -125,7 +123,7 @@ func (l *LogFile) Rotate(maxLogFiles int) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
f, err := os.Create(path)
|
f, err := os.Create(l.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user