Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type Logger struct {
mutex *sync.Mutex
}

var pid = os.Getpid()

// NewLogger creates a new Logger.
func NewLogger(name string, level int, target int) *Logger {
var logger Logger
Expand Down Expand Up @@ -190,7 +192,7 @@ func (logger *Logger) logf(format string, args ...interface{}) {
if logger.callCount%rotationCheckFrq == 0 {
logger.rotate()
}

format = fmt.Sprintf("[%v] %s", pid, format)
logger.callCount++
logger.l.Printf(format, args...)
}
Expand Down
26 changes: 26 additions & 0 deletions log/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
package log

import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
)

Expand Down Expand Up @@ -48,3 +51,26 @@ func TestLogFileRotatesWhenSizeLimitIsReached(t *testing.T) {
}
os.Remove(fn)
}

func TestPid(t *testing.T) {
l := NewLogger(logName, LevelInfo, TargetLogfile)
if l == nil {
t.Fatalf("Failed to create logger.")
}

l.Printf("LogText %v", 1)
l.Close()
fn := l.GetLogDirectory() + logName + ".log"
defer os.Remove(fn)

logBytes, err := ioutil.ReadFile(fn)
if err != nil {
t.Fatalf("Failed to read log, %v", err)
}
log := string(logBytes)
exptectedLog := fmt.Sprintf("[%v] LogText 1", os.Getpid());

if !strings.Contains(log, exptectedLog){
t.Fatalf("Unexpected log: %s.", log)
}
}