Skip to content

Commit

Permalink
#123: log Console
Browse files Browse the repository at this point in the history
  • Loading branch information
stou committed Jan 26, 2019
1 parent 5d6c1ce commit 0c0cdf0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
5 changes: 1 addition & 4 deletions daemonize.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func Deamonize(proc func()) {
context := new(daemon.Context)
context := daemon.Context{LogFileName: "/dev/stdout"}

child, err := context.Reborn()
if err != nil {
Expand All @@ -18,8 +18,5 @@ func Deamonize(proc func()) {
return
}
defer context.Release()

log.Info("daemon started")

proc()
}
14 changes: 11 additions & 3 deletions logger/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,12 +573,12 @@ func (cl *CompositeLogger) ClearAllLogFile() error {
// create a logger for a program with parameters
//
func NewLogger(programName string, logFile string, locker sync.Locker, maxBytes int64, backups int, logEventEmitter LogEventEmitter) Logger {
files := strings.Split(logFile, ",")
files := splitLogFile(logFile)
loggers := make([]Logger, 0)

fmt.Printf("before split:%s\n", logFile)
for i, f := range files {
fmt.Printf("logFile:-%s-\n", f)
var lr Logger
f = strings.TrimSpace(f)
if i == 0 {
lr = createLogger(programName, f, locker, maxBytes, backups, logEventEmitter)
} else {
Expand All @@ -593,6 +593,14 @@ func NewLogger(programName string, logFile string, locker sync.Locker, maxBytes
}
}

func splitLogFile(logFile string) []string {
files := strings.Split(logFile, ",")
for i, f := range files {
files[i] = strings.TrimSpace(f)
}
return files
}

func createLogger(programName string, logFile string, locker sync.Locker, maxBytes int64, backups int, logEventEmitter LogEventEmitter) Logger {
if logFile == "/dev/stdout" {
return NewStdoutLogger(logEventEmitter)
Expand Down
17 changes: 17 additions & 0 deletions logger/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,20 @@ func TestWriteSingleLog(t *testing.T) {
}
logger.Close()
}

func TestSplitLogFile(t *testing.T) {
files := splitLogFile(" test1.log, /dev/stdout, test2.log ")
if len(files) != 3 {
t.Error("Fail to split log file")
}
if files[0] != "test1.log" {
t.Error("Fail to get first log file")
}
if files[1] != "/dev/stdout" {
t.Error("Fail to get second log file")
}
if files[2] != "test2.log" {
t.Error("Fail to get third log file")
}

}
7 changes: 5 additions & 2 deletions supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,16 +492,19 @@ func (s *Supervisor) setSupervisordInfo() {
if err != nil {
logFile, err = process.Path_expand(logFile)
}
if logFile == "/dev/stdout" {
return
}
logEventEmitter := logger.NewNullLogEventEmitter()
s.logger = logger.NewNullLogger(logEventEmitter)
if err == nil {
logfile_maxbytes := int64(supervisordConf.GetBytes("logfile_maxbytes", 50*1024*1024))
logfile_backups := supervisordConf.GetInt("logfile_backups", 10)
loglevel := supervisordConf.GetString("loglevel", "info")
s.logger = logger.NewLogger("supervisord", logFile, &sync.Mutex{}, logfile_maxbytes, logfile_backups, logEventEmitter)
log.SetOutput(s.logger)
log.SetLevel(toLogLevel(loglevel))
log.SetFormatter(&log.TextFormatter{DisableColors: true})
log.SetFormatter(&log.TextFormatter{DisableColors: true, FullTimestamp: true})
log.SetOutput(s.logger)
}
//set the pid
pidfile, err := env.Eval(supervisordConf.GetString("pidfile", "supervisord.pid"))
Expand Down

0 comments on commit 0c0cdf0

Please sign in to comment.