func (s *Service) Open() error { switch s.c.File { case "STDERR": s.f = &nopCloser{f: s.stderr} case "STDOUT": s.f = &nopCloser{f: s.stdout} default: dir := path.Dir(s.c.File) if _, err := os.Stat(dir); os.IsNotExist(err) { err := os.MkdirAll(dir, 0755) if err != nil { return err } } f, err := os.OpenFile(s.c.File, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0640) if err != nil { return err } s.f = f } // Configure default logger log.SetPrefix("[log] ") log.SetFlags(log.LstdFlags) log.SetOutput(wlog.NewWriter(s.f)) wlog.SetLevelFromName(s.c.Level) return nil }
func main() { // Parse flags flag.Parse() // Setup logging log.SetOutput(wlog.NewWriter(os.Stderr)) if err := wlog.SetLevelFromName(*logLevel); err != nil { log.Fatal("E! ", err) } if *socket == "" { a := agent.New(os.Stdin, os.Stdout) h := newHandler(a) a.Handler = h log.Println("I! Starting agent using STDIN/STDOUT") a.Start() err := a.Wait() if err != nil { log.Fatal("E! ", err) } log.Println("I! Agent finished") } else { // Create unix socket addr, err := net.ResolveUnixAddr("unix", *socket) if err != nil { log.Fatal("E! ", err) } l, err := net.ListenUnix("unix", addr) if err != nil { log.Fatal("E! ", err) } // Create server that listens on the socket s := agent.NewServer(l, &accepter{}) // Setup signal handler to stop Server on various signals s.StopOnSignals(os.Interrupt, syscall.SIGTERM) log.Println("I! Socket server listening on", addr.String()) err = s.Serve() if err != nil { log.Fatal("E! ", err) } log.Println("I! Socket server stopped") } }
// newTelegrafWriter returns a logging-wrapped writer. func newTelegrafWriter(w io.Writer) io.Writer { return &telegrafLog{ writer: wlog.NewWriter(w), } }