forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.go
54 lines (44 loc) · 1.3 KB
/
logging.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"fmt"
"os"
"time"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
"www.velocidex.com/golang/velociraptor/json"
logging "www.velocidex.com/golang/velociraptor/logging"
)
type LogWriter struct {
config_obj *config_proto.Config
}
func (self *LogWriter) Write(b []byte) (int, error) {
level, msg := logging.SplitIntoLevelAndLog(b)
logger := logging.GetLogger(self.config_obj, &logging.ClientComponent)
logger.LogWithLevel(level, "%s", msg)
return len(b), nil
}
type StdoutLogWriter struct{}
func (self *StdoutLogWriter) Write(b []byte) (int, error) {
fmt.Printf("%v", string(b))
return len(b), nil
}
func Prelog(format string, v ...interface{}) {
// If the logging_flag is specified we log prelogs immediately -
// this is necessary to be able to capture problems with the
// config file because we are unable to initialize our real
// logging system until we have a valid config.
if *logging_flag != "" {
fd, err := os.OpenFile(*logging_flag, os.O_RDWR|os.O_CREATE, 0600)
if err == nil {
fd.Seek(0, os.SEEK_END)
// Write a JSONL log line
fd.Write([]byte(json.Format(
`{"level":"prelog","msg":%q,"time":%q}`,
fmt.Sprintf(format, v...), time.Now(),
)))
fd.Write([]byte("\n"))
fd.Close()
return
}
}
logging.Prelog(format, v...)
}