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
|
|
|
|
2016-05-23 13:39:13 +02:00
|
|
|
"github.com/vmware/harbor/job/config"
|
2016-04-20 08:24:17 +02:00
|
|
|
"github.com/vmware/harbor/utils/log"
|
2016-05-23 13:39:13 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-04-20 08:24:17 +02:00
|
|
|
)
|
|
|
|
|
2016-05-30 08:44:03 +02:00
|
|
|
// NewLogger create a logger for a speicified job
|
2016-05-23 13:39:13 +02:00
|
|
|
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 {
|
2016-05-23 13:39:13 +02:00
|
|
|
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
|
|
|
}
|
2016-05-23 13:39:13 +02:00
|
|
|
return log.New(f, log.NewTextFormatter(), log.InfoLevel)
|
2016-04-20 08:24:17 +02:00
|
|
|
}
|
|
|
|
|
2016-05-30 08:44:03 +02:00
|
|
|
// GetJobLogPath returns the absolute path in which the job log file is located.
|
2016-05-23 13:39:13 +02:00
|
|
|
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
|
|
|
}
|