mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-07-20 09:39:08 +00:00
Merge pull request #3109 from rn/log
Add '.log' as file extension to 'pkg/logwrite' files
This commit is contained in:
commit
ecd412d087
@ -22,10 +22,10 @@ services:
|
||||
- INSECURE=true
|
||||
# A service which generates log messages for testing
|
||||
- name: write-to-the-logs
|
||||
image: alpine
|
||||
image: alpine:3.8
|
||||
command: ["/bin/sh", "-c", "while /bin/true; do echo hello $(date); sleep 1; done" ]
|
||||
- name: write-and-rotate-logs
|
||||
image: linuxkit/logwrite:v0.5
|
||||
image: linuxkit/logwrite:d9778c0d538094d398cf0cbfc89277aeca67f1be
|
||||
- name: kmsg
|
||||
image: linuxkit/kmsg:v0.5
|
||||
trust:
|
||||
|
@ -62,15 +62,15 @@ func ParseLogMessage(line string) (*LogMessage, error) {
|
||||
// LogFile is where we write LogMessages to
|
||||
type LogFile struct {
|
||||
File *os.File // active file handle
|
||||
Name string // filename of log file
|
||||
Dir string // log file directory
|
||||
Path string // Path to the logfile
|
||||
BytesWritten int // total number of bytes written so far
|
||||
}
|
||||
|
||||
// NewLogFile creates a new LogFile.
|
||||
func NewLogFile(dir, name string) (*LogFile, error) {
|
||||
// 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 {
|
||||
return nil, err
|
||||
}
|
||||
@ -80,8 +80,7 @@ func NewLogFile(dir, name string) (*LogFile, error) {
|
||||
}
|
||||
return &LogFile{
|
||||
File: f,
|
||||
Name: name,
|
||||
Dir: dir,
|
||||
Path: p,
|
||||
BytesWritten: int(fi.Size()),
|
||||
}, nil
|
||||
}
|
||||
@ -106,15 +105,14 @@ func (l *LogFile) Rotate(maxLogFiles int) error {
|
||||
if err := l.File.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
path := filepath.Join(l.Dir, l.Name)
|
||||
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
|
||||
// foo foo.1 foo.2 up to foo.<maxLogFiles-1>
|
||||
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
|
||||
err := os.Rename(newerFile, olderFile)
|
||||
if os.IsNotExist(err) {
|
||||
@ -125,7 +123,7 @@ func (l *LogFile) Rotate(maxLogFiles int) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
f, err := os.Create(path)
|
||||
f, err := os.Create(l.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
#!/bin/sh
|
||||
|
||||
for i in $(seq 1 20); do
|
||||
if [ -e /var/log/fill-the-logs.out.0 ]; then
|
||||
if [ -e /var/log/fill-the-logs.out.log.0 ]; then
|
||||
printf "logwrite test suite PASSED\n" > /dev/console
|
||||
/sbin/poweroff -f
|
||||
fi
|
||||
|
@ -13,7 +13,7 @@ services:
|
||||
image: alpine
|
||||
command: ["/bin/sh", "-c", "while /bin/true; do echo hello $(date); done" ]
|
||||
- name: write-and-rotate-logs
|
||||
image: linuxkit/logwrite:v0.5
|
||||
image: linuxkit/logwrite:d9778c0d538094d398cf0cbfc89277aeca67f1be
|
||||
command: ["/usr/bin/logwrite", "-max-log-size", "1024"]
|
||||
- name: check-the-logs
|
||||
image: alpine:3.8
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
for i in $(seq 1 20); do
|
||||
# Look for a common kernel log message
|
||||
if grep "SCSI subsystem initialized" /var/log/kmsg.out 2>/dev/null; then
|
||||
if grep "SCSI subsystem initialized" /var/log/kmsg.out.log 2>/dev/null; then
|
||||
printf "kmsg test suite PASSED\n" > /dev/console
|
||||
/sbin/poweroff -f
|
||||
fi
|
||||
@ -10,6 +10,6 @@ for i in $(seq 1 20); do
|
||||
done
|
||||
|
||||
printf "kmsg test suite FAILED\n" > /dev/console
|
||||
echo "contents of /var/log/kmsg.out:" > /dev/console
|
||||
cat /var/log/kmsg.out > /dev/console
|
||||
echo "contents of /var/log/kmsg.out.log:" > /dev/console
|
||||
cat /var/log/kmsg.out.log > /dev/console
|
||||
/sbin/poweroff -f
|
||||
|
@ -11,7 +11,7 @@ services:
|
||||
- name: kmsg
|
||||
image: linuxkit/kmsg:v0.5
|
||||
- name: write-and-rotate-logs
|
||||
image: linuxkit/logwrite:v0.5
|
||||
image: linuxkit/logwrite:d9778c0d538094d398cf0cbfc89277aeca67f1be
|
||||
- name: check-the-logs
|
||||
image: alpine:3.8
|
||||
binds:
|
||||
|
Loading…
Reference in New Issue
Block a user