-
Notifications
You must be signed in to change notification settings - Fork 223
/
log.go
39 lines (33 loc) · 979 Bytes
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package log
import (
"github.com/jfrog/jfrog-cli-core/utils/coreutils"
"github.com/jfrog/jfrog-cli-core/utils/log"
"github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"os"
"path/filepath"
"strconv"
"time"
)
func CreateLogFile() (*os.File, error) {
logDir, err := coreutils.CreateDirInJfrogHome(coreutils.JfrogLogsDirName)
if err != nil {
return nil, err
}
currentTime := time.Now().Format("2006-01-02.15-04-05")
pid := os.Getpid()
fileName := filepath.Join(logDir, "jfrog-cli."+currentTime+"."+strconv.Itoa(pid)+".log")
file, err := os.OpenFile(fileName, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
return nil, errorutils.CheckError(err)
}
return file, nil
}
// Closes the log file and resets to the default logger
func CloseLogFile(logFile *os.File) {
if logFile != nil {
log.SetDefaultLogger()
err := logFile.Close()
utils.CheckErrorWithMessage(err, "failed closing the log file")
}
}