Exemple #1
0
func (p *program) initLogger() error {
	c := GetConfig().WebServerConfig()

	f, err := os.Stat(c.Log_Dir)
	if err == nil {
		if !f.IsDir() {
			return errors.New("Log path exists but it's file not dir")
		}
	} else {
		if os.IsNotExist(err) {
			if err := os.MkdirAll(c.Log_Dir, 0755); err != nil {
				return err
			}
		} else {
			return err
		}
	}

	systemLogFile := filepath.Join(c.Log_Dir, c.System_Log)
	verbosity := golog.LDefault | golog.LHeaderFooter
	if golog.ToLogLevel(c.Log_Level) == golog.DEBUG {
		verbosity = verbosity | golog.LFile | golog.LMicroseconds
	}
	golog.NewLogger("general", systemLogFile, &golog.LoggerConfig{
		Level:          golog.ToLogLevel(c.Log_Level),
		Verbosity:      verbosity,
		FileRotateSize: (2 << 23), /*16MB*/
		FileDepth:      5,
	})

	if c.Enable_Access_Log {
		accessLogFile := filepath.Join(c.Log_Dir, c.Access_Log)
		golog.NewLogger("access", accessLogFile, &golog.LoggerConfig{
			MessageQueueSize: 100000,
			Level:            golog.INFO,
			Verbosity:        golog.LHeaderFooter,
			HeaderWriter:     httpwaymid.AccessLogHeaderWriter,
			FileRotateSize:   (2 << 23), /*16MB*/
		})
	}

	return nil
}
Exemple #2
0
func (p *program) Start(srv service.Service) error {
	p.router = httpway.New()

	c := GetConfig().WebServerConfig()
	if c.Enable_Access_Log {
		accessLogger := golog.GetLogger("access").Info
		p.router = p.router.Middleware(httpwaymid.AccessLog(accessLogger))
	}

	if golog.ToLogLevel(c.Log_Level) != golog.DEBUG {
		p.router = p.router.Middleware(httpwaymid.PanicCatcher)
	}

	p.server = p.initWebServer()

	p.router.Logger = golog.GetLogger("general")
	p.router.SessionManager = httpwaymid.NewSessionManager(c.Session_Timeout, c.Session_Expiration, golog.GetLogger("general"))

	if err := p.bootstrap(p.server, golog.GetLogger("general"), p.router); err != nil {
		return err
	}

	p.router = p.router.Middleware(httpwaymid.JSONRenderer("jsonData", "statusCode"))

	if c.Template_Dir != "" {
		p.router = p.router.Middleware(httpwaymid.TemplateRenderer(c.Template_Dir, "templateName", "templateData", "statusCode"))
	}

	p.router.NotFound = httpwaymid.NotFound(p.router)
	p.router.MethodNotAllowed = httpwaymid.MethodNotAllowed(p.router)

	middlewares := p.middlewareFactory(p.router)

	for middlewareName, routeFactory := range p.routeFactory {
		middleware, ok := middlewares[middlewareName]
		if !ok {
			panic("No middleware chain registered for " + middlewareName)
		}

		routeFactory(middleware)
	}

	if err := p.server.Start(); err != nil {
		return err
	}

	return nil
}