harbor/job/utils/logger.go

28 lines
750 B
Go
Raw Normal View History

2016-05-11 10:05:19 +02:00
package utils
2016-04-20 08:24:17 +02:00
import (
"fmt"
2016-05-11 10:05:19 +02:00
"github.com/vmware/harbor/job/config"
2016-04-20 08:24:17 +02:00
"github.com/vmware/harbor/utils/log"
"os"
"path/filepath"
2016-04-20 08:24:17 +02:00
)
// NewLogger create a logger for a speicified job
func NewLogger(jobID int64) *log.Logger {
logFile := GetJobLogPath(jobID)
f, err := os.OpenFile(logFile, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
2016-04-20 08:24:17 +02:00
if err != nil {
log.Errorf("Failed to open log file %s, the log of job %d will be printed to standard output", logFile, jobID)
f = os.Stdout
2016-04-20 08:24:17 +02:00
}
return log.New(f, log.NewTextFormatter(), log.InfoLevel)
2016-04-20 08:24:17 +02:00
}
// GetJobLogPath returns the absolute path in which the job log file is located.
func GetJobLogPath(jobID int64) string {
fn := fmt.Sprintf("job_%d.log", jobID)
return filepath.Join(config.LogDir(), fn)
2016-04-20 08:24:17 +02:00
}