• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang termlog.Logger类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/cortesi/termlog.Logger的典型用法代码示例。如果您正苦于以下问题:Golang Logger类的具体用法?Golang Logger怎么用?Golang Logger使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Logger类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: WrapHandler

// WrapHandler wraps an httpctx.Handler in the paraphernalia needed by devd for
// logging, latency, and so forth.
func (dd *Devd) WrapHandler(log termlog.Logger, next httpctx.Handler) http.Handler {
	h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		revertOriginalHost(r)
		timr := timer.Timer{}
		sublog := log.Group()
		defer func() {
			timing := termlog.DefaultPalette.Timestamp.SprintFunc()("timing: ")
			sublog.SayAs("timer", timing+timr.String())
			sublog.Done()
		}()
		if matchStringAny(dd.IgnoreLogs, fmt.Sprintf("%s%s", r.URL.Host, r.RequestURI)) {
			sublog.Quiet()
		}
		timr.RequestHeaders()
		time.Sleep(time.Millisecond * time.Duration(dd.Latency))
		sublog.Say("%s %s", r.Method, r.URL)
		LogHeader(sublog, r.Header)
		ctx := timr.NewContext(context.Background())
		ctx = termlog.NewContext(ctx, sublog)
		next.ServeHTTPContext(
			ctx,
			&ResponseLogWriter{Log: sublog, Resp: w, Timer: &timr},
			r,
		)
	})
	return h
}
开发者ID:roryokane,项目名称:devd,代码行数:29,代码来源:server.go


示例2: shouldInclude

// Determine if a file should be included, based on the given exclude paths.
func shouldInclude(file string, excludePatterns []string, log termlog.Logger) bool {
	for _, pattern := range excludePatterns {
		match, err := doublestar.Match(pattern, file)
		if err != nil {
			log.Warn("Error matching pattern '%s': %s", pattern, err)
		} else if match {
			return false
		}
	}
	return true
}
开发者ID:roryokane,项目名称:devd,代码行数:12,代码来源:watch.go


示例3: dirList

func dirList(ci inject.CopyInject, logger termlog.Logger, w http.ResponseWriter, name string, f http.File, templates *template.Template) {
	w.Header().Set("Cache-Control", "no-store, must-revalidate")
	files, err := f.Readdir(0)
	if err != nil {
		logger.Shout("Error reading directory for listing: %s", err)
		return
	}
	data := dirData{Name: name, Files: files}
	err = ci.ServeTemplate(http.StatusOK, w, templates.Lookup("dirlist.html"), data)
	if err != nil {
		logger.Shout("Failed to generate dir listing: %s", err)
	}
}
开发者ID:chrissexton,项目名称:devd,代码行数:13,代码来源:fileserver.go


示例4: Serve

// Serve starts the devd server. The callback is called with the serving URL
// just before service starts.
func (dd *Devd) Serve(address string, port int, certFile string, logger termlog.Logger, callback func(string)) error {
	templates, err := ricetemp.MakeTemplates(rice.MustFindBox("templates"))
	if err != nil {
		return fmt.Errorf("Error loading templates: %s", err)
	}
	mux, err := dd.Router(logger, templates)
	if err != nil {
		return err
	}
	var tlsConfig *tls.Config
	var tlsEnabled bool
	if certFile != "" {
		tlsConfig, err = getTLSConfig(certFile)
		if err != nil {
			return fmt.Errorf("Could not load certs: %s", err)
		}
		tlsEnabled = true
	}

	var hl net.Listener
	if port > 0 {
		hl, err = net.Listen("tcp", fmt.Sprintf("%v:%d", address, port))
	} else {
		hl, err = pickPort(address, portLow, portHigh, tlsEnabled)
	}
	if err != nil {
		return fmt.Errorf("Could not bind to port: %s", err)
	}

	if tlsConfig != nil {
		hl = tls.NewListener(hl, tlsConfig)
	}

	hl = slowdown.NewSlowListener(hl, dd.UpKbps*1024, dd.DownKbps*1024)
	url := formatURL(tlsEnabled, address, hl.Addr().(*net.TCPAddr).Port)
	logger.Say("Listening on %s (%s)", url, hl.Addr().String())
	server := &http.Server{Addr: hl.Addr().String(), Handler: mux}
	callback(url)
	err = server.Serve(hl)
	logger.Shout("Server stopped: %v", err)
	return nil
}
开发者ID:roryokane,项目名称:devd,代码行数:44,代码来源:server.go


示例5: LogHeader

// LogHeader logs a header
func LogHeader(log termlog.Logger, h http.Header) {
	max := 0
	for k := range h {
		if len(k) > max {
			max = len(k)
		}
	}
	for k, vals := range h {
		for _, v := range vals {
			pad := fmt.Sprintf(fmt.Sprintf("%%%ds", max-len(k)+1), " ")
			log.SayAs(
				"headers",
				"\t%s%s%s",
				color.BlueString(k)+":",
				pad,
				v,
			)
		}
	}
}
开发者ID:carriercomm,项目名称:devd,代码行数:21,代码来源:logheader.go


示例6: WatchPaths

// WatchPaths watches a set of paths, and broadcasts changes through reloader.
func WatchPaths(paths, excludePatterns []string, reloader livereload.Reloader, log termlog.Logger) error {
	ch := make(chan []string, 1)
	for _, path := range paths {
		modchan := make(chan *moddwatch.Mod, 1)
		_, err := moddwatch.Watch([]string{path}, batchTime, modchan)
		if err != nil {
			return err
		}
		go func() {
			for mod := range modchan {
				filteredMod, err := mod.Filter([]string{"**/*"}, excludePatterns)
				if err != nil {
					log.Shout("Error filtering watches: %s", err)
				}
				if !filteredMod.Empty() {
					ch <- filteredMod.All()
				}
			}
		}()
	}
	go reloader.Watch(ch)
	return nil
}
开发者ID:cortesi,项目名称:devd,代码行数:24,代码来源:watch.go


示例7: Watch

// Watch watches an endpoint for changes, if it supports them.
func (r Route) Watch(ch chan []string, excludePatterns []string, log termlog.Logger) error {
	switch r.Endpoint.(type) {
	case *filesystemEndpoint:
		ep := *r.Endpoint.(*filesystemEndpoint)
		modchan := make(chan *moddwatch.Mod, 1)
		_, err := moddwatch.Watch([]string{ep.Root + "/..."}, batchTime, modchan)
		if err != nil {
			return err
		}
		go func() {
			for mod := range modchan {
				filteredMod, err := mod.Filter([]string{"**/*"}, excludePatterns)
				if err != nil {
					log.Shout("Error filtering watches: %s", err)
				}
				if !filteredMod.Empty() {
					ch <- filteredMod.All()
				}
			}
		}()
	}
	return nil
}
开发者ID:cortesi,项目名称:devd,代码行数:24,代码来源:watch.go


示例8: dirList

func (fserver *FileServer) dirList(logger termlog.Logger, w http.ResponseWriter, name string, f http.File) {
	w.Header().Set("Cache-Control", "no-store, must-revalidate")
	files, err := f.Readdir(0)
	if err != nil {
		logger.Shout("Error reading directory for listing: %s", err)
		return
	}
	sortedFiles := fileSlice(files)
	sort.Sort(sortedFiles)
	data := dirData{
		Version: fserver.Version,
		Name:    name,
		Files:   sortedFiles,
	}
	err = fserver.Inject.ServeTemplate(
		http.StatusOK,
		w,
		fserver.Templates.Lookup("dirlist.html"),
		data,
	)
	if err != nil {
		logger.Shout("Failed to generate dir listing: %s", err)
	}
}
开发者ID:cortesi,项目名称:devd,代码行数:24,代码来源:fileserver.go


示例9: serveFile

// name is '/'-separated, not filepath.Separator.
func (fserver *FileServer) serveFile(
	logger termlog.Logger,
	w http.ResponseWriter,
	r *http.Request,
	name string,
	redirect bool,
) {
	const indexPage = "/index.html"

	// redirect .../index.html to .../
	// can't use Redirect() because that would make the path absolute,
	// which would be a problem running under StripPrefix
	if strings.HasSuffix(r.URL.Path, indexPage) {
		logger.SayAs(
			"debug", "debug fileserver: redirecting %s -> ./", indexPage,
		)
		localRedirect(w, r, "./")
		return
	}

	f, err := fserver.Root.Open(name)
	if err != nil {
		logger.WarnAs("debug", "debug fileserver: %s", err)
		if err := fserver.notFound(logger, w, r, name, nil); err != nil {
			logger.Shout("Internal error: %s", err)
		}
		return
	}
	defer func() { _ = f.Close() }()

	d, err1 := f.Stat()
	if err1 != nil {
		logger.WarnAs("debug", "debug fileserver: %s", err)
		if err := fserver.notFound(logger, w, r, name, nil); err != nil {
			logger.Shout("Internal error: %s", err)
		}
		return
	}

	if redirect {
		// redirect to canonical path: / at end of directory url
		url := r.URL.Path
		if !strings.HasPrefix(url, "/") {
			url = "/" + url
		}
		if d.IsDir() {
			if url[len(url)-1] != '/' {
				localRedirect(w, r, path.Base(url)+"/")
				return
			}
		} else if url[len(url)-1] == '/' {
			localRedirect(w, r, "../"+path.Base(url))
			return
		}
	}

	// use contents of index.html for directory, if present
	if d.IsDir() {
		index := name + indexPage
		ff, err := fserver.Root.Open(index)
		if err == nil {
			defer func() { _ = ff.Close() }()
			dd, err := ff.Stat()
			if err == nil {
				name = index
				d = dd
				f = ff
			}
		}
	}

	// Still a directory? (we didn't find an index.html file)
	if d.IsDir() {
		if err := fserver.notFound(logger, w, r, name, &f); err != nil {
			logger.Shout("Internal error: %s", err)
		}
		return
	}

	// serverContent will check modification time
	sizeFunc := func() (int64, error) { return d.Size(), nil }
	err = serveContent(fserver.Inject, w, r, d.Name(), d.ModTime(), sizeFunc, f)
	if err != nil {
		logger.Warn("Error serving file: %s", err)
	}
}
开发者ID:cortesi,项目名称:devd,代码行数:87,代码来源:fileserver.go


示例10: notFound

func (fserver *FileServer) notFound(
	logger termlog.Logger,
	w http.ResponseWriter,
	r *http.Request,
	name string,
	dir *http.File,
) (err error) {
	sm := http.NewServeMux()
	seen := make(map[string]bool)
	for _, nfr := range fserver.NotFoundRoutes {
		seen[nfr.MuxMatch()] = true
		sm.HandleFunc(
			nfr.MuxMatch(),
			func(nfr routespec.RouteSpec) func(w http.ResponseWriter, r *http.Request) {
				return func(w http.ResponseWriter, r *http.Request) {
					if matchTypes(nfr.Value, r.URL.Path) {
						for _, pth := range notFoundSearchPaths(name, nfr.Value) {
							next, err := fserver.serveNotFoundFile(w, r, pth)
							if err != nil {
								logger.Shout("Unable to serve not-found override: %s", err)
							}
							if !next {
								return
							}
						}
					}
					err = fserver.serve404(w)
					if err != nil {
						logger.Shout("Internal error: %s", err)
					}
				}
			}(nfr),
		)
	}
	if _, exists := seen["/"]; !exists {
		sm.HandleFunc(
			"/",
			func(response http.ResponseWriter, request *http.Request) {
				if dir != nil {
					d, err := (*dir).Stat()
					if err != nil {
						logger.Shout("Internal error: %s", err)
						return
					}
					if checkLastModified(response, request, d.ModTime()) {
						return
					}
					fserver.dirList(logger, response, name, *dir)
					return
				}
				err = fserver.serve404(w)
				if err != nil {
					logger.Shout("Internal error: %s", err)
				}
			},
		)
	}
	handle, _ := sm.Handler(r)
	handle.ServeHTTP(w, r)
	return err
}
开发者ID:cortesi,项目名称:devd,代码行数:61,代码来源:fileserver.go


示例11: testpatt

func testpatt(l termlog.Logger) {
	l.Say("Log")
	l.Notice("Notice!")
	l.Warn("Warn!")
	l.Shout("Error!")
}
开发者ID:cortesi,项目名称:termlog,代码行数:6,代码来源:termlog_demo.go



注:本文中的github.com/cortesi/termlog.Logger类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang termlog.TermLog类代码示例发布时间:2022-05-23
下一篇:
Golang varcmd.VarCmd类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap