// setup sets up the logging middleware. func setup(c *caddy.Controller) error { rules, err := logParse(c) if err != nil { return err } // Open the log files for writing when the server starts c.OnStartup(func() error { for i := 0; i < len(rules); i++ { var err error var writer io.Writer if rules[i].OutputFile == "stdout" { writer = os.Stdout } else if rules[i].OutputFile == "stderr" { writer = os.Stderr } else if rules[i].OutputFile == "syslog" { writer, err = gsyslog.NewLogger(gsyslog.LOG_INFO, "LOCAL0", "caddy") if err != nil { return err } } else { var file *os.File file, err = os.OpenFile(rules[i].OutputFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644) if err != nil { return err } if rules[i].Roller != nil { file.Close() rules[i].Roller.Filename = rules[i].OutputFile writer = rules[i].Roller.GetLogWriter() } else { rules[i].file = file writer = file } } rules[i].Log = log.New(writer, "", 0) } return nil }) // When server stops, close any open log files c.OnShutdown(func() error { for _, rule := range rules { if rule.file != nil { rule.file.Close() } } return nil }) httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler { return Logger{Next: next, Rules: rules, ErrorFunc: httpserver.DefaultErrorFunc} }) return nil }
// setup used internally by Caddy to set up this middleware func setup(c *caddy.Controller) error { mc, err := parse(c) if err != nil { return err } if c.ServerBlockKeyIndex == 0 { // only run when the first hostname has been loaded. if mc.maillog, err = mc.maillog.Init(c.ServerBlockKeys...); err != nil { return err } if err = mc.loadFromEnv(); err != nil { return err } if err = mc.loadPGPKeys(); err != nil { return err } if err = mc.loadTemplate(); err != nil { return err } if err = mc.pingSMTP(); err != nil { return err } c.ServerBlockStorage = newHandler(mc, startMailDaemon(mc)) } c.OnShutdown(func() error { if moh, ok := c.ServerBlockStorage.(*handler); ok { if moh.reqPipe != nil { close(moh.reqPipe) moh.reqPipe = nil } } return nil }) fmt.Printf("%#v\n\n", c) if moh, ok := c.ServerBlockStorage.(*handler); ok { // moh = mailOutHandler ;-) httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler { moh.Next = next return moh }) return nil } return errors.New("[mailout] Could not create the middleware handler") }
// setup configures a new errors middleware instance. func setup(c *caddy.Controller) error { handler, err := errorsParse(c) if err != nil { return err } // Open the log file for writing when the server starts c.OnStartup(func() error { var err error var writer io.Writer switch handler.LogFile { case "visible": handler.Debug = true case "stdout": writer = os.Stdout case "stderr": writer = os.Stderr case "syslog": writer, err = gsyslog.NewLogger(gsyslog.LOG_ERR, "LOCAL0", "caddy") if err != nil { return err } default: if handler.LogFile == "" { writer = os.Stderr // default break } var file *os.File file, err = os.OpenFile(handler.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644) if err != nil { return err } if handler.LogRoller != nil { file.Close() handler.LogRoller.Filename = handler.LogFile writer = handler.LogRoller.GetLogWriter() } else { handler.file = file writer = file } } handler.Log = log.New(writer, "", 0) return nil }) // When server stops, close any open log file c.OnShutdown(func() error { if handler.file != nil { handler.file.Close() } return nil }) httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler { handler.Next = next return handler }) return nil }