-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdlogging.go
More file actions
41 lines (35 loc) · 908 Bytes
/
cmdlogging.go
File metadata and controls
41 lines (35 loc) · 908 Bytes
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
package main
import (
"github.com/kui/liner"
"log"
"os/exec"
)
func main() {
// use the default line processor which just print lines with log.Printf
outw := liner.NewLiningWriter(nil, nil)
defer outw.Close()
// use a custom line processor which just print lines with log.Printf
errw := liner.NewLiningWriter(func(line string) error {
log.Printf("stderr: %s\n", line)
return nil
}, nil)
defer errw.Close()
c := exec.Command("bash", "-c", "seq 5; seq 6 10 >&2")
c.Stdout = outw
c.Stderr = errw
err := c.Run()
if err != nil {
log.Fatal(err)
}
// $ go run _example/cmdlogging.go
// 2016/01/31 16:23:23 1
// 2016/01/31 16:23:23 2
// 2016/01/31 16:23:23 3
// 2016/01/31 16:23:23 4
// 2016/01/31 16:23:23 5
// 2016/01/31 16:23:23 stderr: 6
// 2016/01/31 16:23:23 stderr: 7
// 2016/01/31 16:23:23 stderr: 8
// 2016/01/31 16:23:23 stderr: 9
// 2016/01/31 16:23:23 stderr: 10
}